



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 R.sid FROM Boats B,Reserves R WHERE B.color = ‘red’ AND R.bid=B.bid INTERSECT SELECT R.sid FROM Boats B,Reserves R WHERE B.color = ‘green’ AND R.bid=B.bid Reserves 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 Boats 2 1 sid 1 sid ∩ (^) = (^1) sid Now let’s do this with a self-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/ sidbid 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/ sidbid day 3 Sam 8 27 2 Bilbo 2 39 1 Frodo 7 22 sidsname rating age Sailors Reserves S 2 sid S S Bilbo sname
SELECT S.sname FROM Sailors S WHERE S.sid IN (SELECT R.sid FROM Reserves R WHERE R.bid=103) It can evaluated once and then checked for each tuple in enclosing query
The subquery must be re-evaluated for each tuple in 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/ sidbid day 3 Sam 8 27 2 Bilbo 2 39 1 Frodo 7 22 sidsname 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) sidbid 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/ sidbid day 3 Sam 8 27 2 Bilbo 2 39 1 Frodo 7 22 sidsname 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
2 103 9/ 1 103 9/ 1 103 9/ sidbid day 3 Sam 8 27 2 Bilbo 2 39 1 Frodo 7 22 sidsname rating age Sailors (^) Reserves S S S sidbid day 2 103 9/ Find the names of sailors who’ve reserved boat # exactly once SELECT S.sname FROM Sailors S WHERE UNIQUE (SELECT sid, bid FROM Reserves R WHERE R.bid=103 AND S.sid=R.sid) (^321)
Queries With GROUP BY target-list contains:
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 sidsname rating age Sailors Group 1 1 Frodo 7 22 4 Pippin 2 21 Group 2^2 Bilbo^239 Group 3 3 Sam 8 27 2 21 8 27 7 22 Find the number of reservations for each red boat. SELECT B.bid, COUNT(*) AS tot_res FROM Boats B, Reserves R WHERE R.bid=B.bid AND B.color=‘red’ GROUP BY B.bid 3 101 9/ 2 103 9/ 4 101 9/ 1 102 9/ sid bid day Reserves 103 Santa Maria red 102 Pinta blue 101 Nina red bid bname color Boats 3 101 9/ 4 101 9/ 2 103 9/ 103 1 101 2 Queries With GROUP BY and HAVING
Answer
3 Sam 8 27 2 Bilbo 2 39 1 Frodo 7 22 sidsnamerating age Sailors 1 102 9/ 2 102 9/ 2 101 9/ 2 103 9/ 1 102 9/ sid bid day Reserves 103 Santa Maria red 102 Pinta blue 101 Nina red bid bname color Boats SELECT S.name FROM Sailors S, reserves R WHERE S.sid = R.sid GROUP BY S.name, S.sid HAVING COUNT(DISTINCT R.bid) = ( Select COUNT (*) FROM Boats) 3 Frodo 1 102^ count Bilbo Bilbo Bilbo Frodo sname 2 101 2 102 2 103 1 102 sid bid Bilbo Frodo sname 2 101, 102, 103 1 102, sid bid Bilbo Frodo sname 2 3 1 1 sid count
More about Joins Explicit join semantics needed unless it is an INNER join (INNER is default) SELECT (column_list) FROM table_name [INNER | {LEFT |RIGHT | FULL } OUTER] JOIN table_name ON qualification_list WHERE …
Only rows that match search conditions are returned. SELECT s.sid, s.name, r.bid FROM Sailors s INNER JOIN Reserves r ON s.sid = r.sid Returns only those sailors who have reserved boats SQL-92 also allows: SELECT s.sid, s.name, r.bid FROM Sailors s NATURAL JOIN Reserves r “NATURAL” means equi-join for each pair of attributes with the same name SELECT s.sid, s.name, r.bid FROM Sailors s INNER JOIN Reserves r ON s.sid = r.sid
Left Outer Join returns all matched rows, plus all unmatched rows from the table on the left of the join clause (use nulls in fields of non-matching tuples) SELECT s.sid, s.name, r.bid FROM Sailors s LEFT OUTER JOIN Reserves r ON s.sid = r.sid Returns all sailors & information on whether they have reserved boats SELECT s.sid, s.name, r.bid FROM Sailors s LEFT OUTER JOIN Reserves r ON s.sid = r.sid s.sid s.name r.bid 22 Dustin 101 95 Bob 103 31 Lubber
Right Outer Join returns all matched rows, plus all unmatched rows from the table on the right of the join clause SELECT r.sid, b.bid, b.name FROM Reserves r RIGHT OUTER JOIN Boats b ON r.bid = b.bid Returns all boats & information on which ones are reserved.
NULL F T AND T F Null (null > 0) (null + 1) (null = 0) null AND true is null is null is null is null NULL F T OR T F Null T F Null T F F F Null F Null F T T T Null Null T Null