06 July 2007 08:10 tonyrogerson

SQL Style - FROM x,y,z or INNER JOIN; We are all Cowboy Coders

In ANSI 92 there are two styles of writing SQL, there is what is commonly referred to as the old or ANSI 89 style, that is the style where you put the join criteria in the WHERE clause; the other style is referred to either ANSI 92 or the new style and it is where you put the join criteria in the FROM clause using INNER JOIN, OUTER JOIN etc..

Coming fresh from yet another battle with that Joe Celko bloke from the public news groups (people who worship him should read no further for I am about to blaspheme) I thought I’d write up this entry. It appears that anybody who writes using the new ANSI 92 style is now a ‘Cowboy Coder’. Wow, well – I guess that means the majority of the developers in IT, me, a few world experts, anybody who has written a book on SQL Server and the doc writers of Books Online are all Cowboy Coders. Thanks to Itzik Ben Gan who has obviously too much time on his hands....

It all starts with Best Practice; in Books Online it clearly states that the recommended style is to use the new ANSI 92. Apparently in DB2 there are some optimisation problems with using the new style (yet to be confirmed, I’m sceptical).

Celko states that “Best Practice is not local, but global”, oh, and apparently I missed the “entire Software Engineering and Structured Programming revolution”, oh, and I'm a Barbarian....

Think about that statement, it's at odds with a MS recommendation and how we all work; Celko’s personal best practice is to use the old ANSI 89 style, but the MS SQL Server best practice is to use the new ANSI 92 style; what do you choose? Apparently choosing the MS SQL Server best practice, you know – that product we all code against would relegate us to being a “Cowboy Coder” for using “local and immediate solutions”.

Oh please, what is this guy on?

Best practice being a global thing is actually right, but local recommendations need to override because they have come from the vendor who made the product and it’s a best practice or recommendation. In life it’s the same, local bylaws change global laws.

Ok, why am I so darn angry about this guy and his stance; well, unfortunately he’s one of those internet bully blokes who likes to dismiss and put down newbies or people that at times struggle with SQL; but it’s more fundamental than that, the majority of folks use the ANSI 92 new style coding, in fact some dev’s will never have seen the old syntax, the new syntax was introduced in SQL Server 6.0 many moons ago. Like many, on seeing the old syntax I always rewrite to the new to clearly understand what is going on (it usually takes a while too!) – the logic behind the query, the join relationships etc... In the old syntax the logic etc... was buried in the WHERE clause, remember the days of forgetting the join condition and causing a Cartesian product?

Anyway, let’s have a look at some styles....

Old ANSI 89 style would go something like this...

select *

from sys.objects as o,

     sys.columns as c,

     sys.sql_modules as sm

where o.object_id = c.object_id

  and sm.object_id = c.object_id

  and o.type_desc = 'VIEW'

The join criteria is mixed up in the WHERE clause, what you are actually doing is a cross join query and then restricting the results.

The equivalent new style ANSI 92 query is below

select *

from sys.objects as o

    inner join sys.columns as c on o.object_id = c.object_id

    inner join sys.sql_modules as sm on sm.object_id = c.object_id

where o.type_desc = 'VIEW'

 

 

The join criteria is distinctly separate from the filter, relationships are much clearer.

Formatting is another aspect that is key here...

select *

from sys.objects as o

inner join

sys.columns as c

on o.object_id = c.object_id

inner join

sys.sql_modules as sm

on sm.object_id = c.object_id

where o.type_desc = 'VIEW'

When arguing the syntax you often see Celko format the new style like the above, frankly it’s totally unreadable so I guess that’s where his real problem of migrating to the new syntax is.

I mean, would any of us format like this (sorry, I actually know a few who do – sorry folks, feel free to comment – I’m being adversarial)?

select *

from sys.objects as o,sys.columns as c,sys.sql_modules as sm

where

o.object_id = c.object_id

and

sm.object_id = c.object_id

and

o.type_desc = 'VIEW'

Again,
unreadable, it’s
the equivalent
of me
writing this
sentence
like I’ve
just done.

Crazy!

But one of the real problems with the old style syntax is that people where forever forgetting to put all the join conditions in the WHERE clause so we ended up with Cartesian products...

select *

from sys.objects as o,sys.columns as c,sys.sql_modules as sm

where o.object_id = c.object_id

  and o.type_desc = 'VIEW'

I can’t do that with the new syntax, I’d need to mix old and new...

select *

