Is there a difference in the results or a difference in performance between these two statements/examples:
Example 1 (Consecutive INNER Relationship)
SELECT ID FROM TABLE T
INNER JOIN TABLE2 T2 ON T.ID = T2.ID
INNER JOIN TABLE3 T3 ON T2.ID = T3.ID
Example 2 (Non-Consecutive INNER Relationship)
SELECT ID FROM TABLE T
INNER JOIN TABLE2 T2 ON T.ID = T2.ID
INNER JOIN TABLE3 T3 ON T.ID = T3.ID
or between these 2 statements/examples:
Example 1 (Consecutive LEFT Relationship)
SELECT ID FROM TABLE T
INNER JOIN TABLE2 T2 ON T.ID = T2.ID
LEFT JOIN TABLE3 T3 ON T2.ID = T3.ID
Example 2 (Non-Consecutive LEFT Relationship)
SELECT ID FROM TABLE T
INNER JOIN TABLE2 T2 ON T.ID = T2.ID
LEFT JOIN TABLE3 T3 ON T.ID = T3.ID
The only difference is the second Join is on the Table whereas the first example is on Table2. I’m pretty sure they return the same thing, but I’m just looking for more experienced answers here. Thanks.
There is no difference in results. The statements are functionally equivalent.
The difference in performance depends on the specific database and version, and on the data.
My experience is that in a simple case like this there is typically no difference in performance between the two queries. The SQL2008 optimizer will run the join in whatever order makes sense according to statistics (size of tables, availability of indexes), regardless which way you write this query.