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.