from sys.objects as o

    inner join sys.columns as c on o.object_id = c.object_id

    inner join sys.sql_modules as sm

where o.type_desc = 'VIEW'

The above gives an Invalid syntax error.

Another problem (thanks SQL Menace) is that if you write your query using the old syntax and you then need to make the inner join an OUTER JOIN you need to rewrite to the new style anyway.

To sum up, really you should all be using the new style, I know it’s a new style if you have come over from Oracle because I think it’s only been a couple of years that this syntax has been in the product, but learn it - get used to it; I remember I truely hated the new syntax until I got my head round it and now I really see the benefits.

And as for Celko, well, I think somebody coined a good phrase that is very appropriate – I think Celko is suffering from Unixitis which is a belief that something is better because it is more obscure and complex, thus making you morally superior because you understand it.

 

Filed under: ,

Comments

# re: SQL Style - FROM x,y,z or INNER JOIN; We are all Cowboy Coders

06 July 2007 09:05 by StevenWhite

Totally agree with you Tony.

Reminds me of the (rather old) ease of use debate between VMS vs Unix (Personally I perferred VMS)

Steven

# re: SQL Style - FROM x,y,z or INNER JOIN; We are all Cowboy Coders

06 July 2007 09:21 by Matija Lah

Yipikaye!

What else is there to say...?

:)

# re: SQL Style - FROM x,y,z or INNER JOIN; We are all Cowboy Coders

06 July 2007 12:05 by DamianMulvena

I SO support you on this Tony.

I think it is so much clearer when developers are reminded to specify the conditions for each part of the join (or to document that they actually wanted a Cross Join).

In particular, the new syntax allows you to separate the join condition from the where filter for an outer join, whereas the old syntax treats every condition as the Where. This can allow greater flexibility in which rows are returned - I wish I had a good (succinct) example of this. You can't easily do this with the old syntax.

The old syntax doesn't seem to allow for outer joins. What does Celko use for these?

Lastly a warning though. I've encountered a problem when using views in MS SQL 2000. When a query (or a view) references a view, you can't mix the MS *= variant of the old syntax in one with the new syntax in the other. This has implications for takeup of the new syntax sometimes.

# re: SQL Style - FROM x,y,z or INNER JOIN; We are all Cowboy Coders

06 July 2007 12:39 by tonyrogerson

Hi Damian,

Celko uses the new style for OUTER JOIN and old style for inner thereby mixing the two styles which is well need I say it...

*= and =* where deprecated in 2005 so can no longer be used.

Tony.

# Interesting Finds: July 6, 2007

06 July 2007 14:41 by Jason Haley

# re: SQL Style - FROM x,y,z or INNER JOIN; We are all Cowboy Coders

06 July 2007 16:30 by SQL_Menace

I also agree with Tony

That Google group thread is very entertaining indeed (52 messages right now)

Denis

# re: SQL Style - FROM x,y,z or INNER JOIN; We are all Cowboy Coders

09 July 2007 09:17 by smatyk

What about:

select *

from sys.objects as o

   join sys.columns as c on o.object_id = c.object_id

                                     and o.type_desc = 'VIEW'

   join sys.sql_modules as sm on sm.object_id = c.object_id

1)  You don't need to say 'inner', do you?  Isn't it assumed if not stated?

2)  can't the 'where' clause filter just be appended to the join clause?

# re: SQL Style - FROM x,y,z or INNER JOIN; We are all Cowboy Coders

09 July 2007 09:28 by tonyrogerson

Hi SmatyK,

Absolutely, the inner is implied, what about this though which illustrates why I always state INNER...

select '10' + 10

Without running it - what do you think the answer is? Most people will say '1010' or would need to run it, but imagine if those constants where columns and it becomes hidden because you can't see the data-types. I prefer to explicitly state what is happening to aid the guy following me, or me if its a late night problem.

For inner joins, the place where you put the filter - on the ON clause or the WHERE clause won't make a difference to your query, on OUTER JOIN it makes a massive difference - putting the filter on the ON means you filter BEFORE the join takes place, on the WHERE its AFTER the join takes place.

I prefer to put filtering for constants in the WHERE clause.

Tony.

# re: SQL Style - FROM x,y,z or INNER JOIN; We are all Cowboy Coders

09 July 2007 09:43 by Colin Leversuch-Roberts

I thought the whole point of ansi standards was that we were intended to follow them?  I'd have thought Joe was part of the ansi process - but I guess not.

