Skip to content(if available)orjump to list(if available)

How about trailing commas in SQL?

How about trailing commas in SQL?

147 comments

·February 11, 2025

h1fra

Without talking about SQL it would be nice for git diff. I only see benefits of supporting them, hopefully this can be integrated into the SQL standards

adornKey

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 comma here
  FROM ...
Leading commas also are also very regular and visible. A missing trailing comma is a lot harder to see.

Before people start to mess with the standard, I'd suggest to maybe just change your coding style - and make the start of the list special - instead of the end.

I'd suggest going for a lot less trailing commas - instead of allowing more.

cnity

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

You have simply moved the "special" entry to the beginning rather than the end.

Side remark: I've noticed that when it comes to code formatting and grammar it's almost like there are broadly two camps. There are some instances of code formatting that involve something "clever". Your example of leading commas above for example. Another example is code ligatures. It's as if there's a dividing line of taste here where one either feels delight at the clever twist, or the total opposite, rarely anything in between. I happen to dislike these kinds of things (and particularly loathe code ligatures) but it is often hard to justify exactly why beyond taste.

Code ligature thing has something to do with just seeing the characters that are actually there rather than a smokescreen, which IMO impedes editability because I can't place the cursor half-way through a ligature and so on. But it's more than that -- you could fix those functional issues and I'll still dislike them.

adornKey

Adding something to the end is the most common thing to do. And changing the start is extremely rare - it's anyway special because you usually put it in the line with the SELECT.

quietbritishjim

> you usually put it in the line with the SELECT.

No, you usually don't. That would "bury" the first field so you don't immediately see it if you quickly glance at the code. I'll admit I was a bit surprised when I saw a fully formatted SQL query but it does look much better:

   SELECT
      a,
      b,
      c,
      d
   FROM
      Customers
   ORDER BY
      b,
      c DESC

Edit: I've just seen other comments here suggesting you return an extra "pad" value at the start so you can use leading commas without "losing" the first value visually. I hardly know where to start with that. It transmits more data on the wire and, to my eyes, looks horrific. That level of hackery is proof, as if it were needed, of how badly needed trailing commas are.

boxed

Hard disagree.

Quekid5

> Code ligature thing has something to do with just seeing the characters that are actually there rather than a smokescreen, which IMO impedes editability because I can't place the cursor half-way through a ligature and so on

Why wouldn't that be possible? (The cursor thing)

It's still two characters as far as your editor is concerned.

cnity

I didn't say it's not possible, but certainly when I've tried ligatures in the past my editor treated it like a single character until I pressed backspace after the character (or delete before) or whatever. Anyhow there's all sorts of weird functional artefacts that just feel dodgy to me and they basically all arise from this world where multiple runes are being treated as a single one. Here are two more examples:

1. If I `/=` in vim (find equals signs), should the "!=" ligature be highlighted? If it is, then it is misrepresenting the state: if I then press delete, the highlighted ligature won't actually be removed, only part of it will be.

2. In javascript the `===` operator is less visually distinct from `==` since if there are no comparisons nearby I have to judge from the length somehow and it takes longer, adding some additional comprehension fatigue.

Anyway like I said, you can probably try to fix all of these with weird special case handling but it's just fighting the underlying assumption my editor makes (and my brain has learned to understand): a source file is a list of runes. That's how I like to think about it.

Anyone is free to think otherwise. They're just in that other camp and that's OK. But I don't like them personally.

atombender

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 requires putting a "true" at the bottom instead.)

dangets

I do the same, though my muscle memory is `1=1` instead of `true`.

Of course then you get editors/linters/coworkers that always point out that the 'true' is unnecessary. This also doesn't work with ORs (just swap to false), but in practice it seems it is always ANDs that is being used.

fngjdflmdflg

Huh, I've always used "WHERE 1 = 1 AND ...". Using `true` looks more clean.

NoMoreNicksLeft

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.

atombender

You may have missed what I was getting at.

In order to quickly comment out the "foo = 1" here, you cannot simply comment out the whole line because it would become syntactically invalid:

     SELECT ...
     --WHERE foo = 1
       AND bar = 2
I have a single keyboard shortcut to comment/uncomment a line because I like to work briskly with as little unnecessary typing.

It has nothing to do with indentation or being "fugly". I'm talking about interactive exploration of prototyping when the final SQL isn't set in stone.

wruza

Since we're already here, we could think about trailing AND, actually. Look:

  SELECT a, sum(b),
   WHERE foo = 1
     AND GROUP BY a
Sounds pretty SQL to me.

masklinn

That is the haskell workaround, and it also sucks, because it still requires a special non-uniform first value.

I do not want to write either of your snippets, I want to write

  SELECT
      a,
      b,
      c,
  FROM
Because now selected values are uniform and I can move them around or add new ones with minimal changes no matter their position in the sequence.

It’s also completely wonky in many contexts e.g. CREATE TABLE. Trailing commas always works.

> I'd suggest to maybe just change your coding style - and make the start of the list special - instead of the end.

