question

GPO avatar image
GPO asked

JOIN versus WHERE

I'm mainly writing this to get a bit of validation for some assumptions that I'm making. Please correct me if anything I say here is wrong. For a while I couldn't understand why people put a lot of WHERE-style restrictions in the JOIN part of the FROM clause. For example: SELECT bt.blah ,st.bleep FROM big_things bt JOIN small_things st ON bt.big_things_id = st.big_things_id and st.start_date >= '20070701' and st.end_date = '20070701' and st.end_date = '20070701' and st.end_date = '20070701' OR st.start_date is null) and (st.end_date t-sqljoinssyntax
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Kev Riley avatar image
Kev Riley answered
You need to be careful about mixing up JOIN conditions and WHERE filters. There's a similar discussion here , and I'll repeat here what I said there.... It is best practice to use JOIN to specify the join conditions BOL: >Specifying the join conditions in the FROM clause helps separate them from any other search conditions that may be specified in a WHERE clause, and is the recommended method for specifying joins In most cases it will make the query clearer as to what the JOIN conditions are, rather than what the WHERE filter is specified as. Execution plans may hardly differ (especially for inner joins), but your understanding of the query might. Also as you have shown, changing from an INNER JOIN to an OUTER JOIN, severely changes the meaning of the query if you specify conditions in the JOIN clause rather than the WHERE clause. --- Edit : just a thought Another way of writing the left join, but making it crystal clear what is being used as a filter and what is being used as a join condition, is to filter the table first, using an inline view, or derived table, and then join on that SELECT bt.blah ,st_ilv.bleep FROM big_things bt LEFT JOIN (select st.big_things_id, st.bleep from small_things st where st.start_date >= '20070701' and st.end_date
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

GPO avatar image GPO commented ·
...or even a CTE I suppose, which to my eye seem to be easier to read than ILVs.
0 Likes 0 ·
Mark avatar image
Mark answered
Ideally, you could use a primary key, or at least foreign key(s), to join any needed tables and put the rest in the WHERE clause. When you're joining tables, think of the columns that need to join to make the relation work right, then put the rest in the where clause. In effect, the JOIN only makes the tables work together as one table. For example, to me it doesn't make sense to include the dates in the JOIN part since it has nothing to do with the relation between the two tables; you're just limiting the information you want to see with the dates. So the date qualifications need to be in the WHERE clause.
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.