Sub topic 8
The `NOT IN` Trap When `NULL` Values Are Present
This chapter assumes you know SELECT, WHERE, INNER JOIN, LEFT JOIN, table aliases, aggregate functions, and that NULL means a missing or unknown value.
IN tests whether a value matches at least one value returned by a subquery. NOT IN is intended to test whether it matches none of them:
SELECT c.customer_id, c.name
FROM customers c
WHERE c.customer_id NOT IN (
SELECT o.customer_id
FROM orders o
);This looks like a query for customers who have no orders. It is safe only if orders.customer_id cannot contain NULL.
Why `NULL` changes the result
SQL uses three-valued logic. A condition can be TRUE, FALSE, or UNKNOWN. The WHERE clause keeps only rows for which the condition is TRUE.
Suppose the subquery returns these values:
| `orders.customer_id` |
|---|
| 1 |
| `NULL` |
For customer 2, SQL evaluates the condition as if it were:
2 NOT IN (1, NULL)This is equivalent in meaning to:
NOT (2 = 1 OR 2 = NULL)The first comparison is FALSE. The second comparison is UNKNOWN, because SQL cannot determine whether 2 equals an unknown value. Therefore:
FALSE OR UNKNOWN = UNKNOWN
NOT UNKNOWN = UNKNOWNBecause UNKNOWN is not TRUE, customer 2 is removed by the WHERE clause. The same happens to every customer that does not match a known order customer. As a result, one NULL in the subquery can make NOT IN return no rows.
A NULL on the left side is also not a match:
WHERE c.customer_id NOT IN (...)If c.customer_id is NULL, the comparison is UNKNOWN, so that customer is not returned.
Prefer `NOT EXISTS` for absence checks
NOT EXISTS asks a different question: does the correlated subquery find any matching row? It does not compare against a list containing NULL.
SELECT c.customer_id, c.name
FROM customers c
WHERE NOT EXISTS (
SELECT 1
FROM orders o
WHERE o.customer_id = c.customer_id
);For each customer, the inner query searches for an order whose customer_id equals that customer's ID. If no such row exists, NOT EXISTS is TRUE.
If the schema guarantees that the subquery column is non-null, NOT IN can work:
SELECT c.customer_id, c.name
FROM customers c
WHERE c.customer_id NOT IN (
SELECT o.customer_id
FROM orders o
WHERE o.customer_id IS NOT NULL
);Adding IS NOT NULL removes the immediate trap. However, NOT EXISTS usually expresses the relationship more directly and remains safe if the query changes later.
`IN`, `EXISTS`, and `JOIN`
Use IN when you are testing membership in a known, non-null set. Use EXISTS when the question is whether a related row exists. Use a JOIN when you need columns from both tables.
A join can produce duplicates because one customer can have many orders:
SELECT DISTINCT c.customer_id, c.name
FROM customers c
JOIN orders o ON o.customer_id = c.customer_id;The DISTINCT is needed if the intended result contains each customer once. EXISTS naturally returns each outer customer at most once.
Exercise
Why can this query return no rows even when some customers have no orders? SELECT c.customerid FROM customers c WHERE c.customerid NOT IN ( SELECT o.customer_id FROM orders o );
Show answer
If the subquery returns even one NULL, a nonmatching comparison becomes UNKNOWN. WHERE keeps only TRUE, so those customers are rejected. Replace the condition with NOT EXISTS, or filter NULL values inside the subquery.