Skip to content
SQL errors / “Syntax Error at or Near” — Fix This SQL Error
SQL error

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

The exact 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

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.

The query that fails
select product_name, price
from
where price > 20

Running it raises syntax error at or near.

The corrected query
select product_name, price
from products
where price > 20

Open the corrected query in the playground

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.

Watch out

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

Learn more