• subscribe
December 21, 2004 12:00 AM

Choosing SELECT Statements

SQL Server Pro
InstantDoc ID #44719

What's the difference between the following SELECT statements?

SELECT * FROM tableA, tableB
 WHERE tableA.a1 = tableB.b1

and

SELECT * FROM tableA JOIN tableB
 ON tableA.a1 = tableB.b1

Someone told me the first statement isn't ANSI standard, but a lot of my existing code uses that syntax. Do I need to change the non-ANSI standard SELECT statements? Does one format perform better than the other?

Many people incorrectly think that ANSI doesn't support the ability to define a JOIN condition in a query's WHERE predicate. In truth, ANSI lets you define a JOIN condition in either a query's WHERE clause or its JOIN clause. A similar question recently came up on the public newsgroups, and I liked SQL Server MVP Steve Kass's advice and comments. Both SELECT statements are standard ANSI syntax, though not everyone recognizes that. Neither ANSI nor Microsoft favors one statement syntax over the other. The only JOIN syntax ANSI discourages is the old standard of specifying an outer join by using an asterisk and equals symbol (*=). Because ANSI SQL-89 didn't support outer joins (ANSI SQL-92 introduced that support), vendors had to devise their own syntax. But today, ANSI SQL-92 requires you to specify an outer join in the FROM clause. In fact, SQL Server will produce an incorrect answer if you use the old outer join syntax and include an IS NULL predicate in a column on the dependent side of the join.

Both inner join queries in your question are well defined; the choice between them is largely a matter of taste. Many people prefer the second format because the consequences of accidentally issuing SELECT * FROM A JOIN B and forgetting the ON clause is less disastrous than issuing SELECT * FROM A, B and forgetting the WHERE clause. If you forget the ON clause in the second query, the query won't run. But if you forget the WHERE clause in the first query, that query could produce dramatically wrong results and could consume a huge amount of resources if the query produces a Cartesian product between large tables. I find it easier to read queries that specify JOIN conditions in the JOIN clause. Although performance of both query formats is usually identical, sometimes you'll get different plans in complicated join situations if you specify the condition in either the WHERE or FROM clause.



ARTICLE TOOLS

Comments
  • Glenn
    5 years ago
    Jan 05, 2007

    I was mistaken in thinking that the first syntax was pre-ANSI or an earlier version of ANSI. Thanks for the correction.

    I do still strongly prefer the second syntax, and think that most people should get used to it because I think it will supplant the other eventually.

  • Daniel
    5 years ago
    Jan 04, 2007

    .

  • Amy
    5 years ago
    Jan 04, 2007

    I've been told that the join in the Where clause was not ANSI standard. Thank you for clarifying!

You must log on before posting a comment.

Are you a new visitor? Register Here
  • SP1?
    I know there is a SP1 for SQL 2008 R2 available....and there is a "feature pack" as well... ...
  • SQL database mirroring
    I have SQL Server 2008 R2 Enterprise 64bit on Windows 2008 R2 Enterprise 64bit.  Each SQL Server has...
  • Dell Compellent Disk Drive
    Does anybody has experience with Dell Compellent Disk Drive? Basically, this system manages all disk...
  • Sql server performance tuning
    I need to find a tool that help me to optimize sql server,queries,improve the performance and solve ...