You write a LEFT JOIN to pull every order from the database along with active customer details. On test data, the numbers match. You ship to production—and purchases from users who deleted their accounts quietly vanish from your revenue report.
It comes down to evaluation order. Per the PostgreSQL documentation, ON conditions run before the join, while WHERE runs afterward. When an order has no matching row on the right, the database fills in NULL. Then WHERE c.status = 'active' evaluates to UNKNOWN instead of TRUE and drops the row.
-- Wrong: WHERE drops NULLs and turns LEFT JOIN into a plain INNER JOIN
SELECT o.order_id, o.total_amount, c.customer_name
FROM orders AS o
LEFT JOIN customers AS c
ON o.customer_id = c.customer_id
WHERE c.status = 'active';
-- Right: filtering the right table inside ON keeps all orders
SELECT o.order_id, o.total_amount, c.customer_name
FROM orders AS o
LEFT JOIN customers AS c
ON o.customer_id = c.customer_id
AND c.status = 'active';
Move the filter into ON, and the engine selects active customers first before joining them to orders. Every order stays in the result, with NULL in customer_name when there's no match.
This rule holds across every SQL database. Keep WHERE for filters on the left table or explicit c.customer_id IS NULL checks when hunting for unmatched rows.
