Quick revision
The short answer
The LEFT JOIN first produces every customer, adding NULLs for orders where a customer has no match. WHERE runs afterwards, and o.created_at >= '2025-01-01' is unknown for those NULLs, so it removes exactly the rows the LEFT JOIN was meant to keep, which turns it into an inner join. Moving the condition into the ON clause, LEFT JOIN orders o ON o.customer_id = c.id AND o.created_at >= '2025-01-01', keeps all customers.
What to remember
In this answer, we saw that a LEFT JOIN first adds NULL-extended rows for unmatched customers, and that WHERE, running later, removes them because a comparison with NULL is unknown. The query silently becomes an inner join, and PostgreSQL even plans it as one. Moving the date condition into ON makes it part of matching, so every customer is preserved. Conditions on the preserved table go in WHERE, conditions on the optional table go in ON.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The LEFT JOIN first produces every customer, adding NULLs for orders where a customer has no match. WHERE runs afterwards, and o.created_at >= '2025-01-01' is unknown for those NULLs, so it removes exactly the rows the LEFT JOIN was meant to keep, which turns it into an inner join. Moving the condition into the ON clause, LEFT JOIN orders o ON o.customer_id = c.id AND o.created_at >= '2025-01-01', keeps all customers.
Why this question is asked
This is probably the most common outer join mistake in real code, and it is silent: the query runs, the result looks reasonable, and customers without recent orders simply vanish from a report that was supposed to list everyone. Interviewers use it to check whether we understand outer joins in terms of when each condition is applied, not as a recipe. In this answer, we will recap what a LEFT JOIN produces, reproduce the problem with a few rows, explain it with the logical processing order, see how the optimizer confirms our reading, fix it, and state the rule for where each kind of condition belongs.
1A Quick Recap of LEFT JOIN
A LEFT JOIN, or left outer join, returns every row of the left table, here customers, together with the matching rows of the right table, here orders. When a customer has no matching order, the database still returns the customer once and fills the order columns with NULL. Those filled-in rows are often called NULL-extended rows. The left table is the preserved table: none of its rows may disappear because of the join. The right table is the optional one.
2Reproducing the Problem
Take three customers. Ana ordered in March 2025, Ben's only order is from 2023, and Cem never ordered.
The goal was all customers, with their 2025 orders where they exist. We got only Ana. Ben disappeared because his only order is old, and Cem disappeared because he has no order at all.
3Why WHERE Removes the Rows
The logical processing order explains it. The FROM clause, including the LEFT JOIN and its ON condition, runs first and produces the three rows above, including Cem's NULL row. Only then does WHERE run, row by row.
For Ben's row, '2023-07-02' >= '2025-01-01' is false, so the row is removed. For Cem's row, the comparison is NULL >= '2025-01-01'. Any comparison with NULL is unknown, and WHERE keeps only rows where the condition is true, so this row is removed too. Every NULL-extended row fails any ordinary comparison on the right table's columns, which means the WHERE clause removes precisely the rows that made the join an outer join.
- LEFT JOIN, ON o.customer_id = c.idno order matches, so Cem gets a NULL order row
- WHERE o.created_at >= '2025-01-01'NULL >= date is unknown
- Is the condition true?YesRow keptNoRow removed
- Resultonly customers with a 2025 order remain
4 steps in order. Play them, or step through them one at a time.
4The Optimizer Agrees: It Becomes an Inner Join
Optimizers know this rule too. If a WHERE condition on the optional table can never be true for a NULL-extended row, the outer join cannot produce any extra rows, so the planner rewrites it as an inner join. PostgreSQL does this, and EXPLAIN for the broken query shows a plain Hash Join or Nested Loop instead of a Hash Left Join. Seeing an inner join in the plan for a query written with LEFT JOIN is a strong hint of this bug.
One tempting fix is WHERE o.created_at >= '2025-01-01' OR o.id IS NULL. It brings Cem back, but not Ben: Ben had a match in the join, so he has no NULL row, and his only row is filtered out. The query now silently means customers with a 2025 order or with no order ever, which is a different question.
5The Fix: Move the Condition Into ON
The condition about which orders count belongs in the ON clause, so it is applied while matching, before the NULL rows are added.
Now the join only matches orders from 2025. Ben's old order fails the ON condition, so Ben has no match and the LEFT JOIN gives him a NULL row, exactly like Cem. Every customer is preserved.
This gives us the rule. Conditions on the preserved table, such as c.status, belong in WHERE, because they decide which customers we want at all. Conditions on the optional table belong in ON, because they decide which orders count as a match. A WHERE condition on the optional table is only correct when it tests for NULL on purpose, as in an anti-join, WHERE o.id IS NULL to find customers without a 2025 order.
6Follow-up questions
How do I count 2025 orders per customer including zero?
Keep the date condition in ON, group by the customer, and count a column from orders: COUNT(o.id). COUNT(*) would count the NULL-extended row as 1 for customers with no order, while COUNT(o.id) ignores NULLs and gives 0.
Does the same apply to RIGHT and FULL joins?
Yes, mirrored. In a RIGHT JOIN, the right table is preserved, so conditions on the left table belong in ON. In a FULL JOIN both sides are optional, so any WHERE condition on either side can remove unmatched rows; filters usually go into subqueries before the join.
How can code review catch this?
Look for a LEFT JOIN where WHERE mentions a column of the right-hand table without IS NULL. Some linters flag it, and seeing an inner join in the EXPLAIN plan of a LEFT JOIN query confirms it.
7Conclusion
In this answer, we saw that a LEFT JOIN first adds NULL-extended rows for unmatched customers, and that WHERE, running later, removes them because a comparison with NULL is unknown. The query silently becomes an inner join, and PostgreSQL even plans it as one. Moving the date condition into ON makes it part of matching, so every customer is preserved. Conditions on the preserved table go in WHERE, conditions on the optional table go in ON.
Open with
The LEFT JOIN itself is fine: it returns every customer, and for customers with no matching order it adds a row where the order columns are NULL. The problem is the order of evaluation.
Then, if they want more
FROM and the join, with the ON condition, are applied first, and WHERE is applied afterwards to the joined rows. For a customer with no orders, o.created_at is NULL, and NULL compared with a date is unknown, which WHERE treats as not true, so the row is dropped.
For a customer whose orders are all older, the comparison is false and those rows are dropped too. So the WHERE removes exactly the rows the outer join was there to keep, and the query behaves like an inner join. PostgreSQL's planner actually rewrites it as an inner join, which you can see in EXPLAIN.
The fix is to move the date condition into the ON clause, so it decides which orders match: LEFT JOIN orders o ON o.customer_id = c.id AND o.created_at >= the date. Then a customer without a 2025 order gets a NULL row and stays in the result.
My rule is that conditions on the preserved table, the customers, go in WHERE, and conditions on the optional table go in ON, unless I am deliberately checking IS NULL for an anti-join. If I count orders, I count o.id rather than star.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- PostgreSQL documentation, The SQL Language
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A LEFT JOIN from customers to orders with WHERE o.created_at >= '2025-01-01' returns...": https://scrivio-interview-library.netlify.app/databases/sql-fundamentals/119ea0377b/