SQL error · The query will not run

The query has a syntax error

Problem: The SQL does not fit the engine's grammar; the cause may sit before the point reported.

Solution: Check commas, quotes and clause order; trim the query down and fix one thing at a time.

Why it happens: the query has a syntax error

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

Self-contained query · SQLite
WITH products(name) AS (VALUES ('Mouse'),('Keyboard'))
SELECT name FROM products ORDER BY name;
Query result
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.

“The query has a syntax error” error messages by engine

The exact wording depends on the database. Compare these common messages with the one in your console.

EngineTypical message
PostgreSQLERROR: syntax error at or near "FROM"
MySQLERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM products' at line 2
SQL ServerMsg 102, Level 15: Incorrect syntax near 'FROM'.
OracleORA-00933: SQL command not properly ended (también ORA-00923: FROM keyword not found where expected)
SQLiteParse error: near "FROM": syntax error

How to fix it in SQL

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