Discussion Anyone else including their WHERE conditions in the JOIN conditions?
I really like to put where conditions, which belong to a table into the join condition. Basic example
from table
left join cte
on cte.ID = table.ID
and cte.rank = 1Personally I find this more readable especially when I have a query involving multiple joins, because you don't have a big block of where conditions after a big block of joins and need to puzzle things together.
Obviously I do not put every condition in the join and I also do not put complex nested conditions in there.
I am just wondering if I am the only who feels that this way is more logical. As far as I am aware the result of the query should be the same.
33
Upvotes
107
u/fauxmosexual NOLOCK is the secret magic go-faster command Mar 25 '26
In your example, putting a condition in the where clause would actually change the behaviour of the query: the join would behave like an inner join.
But stylistically I do include some things in the join that don't strictly need to be there. My rule of thumb being anything that's related to just how the data is structured goes into the join, and business logic based stuff goes in the where. So things like latest_flag or lookup_type in a join to a lookup table I prefer to embed in the join.
29
u/Gargunok Mar 25 '26
Note this isn't just about readability. Where you put the "where" changes the answer set that comes back. For example a left joint with a where essentially becomes a inner joint.
So right answer right place to put the where is where is matches the logic you are trying to write.
19
u/gorkemguzel32 Mar 25 '26
You’re playing with fire my boy, don’t make it your habit if you do critical reporting. Pain of inadvertent inner join will hurt at some point.
10
u/elevarq Mar 25 '26
Putting ‘and cte.rank = 1’ in a WHERE would make it a very different query.
9
u/NeoChrisOmega Mar 25 '26
Once you get to more complex joining you realize it's situational on when you want to do the Logic in the join, or the where.
It definitely changes the outcomes
11
u/InRainbows123207 Mar 25 '26
Madness
4
u/Alkemist101 Mar 25 '26
Depending on the join and tables it won't give the same results. Typically moving where logic to a left join can change the results so check both ways. This is because of the order of operations as to when the filter logic is applied.
Doing it with an inner join will give same results.
It's usually best practice to have the logic in the join where possible not for performance but for readability. It's easier to see where the filter logic is being applied.
4
u/squadette23 Mar 25 '26
> As far as I am aware the result of the query should be the same.
this is not true, and it's a common source of errors. You're also saying "Obviously I do not put every condition in the join and I also do not put complex nested conditions in there." which is not really obvious: why not every condition? What's wrong with complex nested conditions? Why don't you go all-in to your strategy if you think that the result should be the same?
Here is a long explanation on why you should not pick up this habit because it is a source of mistakes when conditions change:
https://kb.databasedesignbook.com/posts/sql-joins/#why-you-should-only-use-id-equality
2
u/tampacraig Mar 25 '26
I do it mostly with inner joins where it helps reduce result set record counts significantly. Especially if indexes support the columns in question.
2
u/LayBackAndEnjoy Mar 25 '26
If i have a JOIN, and I put additional parameters in JOIN does it execute faster than puting it into WHERE?
2
u/BigBlue_72 Mar 25 '26
A better approach would be common table expressions if your DBMS supports them. Other option is to use parentheses to enclose the filter and then use an alias in the rest of the query. In either approach, use a useful business name for what the data represents.
2
u/ComicOzzy sqlHippo Mar 25 '26
This makes sense in some cases, but not others. Here is what you are really writing:
This pre-filters
ctefor only the rows whererank = 1prior to performing the join.A word of warning, though... you can't do this same thing in a LEFT JOIN with the left table or a RIGHT JOIN with the right table. For those you'll have to put the filter in the
WHEREclaue or use a subquery.Also, you cannot move the
cte.rank = 1filter into theWHEREclause without it effectively turning this into anINNER JOIN.2
u/Raychao Mar 25 '26
This is the old Oracle syntax. Since SQL-92 we have used <JOINTYPE> JOIN...ON (JOIN EXPR) syntax.
This could very easily change the behaviour of the query. You should use the modern syntax.
1
u/Possible_Chicken_489 Mar 25 '26
I do see the upside of the approach, especially avoiding accidentally turning left joins into inner joins.
But I find myself reluctant to actually do it. Having all the conditions concentrated in a WHERE block is just baked into my habits at this point.
1
u/Sexy_Koala_Juice DuckDB Mar 25 '26
When I need to, sure. But be warned that it can and will impact your query because the behaviour is different on Join and Where
1
u/thedragonturtle Mar 25 '26
You used a left join so the logic would be different and would exclude some rows if you put the second condition on where instead of the join.
For inner joins I agree though.
1
u/Ginger-Dumpling Mar 25 '26
Even when it doesn't matter, I tend to use on-conditions to show relationships, where-condition to reduce results. Generally helps keep things consistent when switching between inner/outer joins. Do what makes sense to you "and your team" for the type of work you do.
1
u/Legal_Tradition_9681 Mar 26 '26
As others have stated the results will be different because you are using a left join.
Best practices table relationship items should be in the ON clause and rows to be returned in the where clause. This is not mandatory though.
It is rare but I have faced a scenario where the execution planner does a poor job when I had a condition in the ON clause making it slower than putting it in the WHERE clause. This shouldn't be the case but can happen so sticking with best practices is usually better.
1
u/No_Resolution_9252 Mar 27 '26
I put predicates that impact joins in with the join, otherwise I put it in the where
1
u/thatOMoment Mar 27 '26
For (inner) joins Column comparisons only
For left join, yeah sometimes you need a comparison to a value there to get the right results
Where is for comparing columns to constants, variables or against the result of subqueries
Inner joins as top most of possible
Lateral joins in middle
Left Joins as low as possible
At least that's my style.
1
u/Icy_Clench Mar 28 '26
Everyone else has done a good job explaining the technical differences. Semantically there is still a good reason when to use the where clause even when the results are the same.
Conditions in the join should denote how to identify a match between two rows like foreign key relations or a conditional statement (e.g., date_col between start_date and end_date). You define the relation, not the filter. So ask yourself if it’s necessary to match rows together or if you’re trying to discard matched rows.
-3
u/just-fran Mar 25 '26
You been hired for 5 minutes or should we tell your boss your data has been bad for quite some time?
0
u/[deleted] Mar 25 '26
[deleted] — view deleted comment