"Find the names of sailors who have reserved all boats."
SQL code:
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 ))
Could anyone please explain/translate the above sql code? Why is there NOT EXISTS
but not IN
. And why is value checking happens in the innermost subquery (WHERE R.bid = B.bid AND R.sid = S.sid)
?