Sub topic 3
The NOT IN trap when NULLs are present
This chapter assumes you know basic SELECT and WHERE, correlated subqueries, EXISTS and NOT EXISTS, INNER and LEFT JOIN, and that NULL means a missing value.
The NOT IN trap when NULLs are present
Suppose you want customers with no orders. This query looks correct:
SELECT c.customer_id, c.name
FROM customers c
WHERE c.customer_id NOT IN (
SELECT o.customer_id
FROM orders o
);If orders.customer_id contains even one NULL, this query returns no rows. For each customer, the test becomes c.customer_id <> 1 AND c.customer_id <> NULL. The second comparison is UNKNOWN, so no row ever passes.
NOT EXISTS avoids this because a NULL never matches the equality condition:
SELECT c.customer_id, c.name
FROM customers c
WHERE NOT EXISTS (
SELECT 1
FROM orders o
WHERE o.customer_id = c.customer_id
);This chapter assumes you know basic SELECT and WHERE, correlated subqueries, EXISTS and NOT EXISTS, INNER and LEFT JOIN, and that NULL means a missing value.
The NOT IN trap when NULLs are present
Suppose you want customers with no orders. This query looks correct:
SELECT c.customer_id, c.name
FROM customers c
WHERE c.customer_id NOT IN (
SELECT o.customer_id
FROM orders o
);If orders.customer_id contains even one NULL, this query returns no rows. For each customer, the test becomes c.customer_id <> 1 AND c.customer_id <> NULL. The second comparison is UNKNOWN, so no row ever passes.
NOT EXISTS avoids this because a NULL never matches the equality condition:
SELECT c.customer_id, c.name
FROM customers c
WHERE NOT EXISTS (
SELECT 1
FROM orders o
WHERE o.customer_id = c.customer_id
);Semi-joins, anti-joins, and rewriting with JOIN
EXISTS is a semi-join: it keeps each outer row at most once. NOT EXISTS is an anti-join. A plain INNER JOIN repeats an outer row once per matching inner row, so add DISTINCT:
SELECT DISTINCT c.customer_id, c.name
FROM customers c
JOIN orders o ON o.customer_id = c.customer_id;The anti-join form uses LEFT JOIN. This assumes orders.order_id is a non-null key, such as the primary key:
SELECT c.customer_id, c.name
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.customer_id
WHERE o.order_id IS NULL;How the database runs these queries and which indexes help
Most engines run a correlated EXISTS as a semi-join. For each outer row, the engine probes the inner table for a match. An index on the correlation column makes each probe fast:
CREATE INDEX idx_orders_customer_id ON orders (customer_id);Plans differ between engines, so check the strategy with EXPLAIN.
Using EXISTS in UPDATE and DELETE
This statement deletes customers that have no orders:
DELETE FROM customers
WHERE NOT EXISTS (
SELECT 1
FROM orders o
WHERE o.customer_id = customers.customer_id
);Double NOT EXISTS: "for all" questions
To find customers who have ordered every product, use two levels of NOT EXISTS. Read it as: no product exists that the customer has not bought.
SELECT c.customer_id, c.name
FROM customers c
WHERE NOT EXISTS (
SELECT 1
FROM products p
WHERE NOT EXISTS (
SELECT 1
FROM orders o
JOIN order_items oi ON oi.order_id = o.order_id
WHERE o.customer_id = c.customer_id
AND oi.product_id = p.product_id
)
);If the products table is empty, every customer qualifies.
Exercise
Why does the NOT IN query return no rows when orders.customer_id contains a NULL, and how does NOT EXISTS avoid this?
Show answer
The condition c.customer_id NOT IN (..., NULL) evaluates to UNKNOWN rather than TRUE for every customer, so no row passes. NOT EXISTS only checks whether an equality match exists, and a NULL never equals anything, so customers without orders are still returned.