Live data from Hacker News

Writing more legible SQL

craigkerstiens.com

31–40 of 168 posts

Re: Writing more legible SQL

#31
post #17

I write my SQL as follows: SELECT `second_column`, `fourth_column` FROM `table_name` WHERE `first_column` = 'Value' AND `third_column` = 3 ORDER BY `fifth_column` ASC LIMIT 1

What happens when you need to do something non-trivial? Ie. nested selects, joins or even only some more complex expression in WHERE?

Re: Writing more legible SQL

#32

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…

1. Why indent the joins? Are table2 and table3 less important than table1? Is table1 special? Is that why it enjoys privileged status in the from-clause? 2. Why place some predicates in the where-clause and others in the join-clauses? What's the thinking here? Why not put all predicates up in the join-clauses, nearer to the tables that they affect?

I think of joins as operators and from as the block similar to and/or in the where clause. He is being inconsistent with line breaks for select, from and where blocks. Personally I want to be able to visually pick out the blocks of the query and the easiest way to do that is with indention imo.

Re: Writing more legible SQL

#33

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…

I fully agree with the sentiment, and especially with treating SQL code as code, because it really is code. My style differs in 4 regards:

1. "join"s are at the same level as "from", and the contents of from/join are indented

2. prefer tuple comparisons of multi-comparisons, i.e. "(a,b)=(c,d)" instead of "a=c and b=d"

3. operators auch as "and" are at the beginning of a line, as in style guide of almost all other programming languages, too

4. the contents of "order by" and "group by" are also indented, the same way as the "select" columns

Example:

    select
        t1.col1,
        t2.col2,
        t3.col3
    from
        table1 t1
    join
        table2 t2 on t1.col2 = t2.col1
    join
        table3 t3 on (t3.col3,  r3.col2) = (t1.col3, something_else)
    where
        t1.col1 > 0
        and t2.col2  t1.col4
    order by
        col2 asc,
        col1 desc
    limit 100

Re: Writing more legible SQL

#34

Is it just me, or are the code examples not monospaced ? This doesn't help when talking about alignment.

It isn't just you, and indeed, the alignment examples make little sense with a proportional typeface.

I checked the CSS, and for some reason the code blocks use the exact same fonts as the rest of the text (albeit with a new font-family clause just for them).

Re: Writing more legible SQL

#35
post #17

I write my SQL as follows: SELECT `second_column`, `fourth_column` FROM `table_name` WHERE `first_column` = 'Value' AND `third_column` = 3 ORDER BY `fifth_column` ASC LIMIT 1

Well that is cute. I find it odd that the second line of the where clause is on the same level as the where but a neat format otherwise. Tabs or spaces though?

Re: Writing more legible SQL

#36
post #8

I prefer my own style where comma is placed before every column. It makes columns, subqueries and case expressions line up nicely, especially when you have 15 columns or more.

I do this often mainly due to beine annoyed by missing a comma somewhere or having to worry about commas when commenting out the last item

Re: Writing more legible SQL

#37
post #33

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…

I fully agree with the sentiment, and especially with treating SQL code as code, because it really is code. My style differs in 4 regards: 1. "join"s are at the same level as "from", and the contents of from/join are indented 2. prefer tuple comparisons of multi-comparisons, i.e. "(a,b)=(c,d)" instead of "a=c and b=d" 3. operators auch as "and" are at the beginning of a line, as in style guide of almost all other pro…

I always keep the joins indented from the table that they are joined to:

  select    t1.col1,
            t2.col2,
            t3.col3,
            t4.col4
  from      table1 t1
                join table2 t2 on t2.colx = t1.colx
                join table3 t3 on t3.coly = t1.coly
                    join table4 t4 on t4.colz = t3.colz
                join tablen tn on tn.coln = t1.coln
  where     tn.colnx in (...)

            -- Table 3 and Table 2 have some values while Table n value is not something OR Table n has value that is exactly something
            and (
                    (t3.col = x and t2.col = y and tn.col != z)
                    or 
                    tn.col = z
                )
  ...
So in above you can see by the indent on which table some other table is joined to. Tables t2, t3 and tn are related to t1, but t4 is related to t3, not directly t1.

Re: Writing more legible SQL

#38
This is what is wrong with 'best practices'

`SELECT foo, bar FROM baz`

Is not at all more legible than:

`SELECT foo, bar FROM baz`

On first glance I even missed the 'bar' column completely and just saw it when compressing this line.

As things get longer it gets more important to make it legible, but saying that my first example is better than the second is just nonsense.

Don't worry so much about what you should or should not do, just use common sense.

Re: Writing more legible SQL

#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.

Re: Writing more legible SQL

#40
So I think it is much easier to read if you can line up like clauses:

  SELECT spf.blah_blah,
         count(*) AS cnt
    FROM jv_CTLG_ENTITY_ATTR_PROD_MAP prod_map
    JOIN jv_SSA_PRODUCT_FACT spf
         ON prod_map.prod_ref_id = spf.PROD_REF_ID
   WHERE prod_map.INVSBL_IND = '0' AND
         spf.LSTG_END_DT >= date_sub(from_unixtime(unix_timestamp()), 2) AND
         spf.LSTG_START_DT 
Post reply on HN