Why it happens: the query has a syntax error
SELECT name,
FROM products;The trailing comma expects another column before FROM. The message may point at FROM even though the problem is just before it.
Check the result with small data
Start by isolating a valid SELECT over two rows.
WITH products(name) AS (VALUES ('Mouse'),('Keyboard'))
SELECT name FROM products ORDER BY name;| name |
|---|
| Keyboard |
| Mouse |
The simple query returns two names in alphabetical order. If you put back «SELECT name, FROM ...», the comma with no column after it causes an error before any data is read.
Check your understanding: Where does the problem start: at FROM or at the comma before it?
Show answer
At the preceding comma. The engine expects another column after the comma and only notices when it reaches FROM, which is why it points at FROM.
How to fix it in SQL
SELECT name
FROM products;Close quotes and parentheses, separate columns with commas and put the clauses in the order your engine expects. Then add back each part of the original query.
How to check it
Run the minimal fixed version on the same engine and add the other clauses back one at a time.
How to avoid it next time
A valid query can still return the wrong data: syntax and logic are separate problems.
Related cases worth checking
Engines share most of SQL, but not every extension. A syntax error can also appear when you copy a clause supported by another product or another version. Before reshuffling the query at random, check the syntax in your engine's documentation.
Rule of thumb: if the structure looks right and the error appears at one particular word, first check whether that word or variant exists in your version.