Why is a comma required when creating CASE in SQLite?

Very new SQL user and simply curious if there is a defined reason for this.
Why is a comma required after SELECT column when adding a CASE? Why wouldn’t this require parenthesis instead (or nothing)? Thanks :slight_smile:

i.e.
SELECT column,
…CASE
…WHEN review [etc]
…ELSE [etc]
…END AS [etc]
FROM table;

On looking at grammatically, it is a segue, or interruption in the SELECT FROM clause, as I would see it. Perhaps someone will fill you in on a more factual reason.

1 Like

There are sometimes variations in the SQL implementation between vendors, but I quite like the SQLite docs because of the flowcharts for the syntax and how the queries are assembled.

You’re looking to filter the columns provided in the result of your SELECT statement, so you’re required to provide a comma-separated list of the desired columns. What you’re doing with the CASE statement is creating a value for each record and then presenting it in the output as a new column, which is why there’s a comma between it and any surrounding column(s) being selected from the raw table.

In the link at the top, the relevant diagrams are the expr flow (specifically the CASE row which is near the bottom of that diagram), select-stmt flow and result-column flow.

3 Likes