Can we use ORDER BY NULL in MySQL?


Yes, we can do that

Note − Before MySQL 5.7, ORDER BY NULL was useful, but with MySQL 8.0, specifying ORDER BY NULL, for example, at the end to suppress implicit sorting is no longer necessary.

Let us first create a table −

mysql> create table DemoTable
-> (
-> Name varchar(10)
-> );
Query OK, 0 rows affected (1.01 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('John');
Query OK, 1 row affected (0.15 sec)

mysql> insert into DemoTable values('Bob');
Query OK, 1 row affected (0.13 sec)

mysql> insert into DemoTable values('David');
Query OK, 1 row affected (0.17 sec)

mysql> insert into DemoTable values('Sam');
Query OK, 1 row affected (0.08 sec)

mysql> insert into DemoTable values('Mike');
Query OK, 1 row affected (0.13 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

Output

This will produce the following output −

+-------+
| Name  |
+-------+
| John  |
| Bob   |
| David |
| Sam   |
| Mike  |
+-------+
5 rows in set (0.00 sec)

Following is how you can use ORDER BY NULL in MySQL −

mysql> select *from DemoTable order by NULL;

Output

This will produce the following output −

+-------+
| Name  |
+-------+
| John  |
| Bob   | 
| David |
| Sam   |
| Mike  |
+-------+
5 rows in set (0.00 sec)

Updated on: 30-Jun-2020

327 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements