Sub topic 13
NOT EXISTS and NOT IN as Alternatives
This chapter assumes you know SELECT, FROM, and WHERE, understand NULL, and can write an INNER JOIN with an ON condition. You should also know that a LEFT JOIN can find unmatched rows by checking for NULL on the right side.
An anti-join returns rows from one table that have no matching row in another table. For example, to find customers who have never placed an order, you can use a LEFT JOIN:
SELECT c.name
FROM customers AS c
LEFT JOIN orders AS o
ON o.customer_id = c.customer_id
WHERE o.order_id IS NULL;The same question can often be written more directly with NOT EXISTS.
Using NOT EXISTS
EXISTS is true when a subquery returns at least one row. NOT EXISTS is true when the subquery returns no rows.
SELECT c.name
FROM customers AS c
WHERE NOT EXISTS (
SELECT 1
FROM orders AS o
WHERE o.customer_id = c.customer_id
);The inner query is correlated because it refers to c.customer_id from the outer query. SQL evaluates the condition for each customer:
- Ada has matching orders, so the subquery exists. Ada is removed.
- Ben has a matching order, so Ben is removed.
- Cleo has no matching order, so the subquery does not exist. Cleo remains.
The expression SELECT 1 is conventional. EXISTS checks whether a row exists, not what value the subquery selects.
NOT EXISTS also works well when the matching rule uses several columns:
SELECT p.product_id, p.name
FROM products AS p
WHERE NOT EXISTS (
SELECT 1
FROM order_items AS i
WHERE i.product_id = p.product_id
AND i.quantity > 0
);This finds products with no order item having a positive quantity.
Using NOT IN
NOT IN compares one value with every value returned by a subquery:
SELECT c.name
FROM customers AS c
WHERE c.customer_id NOT IN (
SELECT o.customer_id
FROM orders AS o
WHERE o.customer_id IS NOT NULL
);The IS NOT NULL condition is important. In the sample data, order 104 has a NULL customer ID. Without the condition, the subquery returns:
| `customer_id` values |
|---|
| 1, 1, 2, NULL |
SQL cannot determine that 3 is different from NULL. The comparison becomes unknown, so NOT IN returns no customers instead of returning Cleo.
-- Unsafe when orders.customer_id can be NULL
SELECT c.name
FROM customers AS c
WHERE c.customer_id NOT IN (
SELECT o.customer_id
FROM orders AS o
);For this reason, prefer NOT EXISTS when the subquery column might contain NULL. It expresses the intended row-by-row test and does not suffer from this NOT IN trap.
| Pattern | Strength | Main risk |
|---|---|---|
| `LEFT JOIN ... IS NULL` | Familiar when you already need a join | Checking a nullable right-side column can give wrong results |
| `NOT EXISTS` | Clear and safe with nullable columns | Requires a correlated condition |
| `NOT IN` | Compact for a known non-null list | A single `NULL` can change the result |
Choosing among the alternatives
Use NOT EXISTS when you are asking whether a related row is absent. Use NOT IN only when the compared subquery column is guaranteed to be non-null, or when you explicitly remove nulls. Use the LEFT JOIN anti-join when you also need columns from the joined table or are already building a larger join.
Database optimizers can produce similar execution plans for equivalent forms. Choose the form that makes the intended test clear, then inspect an execution plan if performance matters.
Exercise
What does this query return with the sample data, and why? SELECT c.name FROM customers AS c WHERE c.customer_id NOT IN (SELECT o.customer_id FROM orders AS o WHERE o.customer_id IS NOT NULL);
Show answer
It returns Cleo. The inner query returns 1, 1, and 2, and the IS NOT NULL filter removes order 104's null customer ID. Customer 3 is not in that set.