views:

170

answers:

5

Do these two queries differ from each other?

Query 1:

SELECT * FROM Table1, Table2 WHERE Table1.Id = Table2.RefId

Query 2:

SELECT * FROM Table1 INNER JOIN Table2 ON Table1.Id = Table2.RefId

I analysed both methods and they clearly produced the same actual execution plans. Do you know any cases where using inner joins would work in a more efficient way. What is the real advantage of using inner joins rather than approaching the manner of "Query 1"?

+1  A: 

These will result in an identical query plan, but the INNER JOIN, OUTER JOIN, CROSS JOIN keywords are prefered because they add clarity to the code.

While you have the ability to specifiy join hints using the keywords in the FROM clause, you can do more complicated joins in the WHERE clause. But otherwise, there will be no difference in query plan.

Alex Papadimoulis
+2  A: 

The two statements you have provided are functionally equivalent to one another.

The variation is caused by differing SQL syntax standards.

For a really exciting read, you can lookup the various SQL standards by visiting the following Wikipedia link. On the right hand side are references and links to the various dialects/standards of SQL.

http://en.wikipedia.org/wiki/SQL

John Sansom
The old style outer join is deprecated in later versions .. so it makes sense to use JOIN for all queries.
gbn
+3  A: 

These SQL statements are synonymous, though specifying the INNER JOIN is the preferred method and follows ISO format. I prefer it as well because it limits the plumbing of joining the tables from your where clause and makes the goal of your query clearer.

RC
also on left joins, a condition in the ON works completely different than the same condition in the WHERE.
KM
+1  A: 

I will also add that the first syntax is much more subject to inadvertent cross joins as the queries get complicated. Further the left and right joins in this syntax do not work properly in SQL server and should never be used. Mixing the syntax when you add a left join can also cause problems where the query does not correctly return the results. The syntax in the first example has been outdated for 17 years, I see no reason to ever use it.

HLGEM
I caught that and fixed it, must have done that as you were writing this comment.
HLGEM
A: 

Query 1 is considered an old syntax style and its use is discouraged. You will run into problems with you use LEFT and Right joins using that syntax style. Also on SQL Server you can have problems mixing those two different syles together in queries that use view of different formats.

KM