Resolve Syntax error near “ORDER BY order DESC” in MySQL?


The word order is a reserved order in MySQL and you have used it in the query. To get rid of the syntax error, you need to use backticks(` `) around the order.

The correct syntax is as follows −

select *from yourTableName ORDER BY `order` DESC;

Let us first create a table −

mysql> create table DemoTable
   (
   `order` int
   );
Query OK, 0 rows affected (0.58 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values(89);
Query OK, 1 row affected (0.19 sec)
mysql> insert into DemoTable values(67);
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable values(90);
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable values(56);
Query OK, 1 row affected (0.13 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

Output

+-------+
| order |
+-------+
| 89    |
| 67    |
| 90    |
| 56    |
+-------+
4 rows in set (0.00 sec)

Following is the query to remove syntax error near ORDER BY −

mysql> select *from DemoTable ORDER BY `order` DESC;

Output

+-------+
| order |
+-------+
| 90    |
| 89    |
| 67    |
| 56    |
+-------+
4 rows in set (0.00 sec)

Updated on: 30-Jul-2019

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements