Session 3 - Data Analysis
Session 3 - Data Analysis
YOUR INTRODUCTION TO
DATABASES & SQL PROGRAMMING LANGUAGE
SESSION 7 SESSION 8
Database design Project
and data presentations
visualisation
SESSION 6
Data management
using SQL coding
techniques
SESSION 5 SESSION 4
Data analysis
Data Analysis
SESSION 1 SESSION 2
Introduction to SQL SQL coding and
programming Database
language and Data management
Science technique
DATA ANALYSIS
MODULE 1: HTML
• Logical operators.
EX
A
NT
AM
SY
PL
E
SELECT SELECT
<column_name>,
<column_name> first_name,
last_name
FROM <table_name>; FROM person;
Table Person
first_name last_name telephone
Lucy Smith 020 7777 8888
Mike Peters 020 2222 3333
Julie Andrews 074 7878 1212
• These are relational operators that are used to compare the value of operands
(expressions) to produce a logical value. A logical value results to True or False.
• We will be using these operators in our queries to request specific set of results from a
table and also to filter results.
COMPARISON
OPERATORS Operator Meaning Example Result
CONDITION KEYWORDS
Operator Meaning
EX
A
NT
AM
SY
PL
Who are all the people in my class named
E
Mary and are older than 25 years ?
SELECT
<alias>.<column_name>, SELECT p.name, p.surname 🡨 SELECT clause
<alias>.<column_name> FROM clause
FROM <table_name> <alias>; FROM person AS p
WHERE
<alias>.<column_name> = condition WHERE p.name = ‘Mary’ WHERE clause
AND
<alias>.<column_name> > condition
AND p.age > 25; AND clause
X
EX
A
NT
AM
SY
PL
Who are all the people in my class
E
named Mary or their surname is
Smith ?
SELECT
<alias>.<column_name>, SELECT p.name, p.surname 🡨 SELECT clause
<alias>.<column_name> FROM clause
FROM <table_name> <alias>; FROM person p
WHERE
<alias>.<column_name> = condition WHERE p.name = ‘Mary’ WHERE clause
OR OR clause
<alias>.<column_name> > condition;
OR p.surname = ‘Smith’;
X
EX
A
NT
AM
SY
PL
Who are all the people in my class
E
who have email addresses?
PRACTICE
WRITE THE FOLLOWING QUERIES
• Find all savoury items that have either pork or beef filling.
• Find all sweet items and their price, except for cannoli.
• These operators are a bit less common, but extremely useful for, retrieving, searching
and analysing data.
• For example, we can use some of these operators to identify data patterns or to search
for results even if we do not have the exact search criteria.
Operator Meaning
LOGICAL OPERATORS
BETWEEN selects values within a given range
(NOT BETWEEN) (excludes values within given range)
LIKE Logical operator that determines if a character string
matches a specified pattern
IN allows you to specify multiple values in a WHERE
clause
IS Special operator, very much like ‘equals’, but is only
used for values that potentially might be NULL
X
EX
A
NT
AM
SY
PL
Who are all the customers in our
E
contact list that have placed an
order up to 5 times?
SELECT
<alias>.<column_name>, SELECT c.name, c.surname 🡨 SELECT clause
FROM clause
<alias>.<column_name>
FROM <table_name> <alias>; FROM customers c
WHERE WHERE clause
<alias>.<column_name> WHERE c.orders BETWEEN
BETWEEN
Value1 AND value2;
BETWEEN 1 AND 5; clause
• We use LIKE to search for a specific pattern in a column value.
• There are two important wildcards, which are often used in conjunction with the LIKE
operator. These wildcards help us to build a pattern expression.
• % - means zero, one or however many characters
• _ - means only one single character
LIKE Name LIKE ‘m%’ Any word/value that starts with ‘m’
OPERATOR
Name LIKE ‘%m’ Any word/value that ends with ‘m’
Name LIKE ‘%or%’ Any word/value that have "or" in any position
Name LIKE ‘_m%’ Any word/value that have ‘m’ in the second position
Name LIKE ‘m__%’ Any word/value that starts with ‘m’ and is at least 3
characters long
Name LIKE ‘m%t’ Any word/value that starts with ‘m’ and ends with ‘t’
X
EX
A
NT
AM
SY
PL
Who are all the customers in our
E
contact list, with first name
starting with the letter ‘M’?
SELECT
<alias>.<column_name>, SELECT c.name, c.surname 🡨 SELECT clause
<alias>.<column_name> FROM clause
FROM <table_name> AS <alias>;
FROM customers AS c
WHERE WHERE c.name WHERE clause
<alias>.<column_name> LIKE
LIKE ‘M%’; LIKE ‘M%’;
X
EX
A
NT
AM
SY
PL
Who are all the customers in our
E
contact list who are named Mary,
Julie, Katie or Jo?
SELECT
<alias>.<column_name>, SELECT c.name, c.surname 🡨 SELECT clause
FROM clause
<alias>.<column_name>
FROM <table_name> <alias>;
FROM customers c
WHERE WHERE c.name WHERE clause
<alias>.<column_name> IN clause
IN (value1, value2, value2); IN (‘Mary’, ‘Julie’, ‘Katie’, ‘Jo’)
X
EX
A
NT
AM
SY
PL
Who are all the customers in our
E
contacts that do not have an email
address?
PRACTICE
WRITE THE FOLLOWING QUERIES
• Find all sweet items that start with the letter ‘c’
• Find all savoury items that cost more than £1, but less than £3
• Sometimes we want our result data to be presented in a different way to a simple set
of values returned by SELECT statement
• We don’t just want to extract data, but most importantly, we want to:
AGGREGATION • organize
AND • understand
ORDER • interpret
• analyse
• We are going to learn how to shape our result set with very useful ORDER BY and
GROUP BY statements.
• This clause is used with SELECT statement for arranging retrieved data in sorted order.
ORDER BY
• We need to specify one or more columns to order our result set by
• Two keywords that we will often use with this statement are ASC and DESC
• If we do not specify our preference explicitly, the default sorting order is always ASC.
X
EX
A
NT
AM
SY
PL
Who are all the customers in our
E
contacts ordered by surname?
NB: if we have a column in the
ORDER BY clause, it must be
present in our SELECT clause
• A set function is used in place of columns in SELECT clause. We need to pass a column
SET FUNCTIONS name to a set function.
Function Effect
EX
A
NT
AM
SY
PL
What is the total number of all
E
orders placed by our customers?
EX
A
NT
AM
SY
PL
What is the count of unique first
E
names among our customers
contacts?
• Breaks result set into subsets, runs set function against each of those subsets: result set
GROUP BY returns 1 row per subset
EX
A
NT
AM
SY What is the count of unique first
PL
E
names among our customers
contacts?
EX
A
NT
AM
SY
PL
What is the count of unique first names
E
among our customers contacts that appear
at least 3 times?
• Our task will be to write a number of queries to analyse sales data and ‘report it
back to our big bosses’
• All queries that we will not finish in class, will need to be completed as part of
homework for week 3.
WRITE THE FOLLOWING QUERIES
• Find ALL sales records (and all columns) that took place in the London store, not in
December, but sales concluded by Bill or Frank for the amount higher than £50
• Find out how many sales took place each week (in no particular order)
• Find out how many sales took place each week AND present data by week in
HOMEWORK descending and then in ascending order
(start exercises in class –
complete the rest at home) • Find out how many sales were recorded each week on different days of the week
• We need to change salesperson's name Inga to Annette
• Find out how many sales did Annette do
• Find the total sales amount by each person by day
• How much (sum) each person sold for the given period
• How much (sum) each person sold for the given period, including the number of
sales per person, their average, lowest and highest sale amounts
• Find the total monetary sales amount achieved by each store
• Find the number of sales by each person if they did less than 3 sales for the past
period
• Find the total amount of sales by month where combined total is less than £100
THANK YOU
HAVE A GREAT
WEEK!
REFERENCE
MATERIALS
• With the help of Logical Operators, Booleans and Conditional Keywords we
can answer more complex questions by mean of writing more complex
QUICK queries
SUMMARY
• ORDER BY sorts result sets