In MySQL, how it can be possible to specify a sort order using a column that is not retrieved by the query?


Actually, as we know that we can specify a sort order with the help of the ORDER BY clause. We need to write the ORDER BY keyword followed by the name of the column on which we want to sort the table. It is not necessary that we have to use that column name after the SELECT keyword in the query.

Example

mysql> Select Sr, Item from ratelist ORDER BY Price;
+----+------+
| Sr | Item |
+----+------+
|  5 | T    |
|  1 | A    |
|  2 | B    |
|  4 | h    |
|  3 | C    |
+----+------+
5 rows in set (0.00 sec)

In the example above, we can observe that we need not write the name of the same column after the SELECT keyword which we write in ORDER BY keyword to sort the table. It means that the column itself would not be retrieved by MySQL query on the basis of which we have sorted out the table.

Updated on: 22-Jun-2020

47 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements