// SQL
LEFT JOIN with a WHERE clause acts like an INNER JOIN: the SQL fix
Every example on this page was run on 30 November 2026 in the SQL database on this page, which is close to PostgreSQL, and the output under each one is what it printed. How we test these posts.
A LEFT JOIN with a WHERE clause acts like an INNER JOIN when the WHERE condition tests a column of the right table. Rows with no match get NULL in every right-table column, a condition such as o.status = 'paid' is never true for NULL, and WHERE throws those rows away. The most common fix is to move the condition into the ON clause, so it filters the matches instead of the result.
Why does my LEFT JOIN act like an INNER JOIN?
A LEFT JOIN acts like an INNER JOIN because a WHERE condition on the right table removes every row where the right side is NULL, which is exactly the rows the LEFT JOIN added. Here the task is to list every customer with their paid orders. Ben has no orders and Chen has only a refunded one, yet both vanish:
WITH customers(customer_id, name) AS (
VALUES (1, 'Ana'), (2, 'Ben'), (3, 'Chen')
),
orders(order_id, customer_id, status) AS (
VALUES (101, 1, 'paid'), (102, 1, 'refunded'), (103, 3, 'refunded')
)
SELECT c.name, o.order_id
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.customer_id
WHERE o.status = 'paid'
ORDER BY c.name;
WITH customers(customer_id, name) AS (
VALUES (1, 'Ana'), (2, 'Ben'), (3, 'Chen')
),
orders(order_id, customer_id, status) AS (
VALUES (101, 1, 'paid'), (102, 1, 'refunded'), (103, 3, 'refunded')
)
SELECT c.name, o.order_id
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.customer_id
WHERE o.status = 'paid'
ORDER BY c.name;Output
| name | order_id |
|---|---|
| Ana | 101 |
The query is valid, so there is no error and no warning. The result is the same as an INNER JOIN with the same filter, and the bug often goes unnoticed until someone asks why a customer is missing from a report.
Why does a WHERE condition on the right table remove rows?
A WHERE condition on the right table removes rows because SQL runs the join first and the WHERE filter second. After the LEFT JOIN, Ben’s row has NULL for order_id and status. Then NULL = 'paid' is NULL, meaning unknown, and WHERE keeps only rows where the condition is true. Running the join without the filter shows the rows the filter sees:
WITH customers(customer_id, name) AS (
VALUES (1, 'Ana'), (2, 'Ben'), (3, 'Chen')
),
orders(order_id, customer_id, status) AS (
VALUES (101, 1, 'paid'), (102, 1, 'refunded'), (103, 3, 'refunded')
)
SELECT c.name, o.order_id, o.status, o.status = 'paid' AS kept
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.customer_id
ORDER BY c.name, o.order_id;
WITH customers(customer_id, name) AS (
VALUES (1, 'Ana'), (2, 'Ben'), (3, 'Chen')
),
orders(order_id, customer_id, status) AS (
VALUES (101, 1, 'paid'), (102, 1, 'refunded'), (103, 3, 'refunded')
)
SELECT c.name, o.order_id, o.status, o.status = 'paid' AS kept
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.customer_id
ORDER BY c.name, o.order_id;Output
| name | order_id | status | kept |
|---|---|---|---|
| Ana | 101 | paid | true |
| Ana | 102 | refunded | false |
| Ben | NULL | NULL | NULL |
| Chen | 103 | refunded | false |
Only the row marked true survives. Ben is dropped because his comparison is NULL, and Chen is dropped because his one order is refunded. The usual causes, most common first:
- A filter on a right-table column, such as
o.status = 'paid'oro.created_at >= '2026-01-01'. o.col <> 'x', which also fails onNULL, so it removes the unmatched rows too.- A second
INNER JOINthat starts from the right table, such asJOIN payments p ON p.order_id = o.order_id, which needs a non-NULLo.order_id.
The PostgreSQL joined tables reference describes this order: the ON condition decides which rows match, and WHERE filters the joined rows afterwards.
How do I fix a LEFT JOIN with a WHERE condition?
Fix a LEFT JOIN with a WHERE condition by moving the right-table condition into the ON clause, or by filtering the right table in a subquery before the join. Both keep every customer.
Move the condition into the ON clause
Write LEFT JOIN orders o ON o.customer_id = c.customer_id AND o.status = 'paid' and drop the WHERE. In the ON clause, the condition only decides which orders count as a match. A customer with no matching order still gets one row, filled with NULL:
WITH customers(customer_id, name) AS (
VALUES (1, 'Ana'), (2, 'Ben'), (3, 'Chen')
),
orders(order_id, customer_id, status) AS (
VALUES (101, 1, 'paid'), (102, 1, 'refunded'), (103, 3, 'refunded')
)
SELECT c.name, o.order_id
FROM customers c
LEFT JOIN orders o
ON o.customer_id = c.customer_id
AND o.status = 'paid'
ORDER BY c.name;
WITH customers(customer_id, name) AS (
VALUES (1, 'Ana'), (2, 'Ben'), (3, 'Chen')
),
orders(order_id, customer_id, status) AS (
VALUES (101, 1, 'paid'), (102, 1, 'refunded'), (103, 3, 'refunded')
)
SELECT c.name, o.order_id
FROM customers c
LEFT JOIN orders o
ON o.customer_id = c.customer_id
AND o.status = 'paid'
ORDER BY c.name;Output
| name | order_id |
|---|---|
| Ana | 101 |
| Ben | NULL |
| Chen | NULL |
Conditions on the left table still belong in WHERE. WHERE c.name <> 'Ana' removes Ana, as you would expect. Put the same condition in the ON clause and Ana stays in the result with NULL order columns, because ON never removes a row of the left table.
Filter the right table before the join
Filtering the right table in a subquery or a CTE first gives the same result, and reads well when the filter is long:
WITH customers(customer_id, name) AS (
VALUES (1, 'Ana'), (2, 'Ben'), (3, 'Chen')
),
orders(order_id, customer_id, status) AS (
VALUES (101, 1, 'paid'), (102, 1, 'refunded'), (103, 3, 'refunded')
),
paid_orders AS (
SELECT order_id, customer_id FROM orders WHERE status = 'paid'
)
SELECT c.name, p.order_id
FROM customers c
LEFT JOIN paid_orders p ON p.customer_id = c.customer_id
ORDER BY c.name;
WITH customers(customer_id, name) AS (
VALUES (1, 'Ana'), (2, 'Ben'), (3, 'Chen')
),
orders(order_id, customer_id, status) AS (
VALUES (101, 1, 'paid'), (102, 1, 'refunded'), (103, 3, 'refunded')
),
paid_orders AS (
SELECT order_id, customer_id FROM orders WHERE status = 'paid'
)
SELECT c.name, p.order_id
FROM customers c
LEFT JOIN paid_orders p ON p.customer_id = c.customer_id
ORDER BY c.name;Output
| name | order_id |
|---|---|
| Ana | 101 |
| Ben | NULL |
| Chen | NULL |
A filter inside the subquery runs before the join, so it can never remove a left-table row.
How do I count with a LEFT JOIN and still get zero?
Count with a LEFT JOIN and get zero by counting a right-table column, not *, with the filter in the ON clause. COUNT(o.order_id) skips NULL, so a customer with no paid order gets 0. COUNT(*) counts the NULL-filled row and gives 1:
WITH customers(customer_id, name) AS (
VALUES (1, 'Ana'), (2, 'Ben'), (3, 'Chen')
),
orders(order_id, customer_id, status) AS (
VALUES (101, 1, 'paid'), (102, 1, 'refunded'), (103, 3, 'refunded')
)
SELECT c.name,
COUNT(o.order_id) AS paid_orders,
COUNT(*) AS wrong_count
FROM customers c
LEFT JOIN orders o
ON o.customer_id = c.customer_id
AND o.status = 'paid'
GROUP BY c.name
ORDER BY c.name;
WITH customers(customer_id, name) AS (
VALUES (1, 'Ana'), (2, 'Ben'), (3, 'Chen')
),
orders(order_id, customer_id, status) AS (
VALUES (101, 1, 'paid'), (102, 1, 'refunded'), (103, 3, 'refunded')
)
SELECT c.name,
COUNT(o.order_id) AS paid_orders,
COUNT(*) AS wrong_count
FROM customers c
LEFT JOIN orders o
ON o.customer_id = c.customer_id
AND o.status = 'paid'
GROUP BY c.name
ORDER BY c.name;Output
| name | paid_orders | wrong_count |
|---|---|---|
| Ana | 1 | 1 |
| Ben | 0 | 1 |
| Chen | 0 | 1 |
The PostgreSQL aggregate functions page states that COUNT(expression) counts only rows where the expression is not NULL.
When does a WHERE condition on the right table belong in WHERE?
A WHERE condition on the right table belongs in WHERE when it is IS NULL on a column that is never NULL in a real match. WHERE o.order_id IS NULL keeps only the customers with no match, which is the anti-join pattern for “rows in A with no match in B”:
WITH customers(customer_id, name) AS (
VALUES (1, 'Ana'), (2, 'Ben'), (3, 'Chen')
),
orders(order_id, customer_id, status) AS (
VALUES (101, 1, 'paid'), (102, 1, 'refunded'), (103, 3, 'refunded')
)
SELECT c.name
FROM customers c
LEFT JOIN orders o
ON o.customer_id = c.customer_id
AND o.status = 'paid'
WHERE o.order_id IS NULL
ORDER BY c.name;
WITH customers(customer_id, name) AS (
VALUES (1, 'Ana'), (2, 'Ben'), (3, 'Chen')
),
orders(order_id, customer_id, status) AS (
VALUES (101, 1, 'paid'), (102, 1, 'refunded'), (103, 3, 'refunded')
)
SELECT c.name
FROM customers c
LEFT JOIN orders o
ON o.customer_id = c.customer_id
AND o.status = 'paid'
WHERE o.order_id IS NULL
ORDER BY c.name;Output
name Ben Chen
Here the paid filter sits in ON and the IS NULL test sits in WHERE, so the query finds customers with no paid order. Move the paid filter to WHERE and the query returns no rows at all.
How do I catch this in my own queries?
Catch it by reading every LEFT JOIN and checking its WHERE clause for a right-table alias. Any test on that alias other than IS NULL turns the join into an inner join. A quick habit: run the query once without the WHERE, count the left-table rows, and compare. Keep a left-table row with no match in your test data, as the snippets above do, so a missing row shows up before it ships.
The same NULL logic empties a NOT IN subquery, covered in NOT IN subquery returns no rows. For more joins and grouping tasks of the kind interviews ask, see the SQL interview practice guide and Column must appear in the GROUP BY clause.
Related errors
A LEFT JOIN that returns more rows than the left table has a different cause: the right table has several matches per key, so each left row repeats once per match. The fix is to aggregate or deduplicate the right table before the join.