MySQL 1064: You have an error in your SQL syntax
The message points at the first token MySQL could not parse. Reserved words, quoting and client-side placeholders explain most 1064 errors.

Introduction & Quick Summary
A query that looks correct comes back with:
ERROR 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
'ORDER' at line 1Quick answer: the server's parser stopped at the token named in the message, and everything after it is unparsed. 1064 is a syntax error, not a data or permission error. The most common causes are a reserved word used as an identifier, a quoting mistake, a missing comma, and a placeholder that arrived as a quoted string.
-- fails: ORDER is a reserved word
SELECT id FROM orders WHERE status = 'open' ORDER BY created_at DESC LIMIT 10;
-- same query, identifier quoted
SELECT `order`, id FROM audit WHERE `order` = 1;The text after near is the important part: it starts at the offending token, not at the beginning of the query. Read it, compare it with the clause that should be there, and the fix is usually visible. The rest of this guide covers the reserved-word list that changed in MySQL 8, the quoting rules that differ between MySQL and other databases, and the placeholder problems that appear only when a driver builds the statement.
Core Mechanics, Signatures & Reproducible Setup
The message has three parts: the error code, the manual link mentioning the server version, and near '<token>' at line <n>. The token is where the grammar failed; the line number counts newlines in the submitted statement, which matters for multi-line queries and for migrations that send several statements at once.
SELECT VERSION();
-- 8.0.36
SHOW VARIABLES LIKE 'sql_mode';
-- ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,...A minimal reproduction with a reserved word:
CREATE TABLE audit (id INT PRIMARY KEY, `order` INT);
INSERT INTO audit VALUES (1, 5);
SELECT id, order FROM audit; -- ERROR 1064 near 'order FROM audit'
SELECT id, `order` FROM audit; -- OKThe same statement on a server in ANSI_QUOTES mode behaves differently: double quotes stop being string delimiters and become identifier delimiters, so WHERE name = "ryu" fails with 1064 because the parser expects an identifier there.
SET SESSION sql_mode = CONCAT(@@sql_mode, ',ANSI_QUOTES');
SELECT id FROM users WHERE name = "ryu"; -- ERROR 1064 near '"ryu"'
SELECT id FROM users WHERE name = 'ryu'; -- OKKey Comparison Table
| Cause | Example that fails | Fix |
|---|---|---|
| reserved word as identifier | SELECT order FROM t | `order` |
| single quote missing | WHERE name = ryu | 'ryu' |
| trailing comma | SELECT a, b, FROM t | remove the comma |
| clause order | WHERE x GROUP BY y ORDER BY z LIMIT 1 OFFSET 2 wrong order | follow SELECT FROM WHERE GROUP HAVING ORDER LIMIT |
| function used as keyword | SELECT rank() FROM scores on 5.7 | upgrade or rename |
| unterminated string | WHERE name = 'ryu | close the quote |
| placeholder quoted | WHERE id IN ('?') | IN (?) with a bound list |
% inside a format string | LIKE '%s%' after Python % | escape as %% |
| Version | Newly reserved words worth knowing | Impact |
|---|---|---|
| 5.7 | GENERATED, OPTIMIZER_COSTS, STORED, VIRTUAL | column names |
| 8.0 | RANK, DENSE_RANK, ROW_NUMBER, GROUPS, LATERAL, SYSTEM | window functions and old column names |
| 8.0.14 | WINDOW | window clause |
| MariaDB 10.6 | OFFSET, LIMIT in some contexts | pagination aliases |
| Client or driver | What it changes | Symptom |
|---|---|---|
mysql CLI | sends statements verbatim | true syntax error |
| PDO with emulated prepares | interpolates values into the SQL | quoting bug looks like syntax |
JDBC PreparedStatement | sends ? to the server | '?' quoted by mistake |
Python mysqlclient | %s placeholders | % in LIKE breaks |
| ORM query builder | adds backticks itself | double quoting |
Practical Edge Cases, Error Fixes & Deep Dive Code Examples
1. Start from the token in the message
The parser reports the first token it could not place, which is usually one token after the actual mistake:
near 'FROM users WHERE id = 1' at line 1
Here the problem is the token before FROM — a missing column, a stray comma, or an incomplete expression such as WHERE id =. Rewrite the statement in the smallest form that reproduces it:
SELECT 1 FROM users WHERE id = 1; -- works
SELECT , FROM users WHERE id = 1; -- 1064 near ', FROM'That two-step is faster than reading a 60-line generated query.
2. Reserved words in MySQL 8
Upgrading from 5.7 to 8.0 turns working queries into 1064 errors when a column is named rank, groups, system or window. The column still exists; it now needs quoting in every statement that references it, including INSERT column lists and ORDER BY:
-- 5.7 accepted this; 8.0 rejects it
SELECT rank FROM scores ORDER BY rank DESC;
-- 8.0
SELECT `rank` FROM scores ORDER BY `rank` DESC;Renaming the column is the durable fix, but it requires a migration and every query to change; quoting is the immediate one. Compare the column list against the reserved word list for the exact server version rather than for "MySQL" in general.
3. Quoting strings, identifiers and values
MySQL uses single quotes for strings and backticks for identifiers. Double quotes are strings by default and identifiers under ANSI_QUOTES, which is a common source of queries that work on one server and fail on another:
INSERT INTO users (name, note) VALUES ('Ryu', 'it''s fine'); -- escaped quote
INSERT INTO users (name, note) VALUES ("Ryu", "it's fine"); -- works without ANSI_QUOTES
INSERT INTO `users` (`name`) VALUES ('Ryu');Never build a string with Python % formatting or f-strings around user input; besides the injection risk, a value containing a quote breaks the syntax. Use driver placeholders and let the library quote.
cur.execute("SELECT id FROM users WHERE name = %s", (name,))
cur.execute("SELECT id FROM users WHERE name LIKE %s", (f"%{term}%",))The second form is the correct way to build a LIKE pattern: the wildcard characters live in the parameter, not in the SQL string.
4. Clause order and trailing commas
SQL clauses have a fixed order: SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY, LIMIT, OFFSET. A clause placed out of order produces 1064 near that clause, not a helpful "clause out of order" message:
SELECT id, COUNT(*) FROM events GROUP BY id HAVING COUNT(*) > 1 ORDER BY id;
SELECT id FROM events LIMIT 10 OFFSET 20; -- MySQL 8 and MariaDB 10.6+
SELECT id FROM events LIMIT 20, 10; -- older syntax, same meaningA trailing comma before FROM or before a closing parenthesis is the other frequent typo in hand-edited migrations.
5. Placeholders and lists
A placeholder must stand where a single value is allowed. Quoting it turns it into a string literal containing a question mark, and an IN list cannot be bound as one string:
SELECT * FROM users WHERE id IN ('?'); -- 1064, or zero rows
SELECT * FROM users WHERE id IN (?); -- needs a list expanded by the driverExpand the list to one placeholder per value, and pass the values as separate parameters:
placeholders = ",".join(["%s"] * len(ids))
cur.execute(f"SELECT * FROM users WHERE id IN ({placeholders})", ids)Identifiers cannot be bound at all; a table or column name from user input must be validated against an allow-list and quoted with backticks.
6. Server version and feature mismatch
A statement copied from a newer manual fails on an older server with 1064, because the parser does not know the keyword:
SELECT * FROM t WHERE id = 1 FOR UPDATE SKIP LOCKED; -- 8.0 and MariaDB 10.6+
SELECT JSON_TABLE(doc, '$[*]' COLUMNS (x INT PATH '$.x')) FROM docs; -- 8.0+Check SELECT VERSION() and the manual for that version before assuming a typo. Managed database services sometimes run a different minor version from the one in your local container, which is why a query can pass in development and fail in production. Use the SQL formatter to normalise a generated query before comparing it with the failing one, and the SQL schema designer to check column names against the actual table.
Common Compilation/Runtime Mistakes & Version Gotchas
- Assuming the reported token is the mistake. It is where parsing failed; the mistake is often the token before it.
- Interpolating values into SQL. It breaks on quotes and invites injection; placeholders fix both.
- Copying a query from PostgreSQL.
""identifiers,::casts,ILIKEandRETURNINGin older versions are not MySQL syntax at all. - Ignoring
sql_mode.ANSI_QUOTESandNO_BACKSLASH_ESCAPESchange how the same text parses, and a session-level setting can differ from the server default. - Version notes. MySQL 8.0 added window functions and their reserved words, and made some previously optional words reserved; MariaDB 10.6 added
OFFSET. A dump restored into a newer server can fail on statements that were valid when written. - Multi-statement strings. Sending several statements in one call fails on clients that do not enable multi-statements, and the resulting error points at the start of the second statement rather than at a syntax problem in the first.
Official documentation: MariaDB error code reference and the reserved words list, which matches MySQL closely for this error class.
Official references
primary documentationThe SQL documentation that defines the behaviour this guide explains.
Try matching browser tools
All toolsSmall utilities that fit SQL work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related SQL guides
All SQL guidesSQLite: database is locked (SQLITE_BUSY) under concurrent writes
SQLite allows one writer. Set a busy timeout, switch to WAL, start write transactions as IMMEDIATE, and reduce to a single writer connection.
Postgres: column does not exist (SQLSTATE 42703)
42703 means the identifier you wrote is not the one that exists. Check case folding, alias scope, search_path and migration drift.
pg_dump: server version mismatch, aborting because of server version
pg_dump refuses to read a newer server. Install a matching client, run it from a container or the server host, and match pg_restore too.
MySQL: ERROR 1040 (HY000) Too many connections
The server is out of slots because pools are too large or leaking. Read the counters, size pools against replicas, then raise the limit.
More in SQL
All SQL guidesThe guides filed next to this one in SQL, in library order.