Supporting trailing commas means neither is special.

catapart

Spot on.

Also, to generalize a bit: if a human is expected to read it, the way humans write should be able to be parsed by it. That's subjective, to a point, but it's an easy rule-of-thumb to remember. Trailing commas are so common that people have built workarounds for them. Therefore, they can be understood as "the way humans write". If you're writing a language that you still want to be readable by humans, you really should account for that. And, no shade for it not already being done. I'm just reiterating that there should be NO pushback to allowing trailing commas. It's a completely "common-sense" proposal.

yellowapple

I don't want to write commas at all. I want to write

    SELECT
        a
        b
        c
    FROM d
Or even

    SELECT a b c FROM d
Because now selected values are uniform and there's no superfluous punctuation to worry about.

adornKey

I'd still want to have leading commas.

If the items in the list are long IF(...), maybe uses several lines and maybe contain SELECTs it's hard to see a missing trailing comma. At the start they're all lined up well, and it's very hard to get them wrong.

masklinn

> I'd still want to have leading commas.

Real happy for you. Trailing commas support don’t prevent you from doing that.

skeeter2020

I too prefer leading commas, especially useful when you're prototyping a query. I also picked up the ORM trick of starting your WHERE clause with 1=1 so that every meaningful filter can start with AND ... I'm not as consistent with this one, but it's handy too.

I catch (friendly) flak for my zealot SQL formatting (capitalization, indenting) and know it doesn't impact the execution, but there's something about working in logic / set theory that matches with strict presentation; maybe helps you think with a more rigid mindset?

tezza

I do this as well.

on top i often do a pad entry so that the elements are all on their own line

  SELECT 1 as pad
    , a
    , b
then i can reorder lines trivially without stopping to re-comma the endpoints or maintain which special entry is on the line of the SELECT token

what would be helpful is both LEADING and trailing commas

so I am suggesting:

  SELECT
    , a
    , b
would be permissible too. The parsing step is no longer the difficult portion.

Developer ease leads to less mistakes is my conjecture.

mewpmewp2

Why not something like

  SELECT
      a,
      b,
      c,
  1 as pad FROM
Then?

tezza

cool, that would work too. my preference is leading separators so the separators are all in a visual column. being in a visual column allows the eye to discount the separators easily.

typically names are different lengths and the commas are hard/harder to spot

Your suggestion:

  SELECT
     first_column,
     second_column_wider_a_lot,
     (third + fourth_combined_expression),
  1 as pad FROM
vs my current preference:

  SELECT 1 as pad
     , first_column
     , second_column_wider_a_lot
     , (third + fourth_combined_expression)
  FROM

krembo

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

datadrivenangel

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)

mewpmewp2

I agree, I'd say I usually don't care about aesthetics, but that somehow looks so wrong, I am bothered by it.

datadrivenangel

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

skeeter2020

I do this but I'm skeptical of the causation. I think it might be a symptom of people who are generally more careful with syntax because the formatting means more to them, so they spend time reading the query and moving bits around, which is how they find little typo bugs.

starspangled

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

?

tezza

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

  SELECT 1 as pad
    , a
    , b

computerthings

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

_dain_

The first element is modified less often than the last element. Often it's just an "id" column or something. Comma-first is a net win.

nightpool

Don't let the perfect be the enemy of the good. I'm sure that 99% of the people who are requesting trailing comma support care about select lists. And CREATE TABLE for the rest. Yes, the SQL standard has a huge surface area of custom syntax (rather then most programming languages which have a smaller number of composable atoms), which makes adding "consistent" syntax changes a challenge, but it doesn't mean you need to boil the ocean. Just get trailing comma select lists into a version I'm likely to use in the next decade and I will love you forever!

nerdponx

Most of that additional syntax surface area would also benefit from trailing commas. For example, `SELECT * EXCLUDE (a, b, ...)`, or even `FROM a, b, ...`.

ezekiel68

I love it. I can see no downsides (as long as the updated implementations are well tested).

I reject any argument about how mature SQL is at this point. Just bump the version number (SQL 25) if necessary and make it so.

OccamsMirror

Doesn't feel like a breaking change to me.

willd13

Trailing commas are an abomination

hiccuphippo

Let me start with FROM while we are at it.

nickcw

I feel like this ship has sailed. SQL has been around for more than 50 years and everyone who needs to generate it has already put that extra `if` statement in to suppress trailing commas.

What annoys me far more often is the lack of support for trailing commas in JSON.

dv_dt

I feel the same way about SQL too, it's set and difficult to shift. But I also look at PRQL longingly - https://prql-lang.org/

nightpool

Have you seen the Google BQ pipe syntax? https://cloud.google.com/bigquery/docs/reference/standard-sq...

Feels like it does 75% of what PRQL does while still staying somewhat backwards compatible. Already works in BQ if you opt in.

reddalo

> lack of support for trailing commas in JSON

This, 100%! And the lack of comments.

Cthulhu_

