views:

2275

answers:

10

At every company I have worked at, I have found that people are still writing their SQL queries in the ANSI-89 standard:

select a.id, b.id, b.address_1
from person a, address b
where a.id = b.id

rather than the ANSI-92 standard:

select a.id, b.id, b.address_1
from person a
inner join address b
on a.id = b.id

For an extremely simple query like this, there's not a big difference in readability, but for large queries I find that having my join criteria grouped in with listing out the table makes it much easier to see where I might have issues in my join, and let's me keep all my filtering in my WHERE clause. Not to mention that I feel that outer joins are much intuitive than the (+) syntax in Oracle.

As I try to evangelize ANSI-92 to people, are there any concrete performance benefits in using ANSI-92 over ANSI-89? I would try it on my own, but the Oracle setups we have here don't allow us to use EXPLAIN PLAN - wouldn't want people to try to optimize their code, would ya?

+13  A: 

According to "SQL Performance Tuning" by Peter Gulutzan and Trudy Pelzer, of the six or eight RDBMS brands they tested, there was no difference in optimization or performance of SQL-89 versus SQL-92 style joins. One can assume that most RDBMS engines transform the syntax into an internal representation before optimizing or executing the query, so the human-readable syntax makes no difference.

I also try to evangelize the SQL-92 syntax. Sixteen years after it was approved, it's about time people start using it! And all brands of SQL database now support it, so there's no reason to continue to use the abhorrent (+) Oracle syntax or *= Microsoft/Sybase syntax.

As for why it's so hard to break the developer community of the SQL-89 habit, I can only assume that there's a large "base of the pyramid" of programmers who code by copy & paste, using ancient examples from books, magazine articles, or another code base, and these people don't learn new syntax abstractly. Some people pattern-match, and some people learn by rote.

I am gradually seeing people using SQL-92 syntax more frequently than I used to, though. I've been answering SQL questions online for about 14 years.

Bill Karwin
I totally agree. I work with many SQL coders who learnt their SQL 15 years ago or more (as I did myself) and who know nothing of any innovation since they first started. They also lack any interest in finding out.
Tony Andrews
Agree, but add that there are well-documented scenarios where the old ANSI-89 Join syntax produces incorrect results... specifically outer Joins when there are conditional filtering predicates on non-Join related columns from the "outer" side of the join.
Charles Bretana
I've noticed massive performance gains by using the ANSI-92 code over the ANSI-89 code on MSSQL (2k and 2k5) here at work. I think MSSQL is actually stupid enough to do a Cartesian join and then filter the results with the where clause... or maybe it just somehow prevents it from using the index?
rmeador
I don't know specific internals of MS SQL, but that's good anecdotal evidence that SQL-92 syntax is worthwhile. Works for me!
Bill Karwin
Massive? Please post your work. My best bet is that it's not an apples to apples comparison, but don't respond with "oh yes it is" Just respond with a test case we can reproduce, version, patch level etc.
Bill, you're more scientific than that! At least from how I've gotten to know you from posts.
Well, I already want SQL-92 syntax to be used. So if it's true that it does no harm and may improve performance, do we need to know by how much?
Bill Karwin
Besides, "anecdotal" evidence is not a scientific measure anyway. It is always taken with a grain of salt.
Bill Karwin
I believe basing decision or formulating persuasions on lies, no matter how harmless or well intentioned is bad practice. People should expect to show their work. I have. I've been asked to prove answers and spent quite some time to do so.
There is no doubt that is a superior approach.
Bill Karwin
Hey, just for kicks look at the patch notes for 10.2.0.4. You'll see a TON of fixes to ANSI SQL. So there is a performance difference. Oracle seems to have missed something in the parser.
+4  A: 

A few reasons come to mind:

  • people do it out of habit
  • people are lazy and prefer the "old style" joins because they involve less typing
  • beginners often have their problems wrapping their heads around the SQL-92 join syntax
  • people don't switch to new syntax just because it is there
  • people are unaware of the benefits the new (if you want to call it that) syntax has, primarily that it enables you to filter a table before you do an outer join, and not after it when all you have is the WHERE clause.

For my part, I do all my joins in the SQL-92 syntax, and I convert code where I can. It's the cleaner, more readable and powerful way to do it. But it's hard to convince someone to use the new style, when they think it hurts them in terms of more typing work while not changing the query result.

Tomalak
For many people, looking at SQL at all hurts them. Changing any working code carries a risk for introducing a bug, especially when the coder is averting his eyes. :-)
Bill Karwin
Hm... to me even looking at complex regular expressions is no hurt at all. SQL can't harm me. ;-)
Tomalak
"Beginners often have problems... " Well THAT'S a selling point
For some reason I'm not sure if that was a "pro" or a "contra" comment... Maybe my irony detector is broken.
Tomalak
+1  A: 

