MySQL Error 1064: SQL Syntax Error

MySQL error 1064 (SQL Syntax Error) explained: what it means, why it happens, and how to fix it — with copy-paste code examples.

MySQL Error Code 1064 MySQL Last verified 2026-08-19

Quick Answer

Read the 'near \'xxx\'' fragment: the error points at the token right after the problem. If that does not apply, fix the highlighted spot: usually a missing comma, unbalanced quote, or reserved word — the full checklist is below.

Error Code

MySQL Error Code: 1064
Official name: SQL Syntax Error
System name: SQL syntax error
Service: MySQL

What does this error mean?

%s near '%s' at line %s

Common Causes

How to Fix

  1. Read the 'near \'xxx\'' fragment: the error points at the token right after the problem
  2. Fix the highlighted spot: usually a missing comma, unbalanced quote, or reserved word
  3. Quote reserved identifiers with backticks: `order`, `group`, `key`
  4. Test the query in the mysql CLI before wiring it into code, and use prepared statements for dynamic SQL
  5. For ORMs, log the final SQL (e.g. mysql2 sql property, SQLAlchemy echo) to see exactly what is sent

Example Scenario

A Laravel app adds a column named rank to a leaderboard table. MySQL 8 reserves the word RANK for window functions, so the generated query SELECT id, rank FROM players fails with ERROR 1064 (42000): You have an error in your SQL syntax; ... near 'rank FROM players'. The quoted fragment in the message is where parsing broke down - the actual mistake is at or just before its first word. The same error fires for a trailing comma in an INSERT column list or MariaDB-only syntax run against MySQL.

Diagnostic Commands

Run these to confirm the cause before applying a fix — each command answers a specific question.

Is the failing word a reserved keyword? sql
SELECT VERSION();
-- then check the keyword list for your version:
-- https://dev.mysql.com/doc/mysql-reserved-words/

Words like rank, groups, system, lead became reserved in 8.0 - code that worked on 5.7 can fail with 1064 on 8.0.

What exact statement did the server receive? sql
-- enable the general log briefly to capture the raw SQL
SET GLOBAL general_log = 'ON';
-- reproduce, then read the log file, then:
SET GLOBAL general_log = 'OFF';

The logged statement shows whether the ORM mangled quotes, commas, or placeholders before sending.

Decision Guide

Parser error points at an identifier?
├─ it is a reserved word → backtick it: `rank` (or rename the column)
└─ typo / missing comma or quote → fix the SQL text

Error near a value / string?
├─ unescaped quote in user input → use bound parameters, never concatenation
└─ empty IN () clause → guard in code: if ids: query(...)

Worked on MariaDB or 5.7, fails on 8.0?
└─ engine-specific syntax (ALTER ... IF NOT EXISTS, new keywords) →
   rewrite for MySQL 8 grammar; do not disable strict/ANSI modes

Code Examples

Minimal repro in the CLI sql
-- paste the failing statement and fix it iteratively:
SELECT id, order_id FROM orders;  -- `order` is reserved, use `orders`
SELECT id, `order` FROM `order`; -- or backtick-quote it

The mysql CLI shows the exact error position; fix it there, then port the corrected SQL back.

Reserved word fix sql
CREATE TABLE `group` (
  id INT PRIMARY KEY
);
SELECT * FROM `group`;

Backticks let you use reserved words as identifiers without renaming the column.

Framework-Specific Fixes

nodejs-mysql2

Log err.sql alongside the message; the snippet shows the exact statement that failed to parse.

catch (err) {
  console.error('SQL failed:', err.sql)
  // locate err.sqlPosition / the message fragment near the typo
}

python-sqlalchemy

Enable echo to print every emitted statement and catch syntax errors before they hit MySQL.

engine = create_engine(DATABASE_URL, echo=True)
# watch the generated SQL, especially for reserved words

php-pdo

Turn on exceptions and log the query string with the PDOException message.

$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
try { $pdo->query($sql); } catch (PDOException $e) {
  error_log($sql . ' :: ' . $e->getMessage());
}

You Might Also Like

Frequently Asked Questions

Why am I seeing MySQL error 1064?

Most often this happens when a typo or missing punctuation in hand-written SQL (commas, quotes, closing parentheses), or when using a reserved word (e.g. order, group, key) without backticks as a table or column name.

How do I fix MySQL error 1064?

Read the 'near \'xxx\'' fragment: the error points at the token right after the problem.

Which frameworks have documented fixes for error 1064?

This page documents fixes for: nodejs-mysql2, python-sqlalchemy, php-pdo.

Official Sources

This page is based on the official MySQL documentation linked below and adds practical troubleshooting guidance on top.