How to Fix MySQL Error 1064 (Syntax Error)

Published: September 11, 2026 | Updated: September 11, 2026

MySQL error 1064 — You have an error in your SQL syntax — is the database's way of saying it could not parse your statement. It is not a permissions problem, not a missing table, not bad data. The query never ran at all: MySQL rejected the text before execution. That also makes it one of the easiest errors to fix, once you know how to read it.

This guide walks through the five causes behind most 1064s — reserved words used as column names, mismatched or smart quotes, plain typos, syntax that your server version does not support, and -- comments missing their space — each with the broken query, the corrected query, and how to spot it fast. At the end, a short workflow for catching SQL syntax errors before they ever reach MySQL.

Table of Contents
  1. How to Read Error 1064 Correctly
  2. Cause 1: Reserved Words as Column or Table Names
  3. Cause 2: Mismatched, Unescaped, and Smart Quotes
  4. Cause 3: Typos, Trailing Commas, and Missing Pieces
  5. Cause 4: Syntax Your MySQL Version Doesn't Support
  6. Cause 5: The -- Comment Space Rule
  7. Preventing Error 1064: A Validation Workflow
  8. FAQ

1. How to Read Error 1064 Correctly

A typical 1064 looks like this:

mysql> SELECT id, name, rank FROM players ORDER BY rank DESC;
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 'rank FROM players ORDER BY rank DESC' at line 1

Two details in that message do all the work:

The 42000 is the SQLSTATE class for syntax errors and access violations — it tells you nothing beyond what 1064 already did. Ignore the "check the manual" sentence; it is boilerplate. Your debugging loop should be: find the quoted fragment, look at the token just before it, and match against the five causes below.

One quirk worth knowing: when the error points near the end of your statement (or shows an empty quote like near ''), the parser ran out of input while still expecting something — an unclosed parenthesis, a missing closing quote, or a truncated statement. The bug is a missing character, not a wrong one.

2. Cause 1: Reserved Words as Column or Table Names

This is the single most common 1064, and it often appears after a MySQL upgrade — the query worked on 5.7 and breaks on 8.0 because 8.0 reserved new words like RANK, GROUPS, LEAD, and SYSTEM.

Broken vs. fixed

-- FAILS on MySQL 8.0 (RANK is reserved)
SELECT id, name, rank FROM players ORDER BY rank DESC;

-- FIXED: backtick-quote the identifier
SELECT id, name, `rank` FROM players ORDER BY `rank` DESC;

-- BETTER FIX: rename the column once, never escape again
ALTER TABLE players CHANGE `rank` player_rank INT;
SELECT id, name, player_rank FROM players ORDER BY player_rank DESC;

The same trap hits table names. order is a classic in e-commerce schemas:

-- FAILS: 'order' is reserved
CREATE TABLE order (id INT PRIMARY KEY, total DECIMAL(10,2));

-- FIXED
CREATE TABLE `order` (id INT PRIMARY KEY, total DECIMAL(10,2));

-- What most teams eventually do: rename it
CREATE TABLE orders (id INT PRIMARY KEY, total DECIMAL(10,2));

Words that bite people most often

IdentifierWhy it breaksReserved since
rankWindow function nameMySQL 8.0
groupsWindow frame keywordMySQL 8.0
lead / lagWindow function namesMySQL 8.0
systemSystem variable keywordMySQL 8.0.3
order, groupORDER BY / GROUP BYAlways
key, conditionIndex / handler keywordsAlways
rows, rangeWindow frame / partition keywordsMySQL 8.0

My recommendation: backticks are the correct fix, but if you find yourself escaping the same column in twenty queries, rename it. Escaped identifiers leak into ORM configs, export scripts, and hand-written reporting SQL where someone will inevitably forget the backticks.

3. Cause 2: Mismatched, Unescaped, and Smart Quotes

MySQL uses three quote characters with different jobs, and mixing them up produces 1064 in several distinct ways.

QuoteCorrect useExample
'single'String literalsWHERE name = 'Alice'
`backtick`Identifiers (tables, columns)SELECT `order` FROM `group`
"double"String literals by default; identifiers only under ANSI_QUOTES modeDepends on server config

Unescaped apostrophe inside a string

-- FAILS: the apostrophe in O'Brien closes the string early
SELECT * FROM users WHERE name = 'O'Brien';

-- FIXED: double the apostrophe
SELECT * FROM users WHERE name = 'O''Brien';

-- FIXED: backslash escape (MySQL-specific)
SELECT * FROM users WHERE name = 'O\'Brien';

-- ACTUALLY FIXED: parameterized query in application code
-- Python:  cursor.execute("SELECT * FROM users WHERE name = %s", (name,))

Quotes around identifiers instead of backticks

