“Syntax Error at or Near” — Fix This SQL Error
The parser stopped at the first word it could not use. The quoted token is where it gave up, not always where the mistake is.
- DuckDB
-
Parser Error: syntax error at or near "where" - PostgreSQL
-
ERROR: syntax error at or near "where" - MySQL
-
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 'where price > 20' - SQL Server
-
Incorrect syntax near the keyword 'where'. - Oracle
-
ORA-00936: missing expressionORA-00906: missing left parenthesis is the other usual form. Oracle names the missing piece rather than the token it stopped on.
- SQLite
-
near "where": syntax error
The symptom
You run a query and the database rejects it before touching any data, with a message like syntax error at or near "where". That statement did not execute. Earlier statements in the same submitted batch may already have run, depending on the client and transaction handling. The parser reached a word it could not fit into valid SQL and stopped there.
Why it happens
The quoted word is where the parser gave up, which is usually one step past
the real mistake. In the query below the problem is the empty
FROM clause, but the parser only notices when it reaches
where and finds no table name in front of it.
So read the quoted token, then look at what comes immediately before it. The
common causes are a missing table name, a trailing comma before
FROM, a missing closing parenthesis, a misspelled keyword, and
a reserved word used as an unquoted column name.
The fix
Name the table the query reads from. Once FROM products is
present the parser has a complete clause and the statement runs.
select product_name, price
from
where price > 20
Running it raises
syntax error at or near.
select product_name, price
from products
where price > 20
When the cause is not obvious, cut the query down. Delete clauses from the
bottom up, starting with ORDER BY, then WHERE,
until it parses. The last clause you removed contains the mistake.
A missing closing parenthesis often reports at the end of the statement rather than at the opening bracket, because the parser keeps reading, expecting the expression to finish. If the quoted token is the last word of your query, count the brackets first.
How other engines word it
| Engine | Message |
|---|---|
| DuckDB | Parser Error: syntax error at or near "where" |
| PostgreSQL | ERROR: syntax error at or near "where" |
| MySQL | 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 'where price > 20' |
| SQL Server | Incorrect syntax near the keyword 'where'. |
| Oracle | ORA-00936: missing expression ORA-00906: missing left parenthesis is the other usual form. Oracle names the missing piece rather than the token it stopped on. |
| SQLite | near "where": syntax error |
Related errors
- Fix “Referenced Column Not Found” in SQL
No table in the FROM clause has a column by that name. Usually a typo or a table you did not join.
- Fix “Ambiguous Column Reference” in a SQL JOIN
Both joined tables have that column name, so the database refuses to guess.
- Fix “Different Number of Columns” in a SQL UNION
Each SELECT in a UNION has to return the same number of columns, in the same order.
Learn more
Related
Understand tables, rows, columns, and queries.
Clause order and the shape of a SELECT query.
SELECT, WHERE, ORDER BY, and LIMIT at a glance.
Learn the basic shape of relational data.
Run queries and read results in the browser.
WHERE is evaluated before SELECT aliases exist.