skip to content

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.

query =
the list: SELECT instructor_id FROM courses
1CS1011CS3012CS2012CS3503CS4014MATH1014MATH3015MATH2016PHYS1016PHYS2017HIST101nullCS499
Sofia Petrova, id 8:
8 <> 1trueAND8 <> 2trueAND8 <> 3trueAND8 <> 4trueAND8 <> 5trueAND8 <> 6trueAND8 <> 7trueAND8 <> NULLunknown
so NOT IN isunknowndropped

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.

real output, postgres 18.6 (PGlite 0.5.12) · not-in
pick a form of the query and an instructor. the list comes from 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

predict the output
sql
SELECT name
FROM instructors
WHERE id NOT IN (SELECT instructor_id FROM courses);

for real. first the trap, then two ways out:

sqldb=school
SELECT name FROM instructors
WHERE id NOT IN (SELECT instructor_id FROM courses)
ORDER BY id;
output, from postgres 18.6
SELECT 0
nametext
(0 rows)
sqldb=school
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;
output, from postgres 18.6
#1 · SELECT 1
nametext
Sofia Petrova
(1 row)
#2 · SELECT 1
nametext
Sofia Petrova
(1 row)

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:

sqlempty db
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)";
output, from postgres 18.6
SELECT 1
8 NOT IN (1, 2, NULL)bool1 NOT IN (1, 2, NULL)bool8 IN (1, 2, NULL)bool1 IN (1, 2, NULL)bool
nullfnullt
(1 row)

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:

ask with =
query.sql
  1. 1SELECT count(*) AS manage_nobody
  2. 2FROM employees AS e
  3. 3WHERE e.id NOT IN (SELECT manager_id FROM employees);
the result for NOT IN
manage_nobodyint8
0
(1 row)

NOT IN: the CEO's NULL manager_id is in the list, so every id gets unknown. 0 rows.

real output, postgres 18.6 (PGlite 0.5.12) · not-in-org
switch the form. NOT IN finds nobody. the three correct forms all find 200 people who manage no one: the bottom level of the tree.

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:

show =
actual rows
the two plans for the instructors query. NOT IN stays a filter on the instructors scan, testing each id against a hashed list from the subquery. NOT EXISTS becomes a Hash Anti Join.

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) is x <> 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.