How can we get the sorted MySQL output?


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.

Syntax

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

Example

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)

Swarali Sree
Swarali Sree

I love thought experiments.

Updated on: 20-Jun-2020

59 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements