What is the use of ORDER BY clause in MySQL?

MySQLMySQLi Database

MySQL ORDER BY clause is used to specify the sorting on the result of a query. The keyword ORDER BY must be followed by the name of the column on which we want to sort. For example, we want to sort the following table named ‘ratelist’ on the basis of column ‘price’ −

mysql> Select * from ratelist;
+----+------+-------+
| Sr | Item | Price |
+----+------+-------+
| 1  | A    |  502  |
| 2  | B    |  630  |
| 3  | C    | 1005  |
| 4  | h    |  850  |
| 5  | T    |  250  | 
+----+------+-------+
5 rows in set (0.05 sec)

The sorting can be done with the help of ORDER BY clause as follows −

mysql> Select * from ratelist ORDER BY Price;
+----+------+-------+
| Sr | Item | Price |
+----+------+-------+
| 5  | T    |  250  |
| 1  | A    |  502  |
| 2  | B    |  630  |
| 4  | h    |  850  |
| 3  | C    | 1005  | 
+----+------+-------+
5 rows in set (0.01 sec)

The query above sorted the table on the basis of price in default sorting order i.e. ascending.

raja
Published on 22-Feb-2018 10:22:00
Advertisements