JSON5 allows comments, it's been around since 2012. That said, JSON is not meant for humans / manual editing, and deciding to use it for configuration files was a mistake.

recursive

How am I supposed to make changes to this configuration file?

masklinn

Funny I’m the exact opposite: I essentially never write JSON by hand, or add json content to repositories, so could not care less about the lack of trailing commas, I do semi routinely write or review SQL.

Retr0id

I've been bitten by trailing commas in my python+sql code repeatedly, it's an easy mistake to make when python itself has trailing commas (which I make deliberate use of).

econ

The closing tags should be optional too.

[{},{},,,,{},,

Should be fine. Now you can push things to the eof.

8organicbits

You probably want jsonlines. Being able to open a file in append mode without needing to parse the whole thing is great.

https://jsonlines.org/

orf

OP is massively overthinking it. Add them to CREATE TABLE and SELECT queries would remove 99.9% of annoyances.

jjice

I get where they're coming from though. Having trailing commas work in some common cases but most other cases where there are commas would be weird and definitely result in confusion. More confusion that knowing that trailing commas aren't a thing in SQL, currently.

wruza

To this still empty thread: how about we just stop arguing and add these damn commas everywhere? How about having human- and devenv-oriented languages finally, after how many decades?

probably_wrong

In my opinion: because the Robustness principle [1] is a non-insignificant cause of bugs and SQL injection is a major player in the vulnerability game.

The first case that jumps to me: if you write "a,b,c," you can't know whether you forgot a parameter, you passed an empty string where there shouldn't be one, or you intentionally left a trailing comma. And SQL already is human-oriented - compare your typical SQL to a data operation in K [2]. It just so happens that some things are hard and off-by-one errors are famously near the top of the list [3].

Whenever a database complains that your SQL query contains a trailing comma it's a sign that you may not have paid enough attention to your arguments (or even worse, that you're building queries by hand instead of using prepared statements). From where I stand, not allowing trailing commas is a feature, not a bug, and I would therefore object against them.

[1] "be conservative in what you do, be liberal in what you accept from others" - https://en.wikipedia.org/wiki/Robustness_principle#Criticism

[2] https://news.ycombinator.com/item?id=42999650

[3] http://www.randomprogramming.com/2014/09/quote-of-the-week-h...

boxed

This doesn't seem like it's much of an argument. Injecting `d, g` on the `b` place in your example will work fine. Trailing comma or no.

wruza

If you write "a,b,c" you can't know whether you forgot ",d" either, because if you wanted to type ",d" but got distracted and forgot it, the habit of leaving no trailing comma will lead to just "a,b,c" without ",d". And you can't know whether you forgot or mistyped a whole query either. What is this argument even, seriously? It's so weak that it is on par with suddenly having a stroke while programming. Which guardrails should we invent for that case?

SQL is fundamentally unsafe "forgot the item"-wise. It doesn't allow dynamic column selection, there's no type safety, nothing. If you want guarantees, maybe make a proper language with guarantees rather than justifying random side-effects of a comma being a syntax error sometimes.

yellowapple

Why even have commas at all? This is a non-issue for the likes of Tcl and Lisp and your average shell-scripting language and what have you; whitespace is already a good enough delimiter.

Willamin

Genuinely, I'd love to see this approach be taken: allow for a set of characters to be used as list delimiters. I personally like the set to be comma, semicolon, and newline, but of course this set would need to be varied depending on other syntax (e.g. in SQL, we wouldn't want semicolon to be used for this).

Having newline be a valid list separator is particularly nice because it solves the "trailing comma" and "comma-first" style workarounds in a visually elegant way. The newline already provides a visual separator; we can already tell that we're at the end of most lists by way of having another keyword appear next without needing to rely on a lack of commas, for example:

    select
        id
        name
        email
    from users

eftpotrm

Because there's existing implementations that would interpret that as aliasing column names.

``` SELECT Field AS Renamed, OtherField AS AlsoRenamed ```

and

``` SELECT Field Renamed, OtherField AlsoRenamed ```

are semantically equivalent.

nerdponx

Because in SQL whitespace is already used for all manner of special syntax.

That said, generating SQL from s-expressions can be a very pleasant experience.

netcraft

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.

sunnybeetroot

Wouldn’t just trailing solve your issue?

    SELECT
    a,
    b,
    c,
    From Foo

netcraft

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.

nightpool

It's only easy to miss a comma at the end of a long expression because you need to calculate whether it should be there in the first place. If commas were always required unconditionally, it wouldn't be a problem.

edoceo

Moved the problem to column C

sunnybeetroot

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

fifilura

IIRC BigQuery started allowing trailing commas in queries around 2019.

It all came as a very big surprise, out of thin air.

bobchadwick

That's correct, and I absolutely love it. If I move to a new role where BigQuery is not the data warehouse of choice, I think this will be the thing I'll miss most.

shrikant

Snowflake also allows trailing commas :)

morkalork

It's nice when bigquery and python together for data analysis that little things like that are congruent