Lec07 Sqlwrapup
Lec07 Sqlwrapup
1
Recap from last lecture
• Subqueries can occur in many clauses:
– SELECT
– FROM
– WHERE
• Non-monotone queries
– Universal quantifier
– Aggregation
2
Examples of Complex Queries
Likes(drinker, beer)
Frequents(drinker, bar)
Serves(bar, beer)
1. Find drinkers that frequent some bar that serves some beer they like.
2. Find drinkers that frequent some bar that serves only beers they don’t like.
3. Find drinkers that frequent only bars that serves some beer they like.
Likes(drinker, beer)
Frequents(drinker, bar)
Serves(bar, beer) Example 1
Find drinkers that frequent some bar that serves some beer they like.
5
Likes(drinker, beer)
Frequents(drinker, bar)
Serves(bar, beer) Example 2
Find drinkers that frequent some bar that serves only beers they don’t like
Existential Universal
6
Likes(drinker, beer)
Frequents(drinker, bar)
Serves(bar, beer) Example 2
Find drinkers that frequent some bar that serves only beers they don’t like
7
Likes(drinker, beer)
Frequents(drinker, bar)
Serves(bar, beer) Example 2
Find drinkers that frequent some bar that serves only beers they don’t like
Now negate!
SELECT DISTINCT X.drinker
FROM Frequents X
WHERE NOT EXISTS (SELECT *
FROM Serves Y, Likes Z
WHERE X.bar=Y.bar AND
X.drinker=Z.drinker AND
Y.beer = Z.beer) 11
Likes(drinker, beer)
Frequents(drinker, bar)
Serves(bar, beer) Example 3
Find drinkers that frequent only bars that serves some beer they like.
Universal Existential
11
Likes(drinker, beer)
Frequents(drinker, bar)
Serves(bar, beer) Example 3
Find drinkers that frequent only bars that serves some beer they like.
12
Likes(drinker, beer)
Frequents(drinker, bar)
Serves(bar, beer) Example 3
Find drinkers that frequent only bars that serves some beer they like.
13
Likes(drinker, beer)
Frequents(drinker, bar)
Serves(bar, beer) Example 3
Find drinkers that frequent only bars that serves some beer they like.
Unnesting Aggregates
17
Product (pname, price, cid)
Company(cid, cname, city)
Unnesting Aggregates
18
Product (pname, price, cid)
Company(cid, cname, city)
Unnesting Aggregates
19
Purchase(pid, product, quantity, price)
SELECT DISTINCTAuthor.name
FROM Author
WHERE 10 <= (SELECT count(url)
FROM Wrote
WHERE Author.login=Wrote.login)
21
Author(login, name)
Wrote(login, url)
More Unnesting
SELECT name
FROM Author, Wrote This is
WHERE Author.login=Wrote.login SQL by
GROUP BY name an expert
HAVING count(url) >= 10
22
Product (pname, price, cid)
Company(cid, cname, city)
Finding Witnesses
For each city, find the most expensive product made in that city
Finding the maximum price is easy…
But we need the witnesses, i.e. the products with max price
23
Product (pname, price, cid)
Company(cid, cname, city)
Finding Witnesses
For each city, find the most expensive product made in that city
To find the witnesses:
compute the maximum price in a subquery
SELECT DISTINCT u.city, v.pname, v.price
FROM Company u, Product v,
Not a bad
(SELECT x.city, max(y.price) as maxprice solution…
FROM Company x, Product y
WHERE x.cid = y.cid
GROUP BY x.city) w
WHERE u.cid = v.cid
and u.city = w.city
and v.price=w.maxprice;
24
Product (pname, price, cid)
Company(cid, cname, city)
Finding Witnesses
For each city, find the most expensive product made in that city
25
Product (pname, price, cid)
Company(cid, cname, city)
Finding Witnesses
For each city, find the most expensive product made in that city
There is a more concise solution here:
26