In SQL I am trying to filter results based on an ID and wondering if there is any logical difference between
SELECT value
FROM table1
JOIN table2 ON table1
The answer is NO difference, but:
I will always prefer to do the following.
ON clausewhere clauseThis makes the query more readable.
So I will use this query:
SELECT value
FROM table1
INNER JOIN table2
ON table1.id = table2.id
WHERE table1.id = 1
However when you are using OUTER JOIN'S there is a big difference in keeping the filter in the ON condition and Where condition.
Logical Query Processing
The following list contains a general form of a query, along with step numbers assigned according to the order in which the different clauses are logically processed.
(5) SELECT (5-2) DISTINCT (5-3) TOP() (5-1)
(1) FROM (1-J) JOIN ON
| (1-A) APPLY AS
| (1-P) PIVOT() AS
| (1-U) UNPIVOT() AS
(2) WHERE
(3) GROUP BY
(4) HAVING
(6) ORDER BY ;
Flow diagram logical query processing
(1) FROM: The FROM phase identifies the query’s source tables and processes table operators. Each table operator applies a series of sub phases. For example, the phases involved in a join are (1-J1) Cartesian product, (1-J2) ON Filter, (1-J3) Add Outer Rows. The FROM phase generates virtual table VT1.
(1-J1) Cartesian Product: This phase performs a Cartesian product (cross join) between the two tables involved in the table operator, generating VT1-J1.
it is referred from book "T-SQL Querying (Developer Reference)"