❖ Sets in SQL |
The relational model is set-based
Set literals are written as (
,
,
)
SQL query results are (more or less) sets of tuples or atomic values
Examples:
-- set literals (1,2,3) ('a','b','c','d') -- set of atomic values (select salary from Employees) -- set of tuple values (select id, name from Employees)
SQL provides a variety of set-based operators: ∈, ∪, ∩, -, /, ∃, ∀, ...
❖ Bags in SQL |
SQL query results are actually bags (multisets), allowing duplicates, e.g.
select age from Students;
-- yields (18,18,18,...19,19,19,19,...20,20,20,...)
Can convert bag to set (eliminate duplicates) using DISTINCT
select distinct age from Students;
SQL set operations UNION
INTERSECT
EXCEPT
ALL
UNION ALL
(1,2,3) UNION (2,3,4) yields (1,2,3,4) (1,2,3) UNION ALL (2,3,4) yields (1,2,3,2,3,4)
❖ The IN |
Tests whether a specified tuple is contained in a relation (i.e. t∈R)
tuple IN
Conversely for tuple NOT IN
Syntax:
SELECT *
FROM R
WHERE R.a IN (SELECT x FROM S WHERE Cond)
-- assume multiple results
❖ The IN |
Example: Find the name and brewer of beers that John likes.
SELECT name, brewer FROM Beers WHERE name IN (SELECT beer FROM Likes WHERE drinker = 'John'); name | brewer ---------------------+--------------- 80/- | Caledonian Bigfoot Barley Wine | Sierra Nevada Pale Ale | Sierra Nevada Three Sheets | Lord Nelson
Subquery = "What are the names of the beers that John likes?"
(This and subsequent beer queries use an older smaller version of the Beer database)
❖ The EXISTS |
EXISTS(
)
Example: Find the beers that are the unique beer by their manufacturer.
SELECT name, brewer FROM Beers b1 WHERE NOT EXISTS (SELECT * FROM Beers b2 WHERE b2.brewer = b1.brewer AND b2.name <> b1.name);
A subquery that refers to values from a surrounding query is called a correlated subquery.
❖ Quantifiers |
ANY
ALL
Example: Find the beers sold for the highest price.
SELECT beer FROM Sells WHERE price >= ALL(SELECT price FROM sells);
Beware: in common use, "any" and "all" are often synonyms.
E.g. "I'm better than any of you" vs. "I'm better than all of you".
❖ Union, Intersection, Difference |
SQL implements the standard set operations
UNION |
set of tuples in either R1 or R2 | |
INTERSECT |
set of tuples in both R1 and R2 | EXCEPT |
set of tuples in R1 but not R2 |
R1
R2
Union and intersection semantics are straightforward.
❖ Union, Intersection, Difference (cont) |
Example: Find the drinkers and beers such that the drinker likes the beer and frequents a bar that sells it.
(SELECT drinker, beer FROM Likes) INTERSECT (SELECT drinker,beer FROM Sells natural join Frequents); drinker | beer ---------+----------------- Adam | New John | Three Sheets Justin | Victoria Bitter
❖ Union, Intersection, Difference (cont) |
Set difference is implemented by EXCEPT
Semantics of set difference: R except S = { x ∈ R, where x ∉ S }
❖ Division |
Division aims to find values in one table that occur in conjunction with all values in another table:
Arises in queries like "Find Xs that are related to all Ys / every Y"
❖ Division (cont) |
Not all SQL implementations provide a division operator
But can be achieved by combination of existing operations
Example: Find bars that each sell all of the beers Justin likes.
SELECT DISTINCT a.bar FROM Sells a WHERE NOT EXISTS ( (SELECT beer FROM Likes WHERE drinker = 'Justin') EXCEPT (SELECT beer FROM Sells b WHERE bar = a.bar) );
❖ Selection with Aggregation |
Selection clauses can contain aggregation operations.
Example: What is the average price of New?
SELECT AVG(price) FROM Sells WHERE beer = 'New'; avg ------------------ 2.38749998807907
❖ Selection with Aggregation (cont) |
If we want set semantics, can force using DISTINCT
Example: How many different bars sell beer?
SELECT COUNT(DISTINCT bar) FROM Sells; count ------- 6
Without DISTINCT
Sells
Aggregation operators on numbers: SUM
AVG
MIN
MAX
Produced: 4 Oct 2020