×

sql · level 3 · unit 1 · lesson 4

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

finds rows in both lists. finds leftovers.

Two lists, two questions. Who is in both? INTERSECT keeps only the rows that appear in both results. Who is in the first list but not the second? EXCEPT keeps those.

The rules match UNION: the same number of columns on each side, repeats removed, one ORDER BY at the end. Two rows count as the same only when every column matches.

Order matters for EXCEPT. Chess EXCEPT choir means chess pupils who skip choir. Swap the sides and you get choir pupils who skip chess.

SELECT pupil FROM chess
INTERSECT
SELECT pupil FROM choir
ORDER BY pupil;

SELECT pupil FROM chess
EXCEPT
SELECT pupil FROM choir
ORDER BY pupil;

Several of these can chain, and they run top to bottom, one after another.