When joining two simple tables on primary key columns and placing an addition equality condition this can be done in the join itself or the where clause.
For instance the following are equiavalent. My question is - is there any reason to use one style over the other?
SELECT *
FROM A
INNER JOIN B ON A.A_ID = B.A_ID
AND A.DURATION = 3.00
...vs:
SELECT *
FROM A
INNER JOIN B ON A.A_ID = B.A_ID
WHERE A.DURATION = 3.00
For SQL Server 2000+, the query plans for each of these queries will be indentical, and therefore so will the performance.
You can verify this by using SSMS to display the actual execution plan for each of the queries hitting CTRL+M before executing your query. The results pane will have an additional tab that shows you the execution plan. You'll see that in this case, the two plans are the same.
It's a style matter. Generally, you'd want to put the conditions that define the "shape" of the result set in the FROM clause (i.e. those that control which rows from each table should join together to produce a result), whereas those conditions which filter the result set should be in the WHERE clause. For INNER JOINs, the effects are equal, but once OUTER JOINs (LEFT, RIGHT) are involved, it feels a lot clearer.
In your first example, I'm left asking "what has this got to do with Table B?" when I encounter this odd condition in the JOIN. Whereas in the second, I can skip over the FROM clause (and all JOINs) if I'm not interested, and just see the conditions which determine whether rows are going to be returned in the WHERE clause.
It's the type of JOIN that matters.
There's no difference, either version of the query provided will use the same execution plan because you are dealing with an INNER JOIN.
If dealing with an OUTER JOIN (IE: LEFT, RIGHT), there is a huge difference between the two versions because the
WHERE
criteria is applied after the JOIN is made. If the criteria is specified in theON
clause, the criteria is applied before the JOIN is made which can made a considerable difference between the result sets.Generally speaking it makes no semantic difference.
There is one edge case where it can do though. If the (deprecated)
GROUP BY ALL
construct is added to the query as illustrated below.Its a matter of style. The optimizer will do what's best.