Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Comments on Are there best practices for sticking conditions in WHERE clauses vs the JOIN statement?

Parent

Are there best practices for sticking conditions in WHERE clauses vs the JOIN statement?

+12
−0

Lets say I have two tables, A and B and I need to join a subset of them.

Is there best practices of sticking the conditions in the WHERE clause like this,

SELECT * 
FROM A
JOIN B on a.fk_b = b.pk
WHERE a.pk <10000

versus sticking the condition in the JOIN like this,

SELECT * 
FROM A
JOIN B on a.fk_b = b.pk
AND a.pk <10000

For these, it doesn't make any difference in speed or results, but are there best practices for where to put the conditions?

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

Post
+13
−0

SQL is a declarative language, and the form of the query does not dictate the form of the query plan that actually retrieves the data. So these two queries might be not only the same speed, but actually map to the exact same query plan to be executed.

So if speed and results don't give an advantage to one form, the best practice is to go with the one that is easier to read. In this case, I'd go with the first one.

In the second query, the JOIN handles both joining and filtering the data. By using a separate WHERE clause, you separate the actions of joining and filtering into different clauses.

If a second filter needs to be added later, it's a little less intuitive to add it to the JOIN. It's possible a later developer might create a WHERE clause, and then you'd have the JOIN filtering and joining, and the WHERE filtering as well.

But if you're already using a WHERE for filtering, it's simple and intuitive to add another condition to the WHERE.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

First, I want to say I agree with this answer, even though it looks like I am about to disagree. But ... (1 comment)
First, I want to say I agree with this answer, even though it looks like I am about to disagree. But ...
mjb‭ wrote almost 3 years ago

First, I want to say I agree with this answer, even though it looks like I am about to disagree. But from my perspective, join-related criteria is the only stuff that should be in the ON portion. Filter criteria is the only stuff that should be in the WHERE portion. In the specific question above, your answer makes perfect sense. But I've been asked this numerous times when the two options were really about the JOIN part(on a.fk_b = b.pk vs where a.fk_b = b.pk). I hope this does not confuse other newer developers.