Can't say I'd really thought about the merits of the two standards, having moved to the 92 std when microsoft supported it. It just seemed so much easier and logical.

# re: SQL Style - FROM x,y,z or INNER JOIN; We are all Cowboy Coders

09 July 2007 13:22 by jkz999

The reason SQL-92 adopted this is because SQL-89 was wrong when using outer joins.   That's why Celko uses the new style for OUTER JOINs.  Look up

"Specifying Joins in FROM or WHERE Clauses"

In books Online (2000) for why this is so.

# re: SQL Style - FROM x,y,z or INNER JOIN; We are all Cowboy Coders

09 July 2007 13:37 by tonyrogerson

Absolutely, in that article it clearly states the recommended approach is to put joins in the FROM clause and filtering on the WHERE clause.

Celko only does that for OUTER JOIN, he does not use this new style infix notation for inner joins.

When dealing with inner joins he always mixes the join critieria with the filter criteria on the WHERE clause which is against Microsoft recommended best practice as stated in that article. And he thinks anybody who does use the inner join infix notation is a "cowboy coder".

The thread is here: http://groups.google.co.uk/group/microsoft.public.sqlserver.programming/browse_frm/thread/bf9e20ea9299598c/97465f4a1b604410?lnk=st&q=%22--celko&rnum=7&hl=en#97465f4a1b604410

Tony.

# re: SQL Style - FROM x,y,z or INNER JOIN; We are all Cowboy Coders

10 July 2007 04:08 by Ralph D. WIlson II

Well, I'm going to jump in here even though I _am_ a Yank. ;-)

I guess I am some sort of Bi-SQLer because I switch from one style to the other, kind of depnedin on how clear I need to make it to someone I am communicating the query text to.  I have found that it is frequently easier for newbies and, shall we say, "other than programmers" to understnad the query in segments: The items being selected, where the heck they come from, and how those sources relate to each other.

My personal pet peeve/whine is that I tend to format my SQL so that it is fairly readable (and takes up several lines) and then SQL Server turns it all into garbage when it reformats my code into as few lines as possible.

# re: SQL Style - FROM x,y,z or INNER JOIN; We are all Cowboy Coders

11 July 2007 14:51 by Jamie Collins

Have you read any of Celko’s writings on programming style? There’s some IMO good stuff in there, backed up by research, etc. For example, putting SQL keywords in uppercase does IMO make SQL code easier to read. I note you use lowercase SQL keywords, as does BOL. Are you open minded to ideas that do not originate from Microsoft? I get the impression you that if Microsoft tells you to code a certain way there’s a good chance you’ll do so regardless of your own judgement e.g. “I truely [sic] hated the new syntax until I got my head round it” -- so Microsoft can get you to do something you hate?!

“on seeing the old syntax I always rewrite to the new to clearly understand what is going on (it usually takes a while too!)”

You recommend spending significant time re-engineering functional code if it doesn't conform to a vendor’s current style? Risk assessment? Off the clock or chargeable?

Myself, I sometimes code in the ‘old’ style and sometimes in the new, whichever I think will be easiest to maintain in the circumstances. Now I’m thinking I’ll code more in the ‘new’ style, save the next guy the pointless re-engineering <g>!

Jamie.

--

# re: SQL Style - FROM x,y,z or INNER JOIN; We are all Cowboy Coders

11 July 2007 17:07 by tonyrogerson

I read some of his early books and just gave up because his examples don't work on MS SQL Server - there are significantly better value for money books on the market. And, after seeing his attitude and posts in the public forums, a couple of bad reviews on Amazon really makes you think they aren't worth while - background reading may be.

You have to remember, I've been writing SQL since 1989, 17 years; it was only 6.0 we got the new style syntax (around 95/6?), those prior years where on DB2 and MS SQL 4.21. So, after 5 or 6 years of day in day out SQL programming with the old style, yes, it was a pain moving to the new style which everybody now uses.

Unfortunetly, queries have got more complex over the years, the engine more complicated, everything is just more complicated, time is money etc.. I find it far more efficient when looking at a problem to rewrite as part of understanding what the query is doing - it usually takes a few minutes to migrate to the new style after which I can then understand what is going on. A few minutes overhead is nothing; but - that is from somebody who had 5 / 6 years of using the old stuff day in and day out; lot's of people now don't have that luxury - people with only a few years in the industry. At the end of the day, this rarely happens because the majority of folks and systems around now are the new style anyway.