-- FAILS: 'users' is a string literal, not a table name
SELECT * FROM 'users';

-- FIXED
SELECT * FROM `users`;   -- or just: SELECT * FROM users;

Smart quotes pasted from chat apps and documents

-- FAILS: these are Unicode curly quotes (U+2018/U+2019), not ASCII apostrophes
SELECT * FROM users WHERE name = ‘Alice’;

-- FIXED: retype the quotes in your editor
SELECT * FROM users WHERE name = 'Alice';

Smart quotes are nasty because they look correct on screen. If a query copied from Slack, Word, or a PDF fails with 1064 near a string literal, delete both quote characters and retype them. Pasting SQL through a plain-text editor or a formatter strips most of these on the way in.

4. Cause 3: Typos, Trailing Commas, and Missing Pieces

The unglamorous majority. Three patterns cover most of them.

Trailing comma before a keyword

-- FAILS: comma before FROM
SELECT id, name, email, FROM users;

-- FIXED
SELECT id, name, email FROM users;

The error will read near 'FROM users' — the parser expected another column expression after the comma and found a keyword instead. Same bug in other shapes:

-- FAILS: trailing comma in column list
INSERT INTO users (id, name,) VALUES (1, 'Alice');

-- FAILS: missing comma between values
INSERT INTO users (id, name) VALUES (1 'Alice');

-- BOTH FIXED
INSERT INTO users (id, name) VALUES (1, 'Alice');

Misspelled keywords

-- FAILS
SELCT * FROM users;
UPDATE users SET active = 1 WHER id = 5;

-- FIXED
SELECT * FROM users;
UPDATE users SET active = 1 WHERE id = 5;

A misspelled first keyword gives near the whole statement. A misspelled keyword mid-query (like WHER) points at the token after it, because MySQL happily reads WHER as... nothing it recognizes, and fails at the next word.

Unclosed parentheses and truncated statements

