





























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






























CS 186, Spring 2007, Lecture 8 R&G, Chapter 5 Mary Roth
The important thing is not to stop questioning. Albert Einstein Life is just a bowl of queries. -Anon (not Forrest Gump)
SELECT PROJECT JOIN
SELECT R1.sid FROM Boats B1, Reserves R1, Boats B2, Reserves R WHERE B1.color = ‘red’ AND B1.bid=R1.bid AND B2.color = ‘green’ AND B2.bid=R2.bid AND R1.sid=R2.sid Find sids of sailors who’ve reserved a red and a green boat 2 103 9/ 1 105 9/ 1 101 9/ sid bid day 103 Santa Maria red 105 Titanic green 102 Pinta blue 101 Nina red bid bname color Find red reserved boats Find green reserved boats Find matching green and red reserved boats 2 1 sid 1 sid = (^1) sid
SELECT S.sname FROM Sailors S WHERE S.sid IN (SELECT R.sid FROM Reserves R WHERE R.bid=103) e.g. Find the names of sailors who’ve reserved boat #103: …and then check each the sid of each tuple in Sailors to see if it is in S First compute the set of all sailors that have reserved boat 103… 2 103 9/ 1 105 9/ 1 101 9/ sid bid day 3 Sam 8 27 2 Bilbo 2 39 1 Frodo 7 22 sid sname rating age Sailors Reserves S 2 sid S S Bilbo sname
enclosing query SELECT S.sname FROM Sailors S WHERE EXISTS (SELECT * FROM Reserves R WHERE R.bid=103 AND R.sid=S.sid) EXISTS is a set operator that is true if result of set expression has at least one tuple
e.g. Find the names of sailors who’ve reserved boat #103: 2 103 9/ 1 105 9/ 1 101 9/ sid bid day 3 Sam 8 27 2 Bilbo 2 39 1 Frodo 7 22 sid sname rating age Sailors Reserves S S S SELECT S.sname FROM Sailors S WHERE EXISTS (SELECT * FROM Reserves R WHERE R.bid=103 AND R.sid=S.sid) sid bid day Notice that this query computes the same answer as the previous query! 321 2 103 9/
SELECT S.sname FROM Sailors S WHERE NOT EXISTS (SELECT B.bid FROM Boats B WHERE NOT EXISTS (SELECT R.bid FROM Reserves R WHERE R.bid=B.bid AND R.sid=S.sid)) Find Sailors S such that ... there is no boat B... without a reservation by Sailor S
X = set of sailors and Y = set of all boats with reservations. Recall: X/Y means only give me X tuples that have a match in Y.
2
3 103 9/ 3 101 9/ 2 103 9/ 1 103 9/ 1 103 9/ sid bid day 3 Sam 8 27 2 Bilbo 2 39 1 Frodo 7 22 sid sname rating age Sailors Reserves S S S SELECT S.sname FROM Sailors S WHERE NOT EXISTS (SELECT B.bid FROM Boats B WHERE NOT EXISTS (SELECT R.bid FROM Reserves R WHERE R.bid=B.bid AND R.sid=S.sid)) 103 Pinta blue 101 Nina red bid bname color Boats 1 101 R R R B B 3 103 R R
FROM Sailors S WHERE S.rating > ANY(SELECT S2.rating FROM Sailors S WHERE S2.sname=‘Bilbo’) Find sailors whose rating is greater than that of some sailor called Bilbo: 3 Sam 8 27 2 Bilbo 2 39 1 Frodo 7 22 sid sname rating age S 3 Sam 8 27 2 Bilbo 2 39 1 Frodo 7 22 sid sname rating age S 2 Bilbo 2 39 sid sname rating age Correlated or uncorrelated? Uncorrelated!
SELECT S.sname, MAX (S.age) FROM Sailors S
2 Bilbo 2 39 1 Frodo 7 22 sid sname rating age Sailors Sam 39 Bilbo 39 Frodo 39 sname age X Not legal syntax; no other columns allowed in SELECT clause without a GROUP BY clause (we’ll learn about those next)
SELECT S.sname, S.age FROM Sailors S WHERE S.age = (SELECT MAX (S2.age) FROM Sailors S2)
Find the maximum age… And then find the sailors(s) of that age…
Queries With GROUP BY
SELECT S.rating, MIN (S.age) FROM Sailors S WHERE S.age >= 18 GROUP BY S.rating 3 Sam 8 27 4 Pippin 2 21 5 Merry 8 17 2 Bilbo 2 39 1 Frodo 7 22 sid sname rating age Sailors Group 1 1 Frodo 7 22 4 Pippin 2 21 2 Bilbo 2 39 Group 2 Group 3 3 Sam 8 27 2 21 8 27 7 22