0

SQL noob here. I am trying to learn SQL using the following guide. So here is my simple query answer for the following exercise question

SELECT the report_code, year, quarter, and temperature, where a “quarter” is “Q1”, “Q2”, “Q3”, or “Q4” reflecting months 1-3, 4-6, 7-9, and 10-12 respectively.

SELECT report_code, year, temperature,
CASE
    WHEN month >=1 and month <= 3 then "Q1"
    WHEN month >=4 and month <= 6 then "Q2"
    WHEN month >=7 and month <= 9 then "Q3"
    else "Q4"
END as Quarter
FROM station_data

I see that if I terminate the END as Quarter statement with a , or ;, then SQLiteStudio marks the query as invalid. Which is rather strange to me since the statement obviously needs to be terminated IMHO. Or more than likely I haven't understood what constitutes an SQL "statement" and when do we need to terminate it.

AjB
  • 890
  • 13
  • 34

1 Answers1

0

That's pretty common for queries to not need a semicolon when outside the SQL Shell. Generally they're needed, but not mandatory. For example, when launching a SQL query inside a Python script, it's unnecessary to end the query with ;, because the shell doesn't need to know when it ends. Instead, in the SQL Shell, you are able to write the query piece by piece.

  • I am running these queries in the SQL editor provided by SQLiteStudio. Now talking about semicolons (or SQL terminators in general), SQLiteStudio complains if I _omit_ the `,` at the end of `SELECT report_code, year, temperature,` whereas it complains if it _finds_ a `,` at the end of `END as Quarter` . So there seems to be some kind of rule which I am unable to figure out. It is that which I seek to understand – AjB Jun 28 '22 at 09:52
  • that's strange. after indicating the last column you wish to select, you don't need a ```,```. Same for the ```END as Quarter```. Maybe it's a VSCode bug? – Marco Frag Delle Monache Jun 28 '22 at 10:30
  • Is it because the result of the `case-when` block provides the last column as well as it's value? – AjB Jun 29 '22 at 05:27
  • it could be, but i'm not entirely sure – Marco Frag Delle Monache Jun 29 '22 at 07:16