r/SQL • • 24d ago

Discussion your LEFT JOIN might secretly be an INNER JOIN (the WHERE clause trap)

ran into this recently so posting it for anyone newer to joins.

two tiny tables:

customers orders
id | name id | customer_id | status
1 | Asha 1 | 1 | completed
2 | Ben 2 | 1 | refunded
3 | Chen 3 | 2 | completed
4 | Dina 4 | 3 | refunded

question: completed orders per customer, **including customers with 0**.

first try:

SELECT c.name, COUNT(o.id) AS completed_orders
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id
WHERE o.status = 'completed'
GROUP BY c.name;

result:

Asha | 1
Ben | 1

Chen and Dina are gone. the LEFT JOIN did its job and kept them (with NULLs for the order columns), but then WHERE runs after the join and `NULL = 'completed'` isn't true, so those rows get thrown out. Chen's only order is refunded so he gets dropped too. end result is exactly what an INNER JOIN would give you.

fix: move the filter into the ON:

SELECT c.name, COUNT(o.id) AS completed_orders
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id
AND o.status = 'completed'
GROUP BY c.name;

Asha | 1
Ben | 1
Chen | 0
Dina | 0

now the filter decides which orders get matched, not which customers survive.

rule I use: on a LEFT JOIN, conditions on the right table go in ON. conditions on the left table go in WHERE. (and use COUNT(o.id) not COUNT(*), otherwise Chen and Dina show 1 instead of 0)

0 Upvotes

13 comments sorted by

13

u/ElHombrePelicano 24d ago edited 24d ago

No. Your left joins are still left joins and your where clauses are still your where clauses.

2

u/TheMagarity 24d ago

OP is pointing out left join becomes a de facto inner join when the where clause references the outer joined table.

3

u/ElHombrePelicano 24d ago

Even then, that is not always true. You can ask for records where the outer table has a value of null for a given column and return a result that is not equivalent to an inner join.

I keep seeing these posts about left joins silently behaving like inner joins pop up in my feed more and more. I find it incredibly silly to treat this like we’re changing join types here for some specific set of criteria in the where clause, as opposed to just understanding what we are asking for.

1

u/TheMagarity 24d ago

You took OP's example of a problem and said it isnt a problem because of a completely different scenario. A basic left join that puts an equality on the outer table in the where clause does indeed turn it into a de facto inner join. That's all that's being shown here.

1

u/ElHombrePelicano 24d ago

And my point is that there are no problems here. Only a lack of understanding.

1

u/MrRoy_SQL 24d ago

yep exactly this, "de facto inner join" is a better way to put it than my title tbh

1

u/MrRoy_SQL 24d ago

Yeah fair, its still a left join technically, it just ends up returning the same rows an inner join would. "secretly" was doing a lot of work in that title lol

the one exception is WHERE o.id IS NULL, that one is fine on purpose, its the "customers with no orders" pattern

1

u/MrRoy_SQL 24d ago

yeah fair, its still a left join technically, it just ends up returning the same rows an inner join would. "secretly" was doing a lot of work in that title lol

the one exception is WHERE o.id IS NULL, that one is fine on purpose, its the "customers with no orders" pattern

1

u/RajeshRocketMan 23d ago

I’ve seen this break so many reports: someone adds a status filter to an old query and nobody notices. Moving the condition into the ON clause is the right fix.

0

u/alertetainry 24d ago

SQL really has a talent for giving you a completely valid query that returns exactly the wrong answer

1

u/A_name_wot_i_made_up 22d ago

On two occasions, I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able to rightly apprehend the kind of confusion of ideas that could provoke such a question.

Charles Babbage

It doesn't return the wrong answer, but you're asking the wrong question (whether you mean to or not).

0

u/MrRoy_SQL 24d ago

right?? no error, no warning, just confidently wrong numbers lol