Why it happens: LEFT JOIN loses rows from the left table
WITH customers(id, name) AS (
VALUES (1, 'Amy'), (2, 'Liam'), (3, 'Megan')
), orders(id, customer_id, status) AS (
VALUES (101, 1, 'paid'), (102, 2, 'pending')
)
SELECT c.name, p.status
FROM customers AS c
LEFT JOIN orders AS p
ON p.customer_id = c.id
WHERE p.status = 'paid'
ORDER BY c.id;| name | status |
|---|---|
| Amy | paid |
Liam has a pending order and Megan has no order. After the LEFT JOIN, the condition WHERE discards both cases because p.status = 'paid' is not true.
How to fix it in SQL
WITH customers(id, name) AS (
VALUES (1, 'Amy'), (2, 'Liam'), (3, 'Megan')
), orders(id, customer_id, status) AS (
VALUES (101, 1, 'paid'), (102, 2, 'pending')
)
SELECT c.name, p.status
FROM customers AS c
LEFT JOIN orders AS p
ON p.customer_id = c.id
AND p.status = 'paid'
ORDER BY c.id;| name | status |
|---|---|
| Amy | paid |
| Liam | NULL |
| Megan | NULL |
How to check it
Find a left-hand row with no valid match and check that it still appears with NULL on the right.
How to avoid it next time
Do not move filters mechanically: ON and WHERE answer different questions in an outer join.
Related cases worth checking
- Decide which rows should survive. Do you want all customers or only customers with a paid order?
- Identify which condition defines a valid match. That condition may belong to
ON. - Identify which condition filters the final result. That condition may belong to
WHERE. - Test a case with no match. If your test data has no such row, the problem is easy to miss.
The case that OR IS NULL fails to rescue
Having no orders is different from having orders that fail the filter. Suppose Amy has a paid order, Liam has none and Megan has only a cancelled one. A filter on the right table in WHERE keeps Amy. Adding OR p.id IS NULL brings Liam back, but not Megan: the join found her cancelled order, so it did not create an extra row padded with nulls.
If the question is “every customer, with their paid orders where they have any”, the payment condition must be part of ON or of a right-hand source filtered beforehand. That way Megan is kept, with no paid order. If the question is “customers who have at least one paid order”, removing Liam and Megan is correct; a query with EXISTS can express that intent without producing one row per order.
Check at least four situations: one valid match, several valid matches, no right-hand rows, and right-hand rows that fail the filter. Do not use just any nullable column to detect a missing match: it can be null even on an order that was found. Choose a required key from the right table. This test stops you confusing unknown data with a relationship that does not exist.
Self-contained case to check in SQLite
Liam has no orders; Megan has one, but it is cancelled. Both should be kept when listing every customer with their payments.
WITH customers(id, name) AS (
VALUES (1, 'Amy'), (2, 'Liam'), (3, 'Megan')
), orders(id, customer_id, status) AS (
VALUES (101, 1, 'paid'), (103, 3, 'cancelled')
)
SELECT c.name, p.id AS paid_order
FROM customers c LEFT JOIN orders p
ON p.customer_id = c.id AND p.status = 'paid'
ORDER BY c.id;| name | paid_order |
|---|---|
| Amy | 101 |
| Liam | NULL |
| Megan | NULL |
The payment condition is part of finding a match; it is not applied afterwards to remove customers.
Check your interpretation: Why does moving the filter to WHERE and adding OR p.id IS NULL still remove Megan?
Show answer
Because Megan does have an order, the cancelled one: the LEFT JOIN matched her with it and her p.id is not NULL. WHERE discards that row because it is not paid, and OR p.id IS NULL does not rescue it.
Technical questions: PostgreSQL: Table Expressions and Joins (opens in a new tab).