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 comes out fine now. But this query is built so that one row of data can make the whole result disappear.

Tables

TableColumnTypeDescription
customerscust_idintCustomer ID (PK)
orderscust_idintID of the ordering customer. NULL allowed
ordersord_dtdatetime2(0)Order date and time
IndexColumns
customers_pk (clustered)cust_id
orders_x1cust_id, ord_dt

Customers with no orders in 2025

BEFORE · NOT IN

SELECT *
FROM customers
WHERE cust_id NOT IN (SELECT cust_id
                      FROM orders
                      WHERE ord_dt >= CAST('2025-01-01' AS datetime2(0)))

Run it and check the result row count and the execution plan. Consider what the Row Count Spool attached to the customer side checks.

Approach

1. Check that the result is correct before checking speed

This query's result is correct now (823 customers). But orders.cust_id has no NOT NULL constraint. NULL can enter the subquery result.

2. Expand NOT IN into a condition

x NOT IN (a, b, NULL) is x <> a AND x <> b AND x <> NULL. x <> NULL is UNKNOWN, neither true nor false, so the whole expression can never be true.

3. Check whether the execution plan checks NULL separately

The NOT IN plan has an extra branch with Nested Loops (Left Anti Semi Join) and Row Count Spool that checks separately whether "the subquery has a NULL". The optimizer also knows about the possibility of NULL and prepares for it.

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 >= CAST('2025-01-01' AS datetime2(0)))

Why the result is correct

  • Right now both queries return the same result (823 customers). Before has the NULL check branch (Row Count Spool) attached, and After finishes with a single Merge Join (Left Anti Semi Join). Buffers are about 5,600 for both.
  • NOT EXISTS checks only "is there an order row matching the condition" per customer. An order whose cust_id is NULL matches no customer, so it does not affect the result.

Why this is optimal

  • The report returns "all customers without orders", so the cost of reading all customers and the 2025 orders (all of orders_x1) once each is unavoidable. The point of this scenario is correctness, not performance.

Mixing in one NULL

The sandbox is read-only, so inside the subquery CASE turns the cust_id of the last order (ord_id = 2000000) into NULL to simulate "one NULL order 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 >= CAST('2025-01-01' AS datetime2(0)))

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 >= CAST('2025-01-01' AS datetime2(0)))
  • NOT IN returns 0 rows (the Row Count Spool in the NULL check branch finds the NULL and discards every customer). NOT EXISTS returns the same 823 rows as before.

Key points

  • x NOT IN (…, NULL) is always false. Before it is a performance tuning case, this is a bug that produces a wrong result.
  • To use NOT IN, the columns on both sides must be NOT NULL (a constraint or WHERE cust_id IS NOT NULL).
  • DB differences: Oracle keeps the same semantics with HASH JOIN ANTI NA (Null-Aware), and SQL Server with a Row Count Spool branch for the NULL check. The result becoming 0 rows is the same in every DB (SQL standard).