How can we get the sorted MySQL output?

MySQLMySQLi Database

We know that MySQL SELECT command is used to fetch data from a MySQL table. When you select rows, the MySQL server is free to return them in any order, unless you instruct it otherwise by saying how to sort the result. But, we can sort a result set by adding an ORDER BY clause that names the column or columns which you want to sort.


Select column1, column2,…,columN From table_name ORDER BY column1[column2,…];


In the example below, MySQL returns the result set sorted on the basis of column ‘Name’;

mysql> Select Id, Name, Address from Student ORDER BY Subject;
| Id   | Name    | Address |
| 15   | Harshit | Delhi   |
| 1    | Gaurav  | Delhi   |
| 17   | Raman   | Shimla  |
| 2    | Aarav   | Mumbai  |
4 rows in set (0.00 sec)
Updated on 20-Jun-2020 06:21:03