Use NOT Exists for Division Find sailors who’ve reserved all boats

advertisement
Use NOT Exists for Division
Recall: X/Y means only give me X tuples that have a match in Y.
Find sailors who’ve reserved all boats.
X = set of sailors and Y = set of all boats with reservations.
SELECT S.sname
Find Sailors S such that ...
FROM Sailors S
WHERE NOT EXISTS
there is no boat B...
(SELECT B.bid
FROM Boats B
WHERE NOT EXISTS
(SELECT R.bid
without a reservation by
FROM Reserves R
Sailor S
WHERE R.bid=B.bid
AND R.sid=S.sid))
Division
SELECT S.sname
FROM Sailors S
WHERE NOT EXISTS
(SELECT B.bid
FROM Boats B
WHERE NOT EXISTS
(SELECT R.bid
FROM Reserves R
101
103
WHERE R.bid=B.bid
1
3
AND R.sid=S.sid))
2
Reserves
Sailors
sid
sid
bid
day
R
1
103
9/12
R
R
R
R
2
103
9/13
3
103
9/14
3
101
9/12
1
103
9/13
Boats
sname
rating
age
S 1
Frodo
7
22
S 2
Bilbo
2
39
S 3
Sam
8
27
bid
bname
color
B
101
Nina
red
B
103
Pinta
blue
Download