Why can NOT IN return zero rows?
If the subquery in NOT IN returns even one NULL, the query returns zero rows without an error. A comparison with NULL gives UNKNOWN, so NOT IN, which expands into a chain of "not equal" conditions joined by AND, never becomes true, and WHERE keeps only true rows. Safe alternatives are NOT EXISTS or a LEFT JOIN with an IS NULL check on the right table's key.
SQL · Subqueries, CTE, sets
The query "users without accounts" returned two yesterday and nobody today. No data disappeared. What changed?
A report "users with no account" returned two people, and after a new data load it became empty. The users went nowhere: the accounts table got one row with no user_id. The query with NOT IN raised no error, it just returned zero rows. This is one of the quietest bugs in SQL and it is often asked in interviews.
How it works and why. SQL logic has three values: a condition can be true, false or unknown (UNKNOWN). A comparison with NULL always gives UNKNOWN. WHERE keeps only rows where the condition is true, so UNKNOWN is dropped just like false. x NOT IN (a, b, c) means x <> a AND x <> b AND x <> c. If one of the values is NULL, the matching factor is UNKNOWN. An AND with UNKNOWN is either false or unknown, never true. So for any x that is not in the list the answer is UNKNOWN and the row is dropped.
The sample data: users 1 anna, 2 bohdan, 3 clara, 4 dmytro; accounts 10 and 11 belong to user 1, 12 to user 2, and account 13 has user_id = NULL.
-- accounts.user_id contains NULL (account 13): the result is 0 rows
SELECT id, email
FROM users
WHERE id NOT IN (SELECT user_id FROM accounts);
-- for user 3 (clara) NOT IN expands like this
3 <> 1 AND 3 <> 1 AND 3 <> 2 AND 3 <> NULL
-- TRUE AND TRUE AND TRUE AND UNKNOWN => UNKNOWN => row dropped
The input data and the correct answer
| id | user_id |
|---|---|
| 10 | 1 |
| 11 | 1 |
| 12 | 2 |
| 13 | NULL |
| id | |
|---|---|
| 3 | clara |
| 4 | dmytro |
Note that even for a user who definitely has no accounts the condition does not become true. For user 1, who is in the list, the condition is false, and that surprises nobody. What surprises is that the NULL "poisons" those who are not in the list too.
Example: three ways to fix it.
-- 1) NOT EXISTS: clara and dmytro
SELECT u.id, u.email
FROM users u
WHERE NOT EXISTS (SELECT 1 FROM accounts a WHERE a.user_id = u.id);
-- 2) LEFT JOIN + IS NULL: the same result
SELECT u.id, u.email
FROM users u
LEFT JOIN accounts a ON a.user_id = u.id
WHERE a.id IS NULL;
-- 3) NOT IN, but with NULL filtered out
SELECT id, email
FROM users
WHERE id NOT IN (SELECT user_id FROM accounts WHERE user_id IS NOT NULL);
NOT EXISTS compares through a.user_id = u.id. For the account with NULL the comparison is UNKNOWN, so that row simply matches nobody and does not touch the other users. The LEFT JOIN with IS NULL is safe too. The third variant treats the symptom: it works, but it is easy to forget the filter in another query. A separate finding for the tester: an account without an owner. If user_id is mandatory by requirement it is a defect, and the answer to it is a NOT NULL constraint.
The same search for missing records written two ways
NOT IN (SELECT user_id …)
One NULL in the list and the result is empty. There is no error, so the bug is easy to miss.
NOT EXISTS (…)
A comparison with NULL matches nobody and does not touch other rows. The result is stable.
Limits and alternatives. If NULL is impossible in the subquery (a NOT NULL column or an explicit list of literals without NULL), NOT IN works correctly. But the guarantee lasts only until the schema changes: someone adds a column that allows NULL and the query silently breaks. So NOT EXISTS is safer by default. One more detail: if NULL is on the left (NULL NOT IN (1, 2)) the result is UNKNOWN too. A test for this: add one row with NULL in the link column to the test data and see whether the query turned empty.
Typical mistakes.
NOT INover a subquery on a column that allows NULL.- An empty result is read as "all fine" although the data did not change: an empty result must be checked for its cause too.
WHERE user_id <> NULLinstead ofIS NOT NULL: the condition is never true.- A
LEFT JOINwhere the absence check is written not on the key of the right table but on an ordinary column that can itself be NULL.
Check yourself
What does SELECT 5 WHERE 5 NOT IN (1, 2, NULL) return? And SELECT 5 WHERE 5 NOT IN (1, 2)?
Answer
The first returns zero rows: 5 <> NULL is unknown, so the whole condition is UNKNOWN. The second returns a row with the value 5 because both comparisons are true.
What does the NOT EXISTS query return on the sample data and why does the NULL in accounts.user_id not hurt it?
Answer
Two users, 3 and 4. For each user the subquery looks for an account with a.user_id = u.id. For the account with NULL the comparison is unknown, so that row matches nobody and does not affect the result.