I always follow best practice, its a good philosphy; go with the majority - it makes your life easier and the guy who follows you won't think your div for making things over complicated.

Tony.

# re: SQL Style - FROM x,y,z or INNER JOIN; We are all Cowboy Coders

12 July 2007 11:15 by Jamie Collins

"Usually takes a while", "usually takes a few minutes", which is it to be <g>? Either way, in our shop you'd lose marks (literally) for re-writing code to achieve functional equivalence in a different style; among other things, it screws up source control history. I think the essence of this blog topic is lack of respect towards someone who has a style different to your own ([tongue in cheek] or ageist perhaps: if ‘Cole Joke’ is an anagram then you’re 'Snooty Groner' whom I urge to 'go retro, sonny' <g>). SQL is such a [irony] 'rich' language that the same thing can be written many different ways: do you choose the construct that is easiest to maintain, performs best, conforms with the vendor's preferred style? Surely circumstances must dictate. I'm not sure I consider "increased coder comprehension" to be justification for a re-write.

Jamie.

--

# re: SQL Style - FROM x,y,z or INNER JOIN; We are all Cowboy Coders

12 July 2007 11:58 by tonyrogerson

Jamie, Jamie, Jamie - I said thos who worship Celko should not read on.

No, the essance of the blog is style; this so called industrial leader who dictates, degrades and insults people who don't follow what is his personal style and ideas uses a style that is the exact opposite of vendor recommendation and best practice; I want people to be aware next time they read one of his books that his style is old and rarely used, it is fringe coding. Yes, I also want to dish out some stick - but, he invites that through his derogatary attitude to all.

When reading in French, I convert to English so I can understand the context of the text. It's no different converting the French (old style) to the English (new style) when understanding the problem.

You appear to be adopting celkos rudeness in that last comment, did I not show respect to you even though I completely disagree with you? I only treat celko with contempt, nobody else.

;)

Tony.

# re: SQL Style - FROM x,y,z or INNER JOIN; We are all Cowboy Coders

12 July 2007 13:47 by Jamie Collins

Tony, I credit you with a sense of humour but just in case I'll proffer sincere apologies in case my tongue in cheek stuff offended. Brevity is the nature of posting blog comments, I hope to not sound blunt. Yes, you have been respectful to me, although I did find it a little demeaning to be branded a 'Celko worshiper' for trying to preach a message of 'live and let live' as regards programming style and, come to think of it, being addressed as "Jamie, Jamie, Jamie" is a tad condescending, but I can take it [sniffs]. If this blog is about a Celko's personal style in newsroups then we're pretty much in agreement.

Now please don't think me rude in saying this but I don't think translating French to English is the path to greater comprehension of written French; you need to IMO learn how to think in French (for the record, I got CSE grade 4 in French at school). If you need to reconfigure some code to help figure it out, fine by me but please think twice before checking it in e.g. have you introduced a subtle change in behaviour or bug, can other coders use source control history to detect your *material* change (as distinct from opposed to *stylistic* changes), etc? I'm in no way suggesting you write buggy code, just making a simple point about introducing any kind of risk for the sake of a different supported ANSI style.

Final point: I notice you refer to 'SQL Server books' rather than 'SQL books'. Is this because, like me, you realize that 'old style' joins are common in the general SQL literature? For example I was perusing the Snodgrass 'temporal' book today and noted the 'old style' being used throughout.

Jamie.

--

# re: SQL Style - FROM x,y,z or INNER JOIN; We are all Cowboy Coders

12 July 2007 14:13 by tonyrogerson

Check when Richard Snodgrass wrote his book - 1995 - 1997; the new style was not mainstream, it had only been in SQL Server for a couple of years if that and other vendors I don't think they had it in their products at that time. I very much doubt he'd be inclined to change all the examples to the new style because he's not focusing on the SQL.

The PDF version is here (http://www.cs.arizona.edu/~rts/tdbbook.pdf) and definitely worth a read for temporal stuff.

Also my final point; people need to consider the environment to which they write, the overwelming majority of folks write using the new style, I see many many systems each year through my consultancy and as part of the community stuff I do, rarely ever do you see the old style. Development and support is complex enough without somebody adding more complexity by adopting a style nobody uses or has to spend extra time trying to work out what's going on.

Tony.

# re: SQL Style - FROM x,y,z or INNER JOIN; We are all Cowboy Coders

13 July 2007 08:48 by Jamie Collins

Tony, You final point is good and one I intend to put into immediate effect.

Cheers,

Jamie.

--