Live data from Hacker News

How about trailing commas in SQL?

peter.eisentraut.org

111–120 of 272 posts

Re: How about trailing commas in SQL?

#111
post #6

I feel the problem. When coding (not only in SQL) you often have to add something to the end of a list, and it is annoying that the end of the list is always special. You can't just copy some line and move it there. Also when moving things around you always have to take extra care at the end. So, my solution for this was always SELECT a , b , c FROM ... instead of: SELECT a, b, c, -- people want to allow trailing com…

Mode Analytics published some data years ago showing that SQL programmers who preferred leading commas had a lower rate of errors than programmers who used trailing commas. [0]

0 - https://mode.com/blog/should-sql-queries-use-trailing-or-lea...

Re: How about trailing commas in SQL?

#112
post #33

Earlier quoted context omitted.

You've moved the problem from the last to the first element though. Surely people would prefer to be able to do this SELECT a, b, c, FROM ... ?

as mentioned elsewhere, i personally introduce a pad element to get fire and forget consistency SELECT 1 as pad , a , b

That's something where "what it makes the computer do" overrides "how nice it looks in text form" to me.

Re: How about trailing commas in SQL?

#113

I want leading _and_ trailing commas. Frequently I will be working on a query and have something like SELECT a , b , c from foo and then I want to comment out a column as I am working and exploring the data. But I cant comment out column a without also removing the comma on the next line.

Wouldn’t just trailing solve your issue?

    SELECT
    a,
    b,
    c,
    From Foo

Re: How about trailing commas in SQL?

#114
post #6

I feel the problem. When coding (not only in SQL) you often have to add something to the end of a list, and it is annoying that the end of the list is always special. You can't just copy some line and move it there. Also when moving things around you always have to take extra care at the end. So, my solution for this was always SELECT a , b , c FROM ... instead of: SELECT a, b, c, -- people want to allow trailing com…

I often do this with boolean WHERE filters when I'm doing interactive exploration on some data: SELECT ... WHERE foo = 1 AND bar = 2 I want to comment out a line (i.e. "--foo = 1"), but would break the syntax. The solution is to start with "WHERE true": SELECT ... WHERE true --AND foo = 1 AND bar = 2 Now you can comment/comment anything. (Putting the AND at end of each line has the same problem, of course, and requir…

God, everyone's going to hate me for this. (I will have earned it, I think.)

    SELECT ...
     WHERE foo = 1
       AND bar = 2
Each keyword gets a new line, the middle gutter between keyword and expressions stays in the same place, and things get really, really fugly if I need a subselect or whatever. Any given line can be commented out. (And no, none of that leading comma bullshit, somehow that looks nasty to me.) Downvote this into oblivion to protect the junior developers from being infected by whatever degeneracy has ahold of me.

Re: How about trailing commas in SQL?

#115

I want leading _and_ trailing commas. Frequently I will be working on a query and have something like SELECT a , b , c from foo and then I want to comment out a column as I am working and exploring the data. But I cant comment out column a without also removing the comma on the next line.

Wouldn’t just trailing solve your issue? SELECT a, b, c, From Foo

Moved the problem to column C

Re: How about trailing commas in SQL?

#116
post #14
post #6

I feel the problem. When coding (not only in SQL) you often have to add something to the end of a list, and it is annoying that the end of the list is always special. You can't just copy some line and move it there. Also when moving things around you always have to take extra care at the end. So, my solution for this was always SELECT a , b , c FROM ... instead of: SELECT a, b, c, -- people want to allow trailing com…

IMHO this is one of the ugliest formattings. Whenever I see that i try to revert and avoid at all costs. I know it's a personal flavor, yet. I might be too opinionated..

SQL is also case insensitive for most clauses!

`SeLeCt ... fRoM ... wHeRe ...` IS VALID! (And you should use a linter/formatter to avoid these categories of style war)

Re: How about trailing commas in SQL?

#117
post #115

Earlier quoted context omitted.

Wouldn’t just trailing solve your issue? SELECT a, b, c, From Foo

Moved the problem to column C

But if this change is adopted, then commenting out column C would still compile the query. There isn’t a need for leading?

Re: How about trailing commas in SQL?

#119
After writing code for 20+ years these sorts of concerns are just tiresome. The effort expended debating and enforcing optimizations like this completely wipes out any and all benefits they bring, which is incredibly minimal to begin with.

Just read the code, write your change, move on. If you find yourself missing small details like this you need to just slow down a tiny bit. Its not hard. By all means format your code cleanly, just don't spend cycles writing blog posts about it.

Re: How about trailing commas in SQL?

#120

I want leading _and_ trailing commas. Frequently I will be working on a query and have something like SELECT a , b , c from foo and then I want to comment out a column as I am working and exploring the data. But I cant comment out column a without also removing the comma on the next line.

Wouldn’t just trailing solve your issue? SELECT a, b, c, From Foo

in 20 years of writing SQL, preceding commas is so much better IMO. Its so easy to miss a comma at the end of a long expression. preceding commas means you can never forget them.

Then if I can have an extra leading comma, I can reorder, comment out or remove, or add a column at any point in the list without having to think about the rest of the projection. Also diffs are cleaner, it only highlights the rows that have changed, not the row above it/below it that had its comma added or removed. This happens a ton when im iterating on a query.

Post reply on HN