The NULL trap of NOT IN → NOT EXISTS
If a NOT IN subquery contains even one NULL, the result becomes 0 rows. This is a correctness problem before it is a performance one.
SQL
Situation
A marketing report extracts customers with no orders in 2025. The result is correct right now. But this query is built so that a single row of data can make the whole result disappear.
Tables
CUSTOMERS (100,000 rows) holds customers, and ORDERS (2 million rows) holds orders.
| Table | Column | Type | Description |
|---|---|---|---|
CUSTOMERS | CUST_ID | NUMBER | Customer ID (PK) |
ORDERS | CUST_ID | NUMBER | Ordering customer ID. No NOT NULL constraint |
ORDERS | ORD_DT | DATE | Order date and time |
| Index | Columns |
|---|---|
CUSTOMERS_PK | CUST_ID |
ORDERS_X1 | CUST_ID, ORD_DT |
Customers with no orders in 2025
The query must return every customer with no orders at all since 2025-01-01.
BEFORE · NOT IN
SELECT *
FROM customers
WHERE cust_id NOT IN (SELECT cust_id
FROM orders
WHERE ord_dt >= DATE '2025-01-01')
Run it and check the result row count and the name of the join operation in the execution plan. Then think about "what happens to the result if a NULL comes into CUST_ID on the subquery side".
Approach
1. Check that the result is correct before looking at speed
This query returns the correct result now. But "correct now" and "always correct" are different. Start from the table definition and check whether the column the subquery reads can contain NULL. ORDERS.CUST_ID has no NOT NULL constraint.
2. Expand NOT IN into a condition
x NOT IN (a, b, c) is x <> a AND x <> b AND x <> c. If one NULL slips into the list, x <> NULL is included. Work out what that comparison yields, and whether the whole AND expression can ever be true.
3. Look at the join name in the execution plan
Check whether it was unnested into an anti join and whether the name ends with NA (Null-Aware). NA means the optimizer also assumes "there may be NULLs" and checks for them separately. That narrows the methods the optimizer can choose from.
4. Express "none" in a way NULL cannot disturb
There are two directions.
- Add
cust_id IS NOT NULLto the subquery to remove NULLs up front - Instead of comparing with a list of values, ask for each customer "is there an order row matching the conditions"
The second matches the meaning of the question ("customers without orders") exactly. The join condition o.cust_id = c.cust_id never matches an order with NULL, so NULL does not affect the result.
Adding a NOT NULL constraint to the table is another option, but this lab does not change the schema. If the real data already contains NULLs, the constraint cannot even be added.
Explanation
NOT IN → NOT EXISTS
AFTER · NOT EXISTS
SELECT *
FROM customers c
WHERE NOT EXISTS (SELECT 1
FROM orders o
WHERE o.cust_id = c.cust_id
AND o.ord_dt >= DATE '2025-01-01')
Why the result is correct
- Right now both queries return the same result.
- Before resolves to
HASH JOIN ANTI NA(Null-Aware) and After to a regularHASH JOIN ANTI. NA has to check separately for NULLs, which narrows the optimizer's options. - NOT EXISTS only checks, for each customer, "is there an order row matching the conditions". An order with a NULL
CUST_IDmatches no customer, so it does not affect the result.
Why this is optimal
- The point of this scenario is to keep the result from going wrong depending on the data. NOT EXISTS returns the result exactly as the question means, even when NULLs come in.
- The execution method is a regular
HASH JOIN ANTI. It reads all 2025 orders and all customers once each and compares them. The report returns "every customer without orders", so the cost of reading both sides is unavoidable.
Mixing in one NULL
The sandbox is read-only, so instead of changing data, the subquery uses CASE to turn the CUST_ID of one row, the last order (ORD_ID = 2000000), into NULL. This simulates "one order with a NULL CUST_ID has come in".
TRY · NOT IN with one NULL mixed in
SELECT *
FROM customers
WHERE cust_id NOT IN (SELECT CASE WHEN ord_id = 2000000 THEN NULL ELSE cust_id END
FROM orders
WHERE ord_dt >= DATE '2025-01-01')
TRY · NOT EXISTS with one NULL mixed in
SELECT *
FROM customers c
WHERE NOT EXISTS (SELECT 1
FROM (SELECT CASE WHEN ord_id = 2000000 THEN NULL ELSE cust_id END AS cust_id, ord_dt
FROM orders) o
WHERE o.cust_id = c.cust_id
AND o.ord_dt >= DATE '2025-01-01')
- NOT IN returns 0 rows (
HASH JOIN ANTI NAmeets a NULL in the subquery and discards every row). NOT EXISTS returns the same 768 rows as before.
Key points
x NOT IN (a, b, NULL)isx <> a AND x <> b AND x <> NULL, andx <> NULLis always UNKNOWN. So the whole expression can never be true.- Before it is a performance tuning case, this is a bug that produces wrong results.
- To use
NOT IN, the columns on both sides must be NOT NULL (by constraint orWHERE cust_id IS NOT NULL).