SQL error · Missing rows

LEFT JOIN loses rows from the left table

Problem: A WHERE filter on the right table rejects the rows padded with NULL.

Solution: If you want to keep every row from the left table, put the matching condition in ON.

Why it happens: LEFT JOIN loses rows from the left table

SQL that loses rows
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;
Result: only Amy remains
namestatus
Amypaid

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.

What the problem looks like

No error appears: the LEFT JOIN returns fewer rows than the left table and behaves like an INNER JOIN.

Since the engine gives no warning, compare the number of rows in the result with the number in the left-hand table: with a LEFT JOIN it should never be smaller.

How to fix it in SQL

Fixed SQL for this intent
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;
Result: all three customers are kept
namestatus
Amypaid
LiamNULL
MeganNULL

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
  1. Decide which rows should survive. Do you want all customers or only customers with a paid order?
  2. Identify which condition defines a valid match. That condition may belong to ON.
  3. Identify which condition filters the final result. That condition may belong to WHERE.
  4. 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.

Data and query · SQLite
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;
Verified result of the self-contained case
namepaid_order
Amy101
LiamNULL
MeganNULL

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).