sqlstyle.guide icon indicating copy to clipboard operation
sqlstyle.guide copied to clipboard

No dangling commas in column lists for select, group by, and order by.

Open oraclesean opened this issue 1 year ago • 0 comments

When a column list in a select, order by or group by spans multiple lines, individual lines should not end in a comma. This makes it easier to manage and comment code during development. For example:

Bad

  select col1, col2,
         col3
...

In this case, commenting out col3 requires two comments—one to address the dangling comma, and another on the next line:

  select col1, col2 --,
--         col3
...

Good

  select col1, col2
,        col3
...

In this example, commenting out col3 requires a single comment at the beginning of the line.

This makes it easier to debug SQL—especially complex statements with multiple tables/joins, aggregate functions, and ordering—where a developer may need to remove a table. With dangling commas this gets tiresome:

  select a.col1, sum(a.col2) --,
--         b.col3, avg(b.col4)
    from tab_a a
--    join tab_b b
--      on a.pk = b.fk
group by a.col1 --,
--         b.col3

This is more straightforward, less confusing, requires less navigation to edit, saves time, and avoids errors caused by missing a comment:

  select a.col1, sum(a.col2)
--,        b.col3, avg(b.col4)
    from tab_a a
--    join tab_b b
--      on a.pk = b.fk
group by a.col1
--,        b.col3

oraclesean avatar Nov 27 '24 12:11 oraclesean