Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

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.

~5 min readSQL
#mysql#error-1064#syntax#sql
Bold white headline reading ERROR 1064 beside a cyan SQL statement crate broken by an amber syntax chip.

Introduction & Quick Summary

A query that looks correct comes back with:

code
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 1

Quick 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; -- OK

The 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'; -- OK

Key Comparison Table

CauseExample that failsFix
reserved word as identifierSELECT order FROM t`order`
single quote missingWHERE name = ryu'ryu'
trailing commaSELECT a, b, FROM tremove the comma
clause orderWHERE x GROUP BY y ORDER BY z LIMIT 1 OFFSET 2 wrong orderfollow SELECT FROM WHERE GROUP HAVING ORDER LIMIT
function used as keywordSELECT rank() FROM scores on 5.7upgrade or rename
unterminated stringWHERE name = 'ryuclose the quote
placeholder quotedWHERE id IN ('?')IN (?) with a bound list
% inside a format stringLIKE '%s%' after Python %escape as %%
VersionNewly reserved words worth knowingImpact
5.7GENERATED, OPTIMIZER_COSTS, STORED, VIRTUALcolumn names
8.0RANK, DENSE_RANK, ROW_NUMBER, GROUPS, LATERAL, SYSTEMwindow functions and old column names
8.0.14WINDOWwindow clause
MariaDB 10.6OFFSET, LIMIT in some contextspagination aliases
Client or driverWhat it changesSymptom
mysql CLIsends statements verbatimtrue syntax error
PDO with emulated preparesinterpolates values into the SQLquoting bug looks like syntax
JDBC PreparedStatementsends ? to the server'?' quoted by mistake
Python mysqlclient%s placeholders% in LIKE breaks
ORM query builderadds backticks itselfdouble 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.

python
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 meaning

A 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 driver

Expand the list to one placeholder per value, and pass the values as separate parameters:

python
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, ILIKE and RETURNING in older versions are not MySQL syntax at all.
  • Ignoring sql_mode. ANSI_QUOTES and NO_BACKSLASH_ESCAPES change 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 documentation

The SQL documentation that defines the behaviour this guide explains.

Try matching browser tools

All tools

Small utilities that fit SQL work — they run entirely in this browser tab, so nothing you paste leaves your device.

All SQL guides

More in SQL

All SQL guides

The guides filed next to this one in SQL, in library order.