Sub topic 4
NOT IN and NULL: a Common Trap
Prerequisites
This chapter assumes that you can write basic SELECT queries with WHERE, use subqueries with IN, and understand that NULL means “unknown” or “missing,” not zero or an empty string. It also assumes that you know SQL uses three-valued logic: a condition can be true, false, or unknown.
NOT IN looks like the natural way to exclude rows returned by a subquery. However, a single NULL in that subquery can make the condition unknown for every outer row.
How `NOT IN` normally works
Suppose these tables contain the following data:
| customers.id | customers.name | orders.customer_id |
|---|---|---|
| 1 | Ana | 1 |
| 2 | Ben | 3 |
| 3 | Chen | NULL |
To find customers who have not placed an order, you might write:
SELECT c.name
FROM customers AS c
WHERE c.id NOT IN (
SELECT o.customer_id
FROM orders AS o
);The subquery returns the set (1, 3, NULL). For customer Ana, SQL evaluates:
1 NOT IN (1, 3, NULL)This is equivalent to comparing 1 with every value:
1 <> 1 AND 1 <> 3 AND 1 <> NULLThe first comparison is false. The second is true. The comparison with NULL is unknown. SQL combines these with AND, so the complete condition is unknown rather than true.
A WHERE clause keeps only rows for which the condition is true. It discards both false and unknown results. Therefore, the query can return no rows, even though Ben has no matching order.
Why `NULL` changes the result
NULL does not represent a known value. SQL cannot prove that a value is different from an unknown value.
For example:
SELECT
2 NOT IN (1, 3) AS without_null,
2 NOT IN (1, 3, NULL) AS with_null;The first expression is true. The second is unknown. The exact display of an unknown result depends on the database, but it is commonly shown as NULL.
The same problem affects NOT IN when the outer value is NULL. A comparison such as NULL NOT IN (1, 3) is also unknown.
Prefer `NOT EXISTS` for absence tests
NOT EXISTS asks a different question: “Is there no related row that matches this customer?” It does not compare the customer id with every value in a list, so unrelated NULL values do not cause the same problem.
SELECT c.name
FROM customers AS c
WHERE NOT EXISTS (
SELECT 1
FROM orders AS o
WHERE o.customer_id = c.id
);For each customer, the inner query searches for an order whose customer_id equals that customer’s id. NOT EXISTS is true when the search finds no matching row. The NULL order does not match any customer because NULL = c.id is unknown, but it also does not invalidate the result.
You can make NOT IN safe by excluding nulls inside the subquery:
SELECT c.name
FROM customers AS c
WHERE c.id NOT IN (
SELECT o.customer_id
FROM orders AS o
WHERE o.customer_id IS NOT NULL
);This works when the outer c.id is guaranteed non-null. For general absence checks, NOT EXISTS usually states the intent more clearly.
Choosing the safer form
| Question | Usually use |
|---|---|
| Does this value belong to a known list? | `IN` |
| Does no related row exist? | `NOT EXISTS` |
| Is the subquery guaranteed to contain no `NULL`? | `NOT IN` can be suitable |
| Could either side contain `NULL`? | Prefer `NOT EXISTS` or explicit null handling |
Exercise
Rewrite this query so it returns customers with no orders even when orders.customerid can contain NULL: SELECT name FROM customers WHERE id NOT IN (SELECT customerid FROM orders);
Show answer
SELECT c.name FROM customers AS c WHERE NOT EXISTS (SELECT 1 FROM orders AS o WHERE o.customer_id = c.id);