Sharon Ben-Moshe is the founder of sqlfmt, a browser-based SQL formatter and validator with misspelled-keyword suggestions across PostgreSQL, MySQL, SQLite, SQL Server, and BigQuery.
SQLite's 'near X: syntax error' points just past the real mistake. Here are 7 common causes—reserved words, quote confusion, version gaps—with fixes.
Quick answer
SQLite's 'near X: syntax error' names the token where the parser gave up — almost always one step after the real mistake. Common causes include a reserved word used as a column name (wrap it in double quotes), a string literal surrounded by double quotes instead of single quotes, or a missing comma in your SELECT list. Work backward from the named token to find the actual error.
FAQs
Why does SQLite's error message point to the wrong location?
SQLite's parser reads left to right and reports the token where it could no longer continue — not the token that caused the problem. By the time it hits an unexpected token, the earlier mistake has already sent it down the wrong parsing path. The real error is almost always one or two tokens before the one named in the message.
What is the difference between single quotes and double quotes in SQLite?
Single quotes delimit string literals ('hello') and double quotes delimit identifiers such as column and table names ("order"). MySQL also accepts double quotes for strings, which causes confusion when SQL written for MySQL runs against SQLite. Always use single quotes for string values in SQLite.
How do I use a reserved word as a column name in SQLite?
Wrap it in double quotes in every query that references it: SELECT "order", "group" FROM sales. SQLite's full list of reserved words is at sqlite.org/lang_keywords.html. The cleanest long-term fix is to rename the column to something that is not reserved — for example, order_num instead of order — so you never need the quoting.
Does SQLite support the RETURNING clause?
Yes, but only from version 3.35.0 released in March 2021. If you see 'near "RETURNING": syntax error', run SELECT sqlite_version() to check your version. On older versions, use last_insert_rowid() for the inserted row ID or a follow-up SELECT for other columns.
What is the correct clause order in a SQLite SELECT?
The required order is SELECT, FROM, JOIN, WHERE, GROUP BY, HAVING, ORDER BY, LIMIT, OFFSET. Placing any clause before the one it must follow — such as ORDER BY before HAVING, or HAVING before GROUP BY — produces 'near X: syntax error' where X is the first keyword of the out-of-order clause.
Try it now
Paste your SQL into sqlfmt — format, validate, and catch typos free in your browser.
SQLite's "near X: syntax error" is one of the most deceptive error messages in SQL. The token it names is almost never where you went wrong — it is where the parser gave up, which is nearly always one token after the real mistake.
PostgreSQL has a similar error with a line number to guide you. SQLite gives you nothing but the offending token, which makes debugging harder. This post covers the seven most common causes, with before-and-after SQL for each.
SQLite-specific quirks — especially around quotes and version support — are called out explicitly. Some of these causes won't bite you in PostgreSQL or MySQL but are unique sources of confusion in SQLite.
What 'near X: syntax error' actually means
SQLite reads your query left to right, one token at a time. When it encounters a token that cannot legally follow what it has already read, it stops and reports that token as the problem. The earlier part of the query looked valid up to that point — the parser had no reason to complain until something broke its expectations.
The practical rule: when you see near "X": syntax error, look at the code immediately before X — not at X itself.
1. A reserved word used as a column or table name
SQLite reserves a long list of words for its own syntax: ORDER, GROUP, INDEX, TABLE, and dozens more. If you use one as a column or table name without quoting it, the parser sees a keyword where it expected an identifier — and reports the next token as the problem. The full list is at the SQLite keywords reference.
Error: reserved word used as a column name
-- near "order": syntax errorSELECT id, name, order FROM orders;
Fixed: wrap the reserved word in double quotes
SELECT id, name, "order" FROM orders;
SQLite uses double quotes for identifiers and single quotes for string values. If the column name is one you chose, the cleanest fix is to rename it — for example, order_num instead of order. Misspelled keywords cause a related but different class of error; see our guide to common SQL keyword typos.
2. Double quotes around a string literal
In MySQL, both 'value' and "value" are string literals. In SQLite they are not equivalent: single quotes are for strings, double quotes are for identifiers. This is documented as one of SQLite's known quirks. When SQLite sees a double-quoted value and finds no column with that name, it may produce a syntax error or silently treat it as NULL — both are bad.
If you write SQL primarily in MySQL and run it against SQLite — common in cross-platform apps and local testing — scanning for double-quoted string literals is the first thing to check. It is the most common mistake for developers moving between the two databases.
3. A missing comma in the column list
When a comma is missing between column names in a SELECT list, SQLite sees two consecutive identifiers where it expected a comma or keyword. It reports the second identifier as the error token — so the error message names the correct column, but the actual problem is the missing comma before it.
The same pattern appears in CREATE TABLE statements. If a column definition is missing its trailing comma, the next column's name is reported as unexpected. A table with N columns needs exactly N−1 commas between definitions.
4. SQL syntax your SQLite version doesn't support
SQLite ships new SQL features incrementally. If your query uses syntax added after the version you have installed, SQLite produces a syntax error on the unsupported keyword. The SQLite changelog lists exactly when each feature landed. Key additions to know:
NULLS FIRST / NULLS LAST — added in 3.30.0 (October 2019)
STRICT tables — added in 3.37.0 (November 2021)
UNIXEPOCH() and modern date functions — added in 3.38.0 (February 2022)
Check your installed version with SELECT sqlite_version();. If upgrading is not an option, rewrite the query to avoid the unsupported feature.
5. A malformed DELETE or UPDATE statement
Both DELETE and UPDATE require the table name as part of the statement. Dropping it produces a syntax error because the parser encounters WHERE or SET before it expects a table name.
6. Unmatched parentheses in subqueries or functions
An extra or missing parenthesis sends the parser into confusion far downstream. The reported token may be a keyword several lines below the actual imbalance, because the parser kept reading — in the wrong structural context — until it hit something it could not place at all.
When the error token looks completely unrelated to your recent change, count all parentheses in the full query. Every opening paren must have a matching close.
7. SQL clauses in the wrong order
SQL has a strict clause order: SELECT → FROM → JOIN → WHERE → GROUP BY → HAVING → ORDER BY → LIMIT. Placing any clause before the one it must follow causes a syntax error where the misplaced clause's first keyword becomes the reported token.
Remember: WHERE filters rows before grouping, HAVING filters groups after. Both are valid depending on what you are filtering — but ORDER BY must always come after both.
Checklist: debugging 'near X: syntax error' in SQLite
Note the token X. Look at the code immediately before X — the mistake is almost never at X itself.
Check whether X is a reserved word used as a column or table name. If so, wrap it in double quotes.
Verify all string literals use single quotes, not double quotes.
Count the commas in your SELECT list and CREATE TABLE column definitions.
Run SELECT sqlite_version(); and confirm you are not using a feature added after your installed version.
For DELETE and UPDATE, verify the table name appears between the opening keyword and WHERE or SET.
Count parentheses — every opening paren must have a matching close.
Verify clause order: SELECT → FROM → JOIN → WHERE → GROUP BY → HAVING → ORDER BY → LIMIT.
Wrapping up
SQLite's 'near X: syntax error' always points one step past the real problem. In the vast majority of cases you are looking at a reserved-word identifier, a double-quoted string literal, a missing comma, or a version mismatch. Work through the checklist above — most SQLite syntax errors resolve at step one, two, or three.
If you write SQL across multiple dialects, sqlfmt formats and validates SQLite, PostgreSQL, MySQL, SQL Server, and BigQuery queries — catching misspelled keywords and structural issues before you hit a runtime error.