Joinsmedium0-2 years
A LEFT JOIN in a query is supposed to keep customers with no orders, but the report is missing them. Separately, a total looks doubled. What are the two classic causes?
First cause: a filter on the joined table sitting in WHERE instead of ON. WHERE runs after the join, and an unmatched left row has NULLs for every right-side column, so WHERE o.placed_at >= '2026-01-01' evaluates to unknown for those rows and drops them — the LEFT is still in the text and does nothing. Second cause, the doubled total: joining a one-to-many relationship (a customer to their orders, then to their addresses too) multiplies rows, so SUM(total) counts each order once per matching address instead of once per order.
PreviousWhy does `WHERE status <> 'CLOSED'` silently drop rows where status is NULL, and why can COUNT(*) and COUNT(column) disagree on the same table?Next How does a PreparedStatement's plan cache actually save work compared to concatenated SQL, and what's the trade-off — parameter sniffing — that comes with it?