-- FAILS: missing closing paren
SELECT * FROM users WHERE (id = 5 AND active = 1;

-- FIXED
SELECT * FROM users WHERE (id = 5 AND active = 1);

These produce errors pointing near the end of the statement, which is why the "empty quote" variant (near '') almost always means an unbalanced paren or quote. Count your openers against your closers — or format the query so each nesting level sits on its own line and the imbalance becomes visible.

5. Cause 4: Syntax Your MySQL Version Doesn't Support

Error 1064's message literally says "check the manual that corresponds to your MySQL server version" — because syntax support varies by version. A query can be perfectly valid on MySQL 8.4 and still throw 1064 on 5.7. First, find out what you're actually running:

SELECT VERSION();
-- e.g. 5.7.44-log (no CTEs, no window functions)
-- e.g. 8.0.36      (CTEs and window functions OK)

CTEs and window functions on MySQL 5.7

-- FAILS on 5.7: error near 'WITH active_users AS ...'
WITH active_users AS (
  SELECT * FROM users WHERE active = 1
)
SELECT * FROM active_users;

-- FAILS on 5.7: error near 'OVER (ORDER BY score DESC)'
SELECT name, RANK() OVER (ORDER BY score DESC) AS rnk FROM players;

-- 5.7 WORKAROUND: derived table + user variable for row numbering
SELECT name, @r := @r + 1 AS row_num
FROM players CROSS JOIN (SELECT @r := 0) AS init
ORDER BY score DESC;

Feature-to-version reference

SyntaxMinimum MySQL versionError 1064 points near
CTE (WITH ... AS)8.0WITH
Window functions (OVER)8.0OVER (...
EXCEPT / INTERSECT8.0.31EXCEPT
Expressions in DEFAULT8.0.13DEFAULT (...
Enforced CHECK constraints8.0.16CHECK (...)
JSON functions (JSON_TABLE)8.0JSON_TABLE

The reverse case exists too: MariaDB and MySQL diverged years ago, and syntax copied from a MariaDB tutorial can throw 1064 on MySQL (and vice versa). If a query "should work" per a blog post, check which engine and version that post was written against.

Catch Error 1064 Before MySQL Does

Paste your query into the free SQL validator — parse errors are flagged instantly, with the problem location highlighted, no database connection required.

Validate Your SQL Now →

6. Cause 5: The -- Comment Space Rule

MySQL's double-dash comment has a rule most other databases do not: -- only starts a comment if it is followed by a space (or a control character like a newline). This deviates from the SQL standard on purpose — it prevents 1--5 from being parsed as "1 comment".

-- FAILS: no space after the dashes, MySQL keeps parsing "get"
SELECT * FROM users; --get all users

-- FIXED: add the space
SELECT * FROM users; -- get all users

-- ALSO VALID: MySQL's hash comment
SELECT * FROM users; # get all users

-- ALSO VALID: standard block comment, safe on every engine
SELECT * FROM users; /* get all users */

This bug loves generated SQL and one-liners squeezed into shell commands, where someone strips "unnecessary" whitespace. It also breaks copied code: --TODO: refactor with no space will take down the whole statement. If a query fails with 1064 pointing at text that looks like a comment, check the two dashes.

One more comment gotcha: MySQL treats /*! ... */ as an executable version-gated comment, not a comment at all. If you strip or reformat those blocks with a tool that doesn't understand MySQL, you can delete live SQL or expose syntax the server can't parse.

7. Preventing Error 1064: A Validation Workflow

Fixing 1064s is fast once you can read them. Not getting them is better. This is the loop I use for any SQL that will run somewhere I can't watch it fail:

Step 1: Validate the parse before executing

Paste the statement into the SQL validator. It parses the query and reports syntax problems with a location — the same information 1064 gives you, but before the statement touches a server, and without needing a database connection at all. For migration scripts and stored procedures, this alone catches the trailing commas, unbalanced parens, and misspelled keywords that make up most 1064s.

Step 2: Format it so structure is visible

A 200-character one-liner hides missing commas. Run the query through the MySQL formatter so every clause and list item sits on its own line:

-- Before: where is the bug?
SELECT u.id, u.name, o.total, FROM users u JOIN orders o ON o.user_id = u.id WHERE o.created > '2026-01-01';

-- After formatting: the trailing comma jumps out
SELECT
  u.id,
  u.name,
  o.total,        -- ← trailing comma before FROM
FROM users u
JOIN orders o ON o.user_id = u.id
WHERE o.created > '2026-01-01';

Step 3: Match syntax to your oldest supported version

If production runs 5.7 but your laptop runs 8.4, develop against 5.7 syntax or you will ship CTEs that throw 1064 in production only. Keep the version table from section 5 handy, and run SELECT VERSION(); on every environment — people are regularly surprised by what's actually deployed.

Step 4: Parameterize application queries

Prepared statements with bound parameters eliminate the entire quote-escaping class of 1064 (and SQL injection with it). If your application builds SQL by concatenating strings, that is the real bug; 1064 is just the symptom that occasionally surfaces.

Step 5: Check the plan of anything complex

Once a query parses, run it through EXPLAIN before trusting it in production. It confirms MySQL understood your query the way you meant it — join order, index usage, row estimates — which catches semantic mistakes that 1064 can't.

Quick triage table

Error points near...Most likely causeFix
A column name that's also a keywordReserved wordBackticks or rename
A string with a name in itUnescaped apostrophe or smart quotes'' escape / retype quotes / parameterize
FROM or VALUESTrailing or missing commaFix the list punctuation
OVER (, WITH, EXCEPTSyntax newer than the serverRewrite for your version
Text that looks like a commentMissing space after ---- comment or #
End of statement / empty quoteUnclosed paren or quoteBalance the delimiters

Error 1064 has a reputation as a mystery, but it is the most forthcoming error MySQL produces: it quotes the exact spot where parsing died. Read the fragment, check the token before it, work down the five causes — and validate before you execute so most of them never reach the server in the first place.

FAQ

What does "check the manual that corresponds to your MySQL server version for the right syntax to use near" mean?

It is the standard tail of MySQL error 1064. The quoted fragment after near shows where the parser gave up. The actual mistake is usually at that token or in the one immediately before it, because MySQL parses left to right and only fails when it hits something it cannot continue with.

Why does a simple SELECT query fail with error 1064?

The most common reason is a reserved word used as a column or table name — SELECT id, rank FROM players fails on MySQL 8.0 because RANK became reserved. Other frequent causes: a trailing comma before FROM, an unescaped apostrophe inside a string, or smart quotes pasted from a chat app or word processor.

How do I escape reserved words in MySQL?

Wrap the identifier in backticks: `rank`, `order`, `group`. Backticks work regardless of SQL mode. Double quotes only work as identifier quotes when ANSI_QUOTES mode is enabled. The better long-term fix is renaming the column (rank to player_rank) so every query stops needing escapes.

Can MySQL error 1064 be caused by the server version?

Yes. CTEs (WITH ... AS) and window functions (RANK() OVER) require MySQL 8.0; EXCEPT and INTERSECT require 8.0.31. Running these on MySQL 5.7 produces error 1064 pointing near the new keyword. Check your server with SELECT VERSION(); and rewrite with subqueries or user variables when you must support older versions.

How do I stop error 1064 from reaching production?

Validate before executing: paste the query into an online SQL validator to catch parse errors instantly, format the SQL so missing commas and unclosed parentheses become visible, use parameterized queries in application code to eliminate string-quoting bugs entirely, and test migrations against the oldest MySQL version you support.