I don't know the answer for sure.. this is a religous war (albiet of a lesser degree than Mac-Pc or others)

A guess is that until fairly recently, Oracle, (and maybe other vendors as well) did not adopt the ANSI-92 standard (I think it was in Oracle v9, or thereabouts) and so, for DBAs/Db Developers working at companies which were still using these versions, (or wanted code to be portable across servers that might be using these versions, they had to stick to the old standard...

It's a shame really, because the new join syntax is much more readable, and the old syntax generates wrong (incorrect) results in several well-documented scenarios.

  • Specifically, outer Joins when there are conditional filtering predicates on non-Join related columns from the table on the "outer" side of the join.
Charles Bretana
A: 

I can answer from the point of view of an average developer, knowing just enough SQL to understand both syntaxes, but still googling the exact syntax of insert each time I need it... :-P (I don't do SQL all day, just fixing some problems from time to time.)

Well, actually, I find the first form more intuitive, making no apparent hierarchy between the two tables. The fact I learned SQL with possibly old books, showing the first form, probably doesn't help... ;-)
And the first reference I find on a sql select search in Google (which returns mostly French answers for me...) first shows the older form (then explain the second one).

Just giving some hints on the "why" question... ^_^ I should read a good, modern book (DB agnostic) on the topic. If somebody has suggestions...

PhiLho
A: 

Oracle does not implement ANSI-92 at all well. I've had several problems, not least because the data tables in Oracle Apps are so very well endowed with columns. If the number of columns in your joins exceeds about 1050 columns (which is very easy to do in Apps), then you will get this spurious error which makes absolutely no logical sense:

ORA-01445: cannot select ROWID from a join view without a key-preserved table.

Re-writing the query to use old style join syntax makes the issue disappear, which seems to point the finger of blame squarely at the implementation of ANSI-92 joins.

Until I encountered this problem, I was a steadfast promoter of ASNI-92, because of the benefits in reducing the chance of an accidental cross join, which is far too easy to do with old-style syntax.

Now, however, I find it much more difficult to insist on it. They point to Oracle's bad implementation and say "We'll do it our way, thanks."

Jonathan
It may be easy to do a Cross Join, it's also something that doesn't happen spontaneously and it's certainly not ambiguous. Any decent SQL developer could spot it. But USING and NATURAL JOIN are temptresses which call out to you and smash your little boat on the rocks of anguish and misery.
My point was, it's easier to successfully create an accidental cross join by missing off the where clause that joins two tables together. A cross join has to be deliberate in ANSI-92. But I do agree that NATURAL JOIN is an abomination. :)
Jonathan
I understood your point. But it doesn't just happen out of the blue. As you debug your query you notice the problem and fix it. If you use natural join, with no changes -at all- to the query itself, it can change because of a table change.
... and not throw an error, just give wrong results.
A: 

I can't speak for all schools but at my university when we were doing the SQL module of our course, they didn't teach ANSI-92, they taught ANSI-89 - on an old VAX system at that! I wasn't exposed to ANSI-92 until I started digging around in Access having built some queries using the query designer and then digging into the SQL code. Realising I had no idea how it was completing the joins, or the implications of the syntax I started digging deeper so I could understand it.

Given that the available documentation isn't exactly intuitive in a lot of cases, and that people tend to stick to what they know and in many cases don't strive to learn any more than they need in order to get their job done, it's easy to see why adoption is taking so long.

Of course, there are those technical evangelists that like to tinker and understand and it tends to be those types that adopt the "newer" principles and try to convert the rest.

Oddly, it seems to me that a lot of programmers come out of school and stop advancing; thinking that because this is what they were taught, this is how it's done. It's not until you take off your blinkers that you realise that school was only meant to teach you the basics and give you enough understanding to learn the rest yourself and that really you barely scratched the surface of what there is to know; now it's your job to continue that path.

Of course, that's just my opinion based on my experience.

BenAlabaster
It's not just programmers. In many fields, it's hard to convince people to retrain once they're established in their career. There are exceptional individuals, of course, I assume in the same proportion in every field.
Bill Karwin
It's hard to convince someone to switch away from something successful, to something different that offers no benefit. Our .net side of the house has changed from 1.0 to 3.5 and each step in between with ZERO cajoling. Each new version was better. Can't say the same here.
+10  A: 

Well the ANSI092 standard includes some pretty heinous syntax. Natural Joins are one and the USING Clause is another. IMHO, the addition of a column to a table shouldn't break code but a NATURAL JOIN breaks in a most egregious fashion. The "best" way to break is by compilation error. For example if you SELECT * somewhere, the addition of a column could fail to compile. The next best way to fail would be a run time error. It's worse because your users may see it, but it still gives you a nice warning that you've broken something. If you use ANSI92 and write queries with NATURAL joins, it won't break at compile time and it won't break at run time, the query will just suddenly start producing wrong results. These types of bugs are insidious. Reports go wrong, potentially financial disclosure are incorrect.

For those unfamiliar with NATURAL Joins. They join two tables on every column name that exists in both tables. Which is really cool when you have a 4 column key and you're sick of typing it. The problem comes in when Table1 has a pre-existing column named DESCRIPTION and you add a new column to Table2 named, oh I don't know, something innocuous like, mmm, DESCRIPTION and now you're joining the two tables on a VARCHAR2(1000) field that is free form.

The USING clause can lead to total ambiguity in addition to the problem described above. In another SO post, someone showed this ANSI-92 SQL and asked for help reading it.

SELECT c.* 
FROM companies AS c 
JOIN users AS u USING(companyid) 
JOIN jobs AS j USING(userid) 
JOIN useraccounts AS us USING(userid) 
WHERE j.jobid = 123

This is completely ambiguous. I put a UserID column in both Companies and user tables and there's no complaint. What if the UserID column in companies is the ID of the last person to modify that row?

I'm serious, Can anyone explain why such ambiguity was necessary? Why is it built straight into the standard?

I think Bill is correct that there is a large base of developer who copy/paste there way through coding. In fact, I can admit that I'm kind of one when it comes to ANSI-92. Every example I ever saw showed multiple joins being nested in parentheses. Honesty, that makes picking out the tables in the sql difficult at best. But then an SQL92 evangilist explained that would actually force a join order. JESUS... all those Copy pasters I've seen are now actually forcing a join order - a job that's 95% of the time better left to optimizers especially a copy/paster.

Tomalak got it right when he said,

people don't switch to new syntax just because it is there

It has to give me something and I don't see an upside. And if there is an upside, the negatives are an albatross too big to be ignored.

"Amen" on your comments for forcing a join order.
David Aldridge
I tend to use ON because it's less ambiguous than USING or NATURAL JOIN. As for parentheses, people who learn "SQL" on Microsoft Access will use that as Access whines if you omit them. (The quotes around SQL should be finger quotes.)
R. Bemrose
*Cough* What's a USING clause? ;-) I come from the SQL server fraction, so this is not really on my radar. As R. Bemrose said, there is the ON clause, working just fine, never leaving me with a join I could not express syntactically. No need to adapt my DB design to query syntax to save some typing.
Tomalak
I never use NATURAL JOIN for the reasons you mention. If you use USING in an ambiguous way, the RDBMS should give you an error. MySQL does, for example. Compile-time failure is not possible when programmer use dynamic SQL.
Bill Karwin
A: 

Inertia and practicality.

ANSI-92 SQL is like touch-typing. In some theoretical way it might make everything better someday, but I can type much faster looking at the keys with four fingers now. I would need to go backwards in order to go forwards, with no guarantee that there would ever be a pay-off.

Writing SQL is about 10% of my job. If I need ANSI-92 SQL to solve a problem that ANSI-89 SQL can't solve then I'll use it. (I use it in Access, in fact.) If using it all the time would help me solve my existing problems much faster, I'd spend the time to assimilate it. But I can whip out ANSI-89 SQL without ever thinking about the syntax. I get paid to solve problems--thinking about SQL syntax is a waste of my time and of my employer's money.

Someday, young Grasshopper, you'll be defending your use of ANSI-92 SQL syntax against young people whining that you should be using SQL3 (or whatever). And then you'll understand. :-)

JPLemme
A: 

First let me say that in SQL Server the outer join syntax (*=) does not give correct results all the time. There are times when it interprets that as a cross join and not an outer join. So right there is a good reason to stop using it. And that outer join syntax is a deprecated feature and will not be in the next version of SQL Server after SQL Server 2008. You'll still be able to do the inner joins but why on earth would anyone want to? They are unclear and much much harder to maintain. You don't easily know what is part of the join and what is really just the where clause.

One reason why I believe you should not use the old syntax is that understanding joins and what they do and do not do is a critical step for anyone who will write SQL code. You should not write any SQL code without understanding joins thoroughly. If you understand them well, you will probably come to the conclusion that the ANSI-92 syntax is clearer and easier to maintain. I've never met a SQL expert who didn't use the ANSI-92 syntax in preference to the old syntax.

Most people who I have met or dealt with who use the old code, truly don't understand joins and thus get into trouble when querying the database. This is my personal experience so I'm not saying it is always true. But as a data specialist, I've had to fix too much of this junk through the years not to believe it.

HLGEM
Glad to meet you. Happy to be your first.
+2  A: 

In response to the NATURAL JOIN and USING post above.

WHY would you ever see the need to use these - they weren't available in ANSI-89 and were added for ANSI-92 as what I can only see as a shortcut.

I would never leave a join to chance and would always specify the table/alias and id.

For me, the only way to go is ANSI-92. It is more verbose and the syntax isn't liked by ANSI-89 followers but it neatly separates your JOINS from your FILTERING.

Roger Bold