the NOT IN trap
one NULL in the list and NOT IN returns nothing at all. NOT EXISTS doesn't care.
- address
- everything.joins.not_in_trap
- part
- part 3, joins and subqueries
- chapter
- chapter 8 of 11
- reading time
- 7 min
x NOT IN (subquery) means "x is different from every value the subquery returns". if one of those values is NULL, then x <> NULL is unknown for every x, so no row can ever pass, and the query returns nothing at all. no error, no warning.
NOT EXISTS asks a different question and has no such hole. use it.
the picture
you know how you ask for the instructors who teach nothing, get zero rows back, and conclude everyone's busy? Sofia Petrova teaches nothing. the query just told you she doesn't exist. and the reason is a course: CS499, the senior thesis, has no instructor yet, so its instructor_id is NULL.
think of a bouncer at a door that only lets in people who are not on a list. one line of the list is smudged, unreadable. you walk up, and the bouncer can't swear you aren't the smudged name, so you don't get in. neither does anyone else. one smudge, and the door is shut for everybody.
0 rows. Sofia Petrova is unknown: every term but the last is true, and true AND unknown is unknown. the NULL from CS499 makes the last term unknown for every instructor, so nobody can come out true, and WHERE keeps only true.
SELECT instructor_id FROM courses, and CS499 puts a null in it. NOT IN turns into one <> per value, ANDed together, and the last one is unknown for everybody. a row is kept only when the whole thing is true.the proof, both ways
SELECT name
FROM instructors
WHERE id NOT IN (SELECT instructor_id FROM courses);for real. first the trap, then two ways out:
SELECT name FROM instructors
WHERE id NOT IN (SELECT instructor_id FROM courses)
ORDER BY id;| nametext |
|---|
SELECT name FROM instructors
WHERE id NOT IN (SELECT instructor_id FROM courses WHERE instructor_id IS NOT NULL)
ORDER BY id;
SELECT name FROM instructors AS i
WHERE NOT EXISTS (SELECT 1 FROM courses AS c WHERE c.instructor_id = i.id)
ORDER BY id;| nametext |
|---|
| Sofia Petrova |
| nametext |
|---|
| Sofia Petrova |
same tables, same question, and the only difference between the first query and the second is one NULL in the list.
why one NULL is enough
x IN (a, b, c) is shorthand for x = a OR x = b OR x = c. put a NOT in front and De Morgan flips it: x <> a AND x <> b AND x <> c. an AND is true only if every part is true, and x <> NULL is never true, it's unknown (NULL and three-valued logic has the whole table). so with a NULL in the list, the best NOT IN can ever do is unknown:
SELECT 8 NOT IN (1, 2, NULL) AS "8 NOT IN (1, 2, NULL)",
1 NOT IN (1, 2, NULL) AS "1 NOT IN (1, 2, NULL)",
8 IN (1, 2, NULL) AS "8 IN (1, 2, NULL)",
1 IN (1, 2, NULL) AS "1 IN (1, 2, NULL)";| 8 NOT IN (1, 2, NULL)bool | 1 NOT IN (1, 2, NULL)bool | 8 IN (1, 2, NULL)bool | 1 IN (1, 2, NULL)bool |
|---|---|---|---|
| null | f | null | t |
two NULLs (unknown) and a false: NOT IN can still say false (1 is in the list), but it can never say true. plain IN has the same unknown in it, but there it's harmless: a value that's in the list is true, a value that isn't is unknown, and WHERE drops unknown exactly like it drops false. only the NOT turns "unknown" from "no" into "nobody".
NOT EXISTS doesn't care
NOT EXISTS (SELECT 1 FROM courses AS c WHERE c.instructor_id = i.id) asks: is there a course row for which c.instructor_id = i.id is true? CS499's row gives unknown for every instructor, and unknown isn't true, so CS499 just isn't a match for anyone. it can't poison the answer, it can only fail to match. the question is about rows, not about a list of values, and a NULL can only ever be a row that doesn't match.
the bigger one
the school's trap costs one instructor. here's one that costs two thirds of a company. in the org dataset, who manages nobody? the CEO's manager_id is NULL, so it's in the list:
- 1SELECT count(*) AS manage_nobody
- 2FROM employees AS e
- 3WHERE e.id NOT IN (SELECT manager_id FROM employees);
| manage_nobodyint8 |
|---|
| 0 |
NOT IN: the CEO's NULL manager_id is in the list, so every id gets unknown. 0 rows.
0 against 200, and the 0 looks perfectly plausible if you don't already know the answer. that's the dangerous part: the trap doesn't look broken, it looks like a fact.
and the planner can't save you
NOT EXISTS gets rewritten into an anti join. NOT IN, here, doesn't:
an anti join keeps a row when no match is found. NOT IN keeps a row only when every comparison is false, and with NULLs around those aren't the same thing, so postgres can't swap one for the other. it builds the whole subquery result into a hash table and checks every outer row against it: (hashed SubPlan 1) in the filter. same answer to the same rules, just with the NULL semantics kept, which in this case means the wrong answer, faithfully. how subqueries get planned goes into when the rewrite is legal.
keylines
x NOT IN (list)isx <> a AND x <> b AND .... one NULL makes it never true.- the result is zero rows, with no error, and it looks like a real answer.
- IN is safe. it's the NOT that turns unknown into "nobody".
- NOT EXISTS tests for a matching row, so a NULL just fails to match.
- prefer NOT EXISTS: right whatever the column allows, and an anti join in the plan.