code_junkie

Is there set division in SQL?

Nov 11th, 2011
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.47 KB | None | 0 0
  1. SELECT name FROM sailors
  2. WHERE Sid NOT IN (
  3. -- A sailor is disqualified if by attaching a boat,
  4. -- we obtain a tuple <sailor, boat> that is not in reserves
  5. SELECT s.Sid
  6. FROM sailors s, boats b
  7. WHERE (s.Sid, b.Bid) NOT IN (
  8. SELECT Sid, Bid FROM reserves
  9. )
  10. );
  11.  
  12. -- Alternatively:
  13. SELECT name FROM sailors s
  14. WHERE NOT EXISTS (
  15. -- Not reserved boats
  16. (SELECT bid FROM boats)
  17. EXCEPT
  18. (SELECT r.bid FROM reserves r
  19. WHERE r.sid = s.sid)
  20. );
Add Comment
Please, Sign In to add comment