Live data from Hacker News

Writing more legible SQL

craigkerstiens.com

161–168 of 168 posts

Re: Writing more legible SQL

#161

Am I the only one who puts commas on the next line? I think of them as an operator analogous to AND or ON: select t1.col1 as col1 , t2.col2 as col2 , t2.col3 as col3 It makes commenting out columns painless.

Yes I do. As you say so I can comment out easily but not so much on with the 'as' and maintaining the river.

Re: Writing more legible SQL

#162
I remember at one of my very first jobs, I had to deal with a dynamic report generator that would generate sql based on your UI selection. The SQL it generated was atleast a couple of pages long. The first thing I did was to lookup a sql formatter to help me figure out the dynamically generated sql statements. Thats how I came across sqlinform.com and I have always used it ever since.

It has some pre-defined formats depending on the database you use such as Oracle, DB2, etc. and it also lets you customize these formats if you like.

Re: Writing more legible SQL

#163

Here is how I write SQL: select t1.col1, t2.col2, t3.col3 from table1 t1 join table2 t2 on t1.col2 = t2.col1 join table3 t3 on t1.col3 = t3.col1 and t3.col2 = something_else where t1.col1 > 0 and t2.col2 t1.col4 order by col2 limit 100 So: 1. SQL capitalization is not sacred. I lowercase everything. 2. I just indent subclauses, with four spaces, like I indent other languages. I don't go out of my way to line up thing…

quick question about the indent of subclauses. How would you write a query where table3 needs to be joined with both table1 and table2? Would it change anything?

No. I indent all joins to the same level, regardless of the join condition. The effect of joins is to make one large flat table, so nested indentation is an unnecessary and misleading signal.

Re: Writing more legible SQL

#164
post #152

Earlier quoted context omitted.

My first thought was "why would you do that?". Glad to say I've rarely seen joins in the where clause.

This is what he means, and I see it all the time: SELECT FROM table1 JOIN table2 WHERE table1.field = table2.field An unsettling number of SQL developers never actually learned join syntax and write it that way. It's functionally equivalent to putting the equality in the JOIN ... ON clause, and any modern database will optimize it to execute the same way, but it's a worse syntactical representation of what's actually…

Great, thanks for that - I've been using (typically INNER) JOIN .. ON .. for all my life, well 20 odd years of development, and not once did I ever put the ON clause into WHERE. :)

I can vaguely recall seeing it a few times now, actually, and being rather confused as to why you'd do it that way - as perhaps it was a more optimal way of putting it, and that I'd done it wrong all the time?

As you state though, it's just bad; WHERE clauses ought to be used for filtering the data, not declaring how the tables join. Glad to have cleared that up. :)

Re: Writing more legible SQL

#165
post #39

Is there a good sql autoformatter? For cleaning up ORM-generated queries so I can read them. I've used python's sqlparse but it produces output that's often still unreadable.

Redgate's SQL Prompt has a great auto formatter with definable rule sets, if you use SSMS - and have the budget for it.

If you like the formatter in the current version of Prompt, wait until you see what's coming out in version 7.3. They've gone nuts. The new mechanisms for formatting are far beyond anything we've had so far. I'm pretty jazzed about it (disclosure: I work for Redgate).

Re: Writing more legible SQL

#167
post #152

Earlier quoted context omitted.

This is what he means, and I see it all the time: SELECT FROM table1 JOIN table2 WHERE table1.field = table2.field An unsettling number of SQL developers never actually learned join syntax and write it that way. It's functionally equivalent to putting the equality in the JOIN ... ON clause, and any modern database will optimize it to execute the same way, but it's a worse syntactical representation of what's actually…

Great, thanks for that - I've been using (typically INNER) JOIN .. ON .. for all my life, well 20 odd years of development, and not once did I ever put the ON clause into WHERE. :) I can vaguely recall seeing it a few times now, actually, and being rather confused as to why you'd do it that way - as perhaps it was a more optimal way of putting it, and that I'd done it wrong all the time? As you state though, it's jus…

You'd do it that way if you just didn't know any better. "Two tables where A = B" falls into a natural line of thinking for someone who picked up SQL ad hoc rather than formally learning what a join is and the syntax for it.
Post reply on HN