SQL Subqueries
SQL Subqueries
Peter Wood
SQL queries
SQL Subqueries
Aggregation Queries
Peter Wood
Find all locations where a drinker lives or a pub is located:
SQL queries
SQL Subqueries
Aggregation Queries
Database
Union of two tables Management
Peter Wood
Find all locations where a drinker lives or a pub is located:
SQL queries
SQL Subqueries
name loc name loc Aggregation Queries
Peter Wood
Find all locations where a drinker lives or a pub is located:
SQL queries
SQL Subqueries
name loc name loc Aggregation Queries
Peter Wood
Find all locations where a drinker lives or a pub is located:
SQL queries
SQL Subqueries
name loc name loc Aggregation Queries
loc
Bloomsbury
Islington
Stratford
Database
Intersection of two tables Management
Peter Wood
SQL queries
SQL Subqueries
Find locations where both a drinker lives and a pub is Aggregation Queries
located:
Database
Intersection of two tables Management
Peter Wood
SQL queries
SQL Subqueries
Find locations where both a drinker lives and a pub is Aggregation Queries
located:
Peter Wood
SQL queries
SQL Subqueries
Find locations where both a drinker lives and a pub is Aggregation Queries
located:
loc
Bloomsbury
Islington
Database
Intersection of two tables Management
Peter Wood
SQL queries
SQL Subqueries
Find locations where both a drinker lives and a pub is Aggregation Queries
located:
loc
Bloomsbury
Islington
Peter Wood
SQL queries
SQL Subqueries
Aggregation Queries
Peter Wood
SQL queries
SQL Subqueries
Aggregation Queries
Peter Wood
SQL queries
SQL Subqueries
Aggregation Queries
loc
Stratford
Database
Difference of two tables Management
Peter Wood
SQL queries
SQL Subqueries
Aggregation Queries
loc
Stratford
Peter Wood
SQL queries
SQL Subqueries
Aggregation Queries
Peter Wood
SQL queries
SQL Subqueries
Aggregation Queries
Peter Wood
There are a number of SQL operators that apply to a
relation (or expression) R and return a Boolean result: SQL queries
SQL Subqueries
Aggregation Queries
Database
Conditions involving relations Management
Peter Wood
There are a number of SQL operators that apply to a
relation (or expression) R and return a Boolean result: SQL queries
SQL Subqueries
Aggregation Queries
I EXISTS R is true if and only if R is not empty
Database
Conditions involving relations Management
Peter Wood
There are a number of SQL operators that apply to a
relation (or expression) R and return a Boolean result: SQL queries
SQL Subqueries
Aggregation Queries
I EXISTS R is true if and only if R is not empty
I s IN R is true if and only if s is equal to one of the
values in R (where, e.g., s is an attribute and R is a
unary (one-column) relation/expression)
Database
Conditions involving relations Management
Peter Wood
There are a number of SQL operators that apply to a
relation (or expression) R and return a Boolean result: SQL queries
SQL Subqueries
Aggregation Queries
I EXISTS R is true if and only if R is not empty
I s IN R is true if and only if s is equal to one of the
values in R (where, e.g., s is an attribute and R is a
unary (one-column) relation/expression)
I s > ALL R is true if and only if s is greater than every
value in R
Database
Conditions involving relations Management
Peter Wood
There are a number of SQL operators that apply to a
relation (or expression) R and return a Boolean result: SQL queries
SQL Subqueries
Aggregation Queries
I EXISTS R is true if and only if R is not empty
I s IN R is true if and only if s is equal to one of the
values in R (where, e.g., s is an attribute and R is a
unary (one-column) relation/expression)
I s > ALL R is true if and only if s is greater than every
value in R
I s > ANY R is true if and only if s is greater than at
least one value in R
Database
Conditions involving relations Management
Peter Wood
There are a number of SQL operators that apply to a
relation (or expression) R and return a Boolean result: SQL queries
SQL Subqueries
Aggregation Queries
I EXISTS R is true if and only if R is not empty
I s IN R is true if and only if s is equal to one of the
values in R (where, e.g., s is an attribute and R is a
unary (one-column) relation/expression)
I s > ALL R is true if and only if s is greater than every
value in R
I s > ANY R is true if and only if s is greater than at
least one value in R
I we can use any other comparison operator instead of
> above
Database
Conditions involving relations Management
Peter Wood
There are a number of SQL operators that apply to a
relation (or expression) R and return a Boolean result: SQL queries
SQL Subqueries
Aggregation Queries
I EXISTS R is true if and only if R is not empty
I s IN R is true if and only if s is equal to one of the
values in R (where, e.g., s is an attribute and R is a
unary (one-column) relation/expression)
I s > ALL R is true if and only if s is greater than every
value in R
I s > ANY R is true if and only if s is greater than at
least one value in R
I we can use any other comparison operator instead of
> above
I we can put NOT in front of IN to test if s is equal to
no value in R
Database
Conditions involving relations Management
Peter Wood
There are a number of SQL operators that apply to a
relation (or expression) R and return a Boolean result: SQL queries
SQL Subqueries
Aggregation Queries
I EXISTS R is true if and only if R is not empty
I s IN R is true if and only if s is equal to one of the
values in R (where, e.g., s is an attribute and R is a
unary (one-column) relation/expression)
I s > ALL R is true if and only if s is greater than every
value in R
I s > ANY R is true if and only if s is greater than at
least one value in R
I we can use any other comparison operator instead of
> above
I we can put NOT in front of IN to test if s is equal to
no value in R
I EXISTS, ANY and ALL can be negated by putting
NOT in front of the whole expression
Database
Using ALL Management
Peter Wood
SQL queries
SQL Subqueries
Aggregation Queries
Find the pub, beer and price for the cheapest beer sold
by any pub:
Database
Using ALL Management
Peter Wood
SQL queries
SQL Subqueries
Aggregation Queries
Find the pub, beer and price for the cheapest beer sold
by any pub:
SELECT *
FROM Sells
WHERE price <= ALL
(SELECT price
FROM Sells);
Database
Using ALL Management
Peter Wood
SQL queries
SQL Subqueries
Aggregation Queries
Find the pub, beer and price for the cheapest beer sold
by any pub:
SELECT *
FROM Sells
WHERE price <= ALL
(SELECT price
FROM Sells);
Peter Wood
Peter Wood
SELECT beer
FROM Sells
WHERE pub IN
(SELECT pub
FROM Visits
WHERE drinker = ’Bob’);
Database
Using IN Management
Peter Wood
SELECT beer
FROM Sells
WHERE pub IN
(SELECT pub
FROM Visits
WHERE drinker = ’Bob’);
Previously we had:
SELECT beer
FROM Sells, Visits
WHERE drinker = ’Bob’
AND Sells.pub=Visits.pub;
Database
Using IN for intersection Management
Peter Wood
SQL queries
MySQL does not support the INTERSECT operator SQL Subqueries
Aggregation Queries
Peter Wood
SQL queries
MySQL does not support the INTERSECT operator SQL Subqueries
Aggregation Queries
Peter Wood
SQL queries
MySQL does not support the INTERSECT operator SQL Subqueries
Aggregation Queries
we can use:
Peter Wood
SQL queries
MySQL does not support the EXCEPT operator SQL Subqueries
Aggregation Queries
Peter Wood
SQL queries
MySQL does not support the EXCEPT operator SQL Subqueries
Aggregation Queries
Peter Wood
SQL queries
MySQL does not support the EXCEPT operator SQL Subqueries
Aggregation Queries
we can use:
Peter Wood
SQL queries
SQL Subqueries
Aggregation Queries
Peter Wood
Find the names of drinkers who live where no pub is SQL queries
located: SQL Subqueries
Aggregation Queries
Database
Example of correlated subquery Management
Peter Wood
Find the names of drinkers who live where no pub is SQL queries
located: SQL Subqueries
Aggregation Queries
SELECT name
FROM Drinkers
WHERE NOT EXISTS
(SELECT name
FROM Pubs
WHERE location=Drinkers.location);
Database
Example of correlated subquery Management
Peter Wood
Find the names of drinkers who live where no pub is SQL queries
located: SQL Subqueries
Aggregation Queries
SELECT name
FROM Drinkers
WHERE NOT EXISTS
(SELECT name
FROM Pubs
WHERE location=Drinkers.location);
Peter Wood
Find the names of drinkers who live where no pub is SQL queries
located: SQL Subqueries
Aggregation Queries
SELECT name
FROM Drinkers
WHERE NOT EXISTS
(SELECT name
FROM Pubs
WHERE location=Drinkers.location);
name
Eve
Database
Aggregation Operators Management
Peter Wood
SQL queries
SQL Subqueries
Aggregation Queries
Peter Wood
SQL queries
SQL Subqueries
Find the price of the cheapest beer sold in Bloomsbury: Aggregation Queries
Database
Finding the minimum value Management
Peter Wood
SQL queries
SQL Subqueries
Find the price of the cheapest beer sold in Bloomsbury: Aggregation Queries
Peter Wood
SQL queries
SQL Subqueries
Find the price of the cheapest beer sold in Bloomsbury: Aggregation Queries
minPrice
1.50
Database
Counting tuples Management
Peter Wood
SQL queries
SQL Subqueries
Aggregation Queries
Peter Wood
SQL queries
SQL Subqueries
Aggregation Queries
Peter Wood
SQL queries
SQL Subqueries
Aggregation Queries
numberVisiting
2
Database
Counting tuples Management
Peter Wood
SQL queries
SQL Subqueries
Aggregation Queries
numberVisiting
2
Peter Wood
I Sometimes we don’t want an aggregation applied to
SQL queries
an entire column. SQL Subqueries
Aggregation Queries
I Instead we want to group the tuples of a relation into
groups based on the value of some attribute.
I E.g., we can group Sells tuples according to pub
value.
Database
Grouping Management
Peter Wood
I Sometimes we don’t want an aggregation applied to
SQL queries
an entire column. SQL Subqueries
Aggregation Queries
I Instead we want to group the tuples of a relation into
groups based on the value of some attribute.
I E.g., we can group Sells tuples according to pub
value.
pub beer price
Horse and Hound Bad Habit 1.50
Horse and Hound Rampant Ram 2.00
Hound and Hare Shining Wit 2.75
Hound and Hare Rampant Ram 2.50
March Hare Bad Habit 1.75
March Hare Rampant Ram 2.50
Black Horse Bad Habit 2.50
Black Horse Shining Wit 2.25
Black Horse Rampant Ram 2.50
White Horse Rampant Ram 2.75
Database
Grouping example Management
Peter Wood
SQL queries
Find the average price of the beer sold in each pub: SQL Subqueries
Aggregation Queries
Database
Grouping example Management
Peter Wood
SQL queries
Find the average price of the beer sold in each pub: SQL Subqueries
Aggregation Queries
Peter Wood
SQL queries
Find the average price of the beer sold in each pub: SQL Subqueries
Aggregation Queries
pub avgPrice
Horse and Hound 1.75
Hound and Hare 2.675
March Hare 2.125
Black Horse 2.416
White Horse 2.75
Database
Grouping example Management
Peter Wood
SQL queries
Find the average price of the beer sold in each pub: SQL Subqueries
Aggregation Queries
pub avgPrice
Horse and Hound 1.75
Hound and Hare 2.675
March Hare 2.125
Black Horse 2.416
White Horse 2.75
Peter Wood
SQL queries
Find the average price of the beer sold in each pub that SQL Subqueries
Aggregation Queries
Peter Wood
SQL queries
Find the average price of the beer sold in each pub that SQL Subqueries
Aggregation Queries
Peter Wood
SQL queries
Find the average price of the beer sold in each pub that SQL Subqueries
Aggregation Queries
pub avgPrice
Horse and Hound 1.75
Hound and Hare 2.675
March Hare 2.125
Black Horse 2.416
Database
Grouping, Aggregation and Nulls Management
Peter Wood
SQL queries
SQL Subqueries
Aggregation Queries
Peter Wood
SQL queries
SQL Subqueries
Aggregation Queries
Peter Wood
SQL queries
SQL Subqueries
Aggregation Queries
Peter Wood
Which pubs have names that include the string ’Hare’?:
SQL queries
SQL Subqueries
Aggregation Queries
Database
String Pattern Matching with LIKE Management
Peter Wood
Which pubs have names that include the string ’Hare’?:
SQL queries
SQL Subqueries
SELECT name Aggregation Queries
FROM Pubs
WHERE name LIKE ’%Hare%’
Database
String Pattern Matching with LIKE Management
Peter Wood
Which pubs have names that include the string ’Hare’?:
SQL queries
SQL Subqueries
SELECT name Aggregation Queries
FROM Pubs
WHERE name LIKE ’%Hare%’
name location
Horse and Hound Bloomsbury
Hound and Hare Islington
March Hare Bloomsbury
Black Horse Islington
White Horse Bloomsbury
⇓
Database
String Pattern Matching with LIKE Management
Peter Wood
Which pubs have names that include the string ’Hare’?:
SQL queries
SQL Subqueries
SELECT name Aggregation Queries
FROM Pubs
WHERE name LIKE ’%Hare%’
name location
Horse and Hound Bloomsbury
Hound and Hare Islington
March Hare Bloomsbury
Black Horse Islington
White Horse Bloomsbury
⇓
name
Hound and Hare
March Hare
Database
References Management
Peter Wood
SQL queries
SQL Subqueries
Aggregation Queries
I Chapter 6 of [CB10]
I Chapters 3 and 4 of [SKS11]
I Chapter 6 of [UW08]