The Exact Error
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
Or in other databases:
ERROR: syntax error at or near "order"
Incorrect syntax near the keyword 'user'
Quick summary: A column or table name in your query is a SQL reserved word. Quote the identifier using the correct syntax for your database ? backticks for MySQL, double quotes for PostgreSQL/SQL Server.
Why This Error Happens
SQL has reserved words (SELECT, FROM, WHERE, ORDER, USER, KEY, VALUE, TABLE, INDEX, etc.) that are part of the language grammar. When the parser encounters them, it tries to interpret them as keywords rather than identifiers.
Three root causes:
1. Reserved word used as identifier ? SELECT order FROM orders ? order is interpreted as a keyword.
2. Future reserved words ? A word not reserved in MySQL 5.x becomes reserved in MySQL 8.x.
3. Database migration ? Moving from MySQL to PostgreSQL which has a different set of reserved words.
Step-by-Step Diagnosis
Step 1 ? Identify the reserved word in the error
The error message names the exact word: near 'order' at line 1. That word needs to be quoted.
Step 2 ? Check your database's reserved word list
-- MySQL:
SELECT WORD, RESERVED FROM information_schema.KEYWORDS WHERE WORD = 'ORDER';
-- PostgreSQL:
SELECT word, resreserved FROM pg_get_keywords() WHERE word = 'order';
Step 3 ? Find all uses in your query
Look at the query around the reported line. Common reserved column names: order, user, key, value, group, index, status, table.
Solutions
Solution 1 ? MySQL: backticks
SELECT `order`, `user`, `key`, `value`
FROM `orders`
WHERE `status` = 'pending';
Solution 2 ? PostgreSQL/SQL Server: double quotes
-- PostgreSQL:
SELECT "order", "user", "key" FROM "orders";
-- SQL Server:
SELECT [order], [user], [key] FROM [orders];
Solution 3 ? Rename the column (cleanest fix)
ALTER TABLE orders RENAME COLUMN `order` TO order_number;
Update all queries after renaming.
Solution 4 ? Fix ORM-generated queries
// Sequelize ? enable backtick quoting for MySQL:
const sequelize = new Sequelize(database, username, password, {
dialect: 'mysql',
define: { quoteIdentifiers: true },
});
Quick Reference
| Database | Quote syntax | Common reserved words |
|---|---|---|
| MySQL | `column` | ORDER, USER, KEY, VALUE, GROUP, INDEX |
| PostgreSQL | "column" | ORDER, USER, VALUE, TABLE, COLUMN, OFFSET |
| SQL Server | [column] | ORDER, USER, KEY, TABLE, INDEX |
Prevent This Error in the Future
1. Adopt a naming convention that avoids reserved words. Suffix with role: user_id, order_ref, group_name.
2. Run your schema through a SQL linter before applying it.
3. Test against the specific database version used in production.
Use ToolNinja to Debug Faster
The SQL Formatter reformats your query with proper indentation, making syntax errors immediately visible. Paste the failing query and the problematic identifier becomes clear.