How can the rows be sorted out in a meaningful way?


For sorting the rows in a meaningful way we can use ORDER BY clause. Suppose we want to sort the rows of the following table −

mysql> Select * from Student;
+--------+--------+--------+
| Name   | RollNo | Grade  |
+--------+--------+--------+
| Gaurav |    100 | B.tech |
| Aarav  |    150 | M.SC   |
| Aryan  |    165 | M.tech |
+--------+--------+--------+
3 rows in set (0.00 sec)

The query below sorted the table by ‘Name’.

mysql> Select * from student order by name;
+--------+--------+--------+
| Name   | RollNo | Grade  |
+--------+--------+--------+
| Aarav  |    150 | M.SC   |
| Aryan  |    165 | M.tech |
| Gaurav |    100 | B.tech |
+--------+--------+--------+
3 rows in set (0.00 sec)

The query below sorted the table by ‘Grade.

mysql> Select * from student order by Grade;
+--------+--------+--------+
| Name   | RollNo | Grade  |
+--------+--------+--------+
| Gaurav |    100 | B.tech |
| Aarav  |    150 | M.SC   |
| Aryan  |    165 | M.tech |
+--------+--------+--------+
3 rows in set (0.00 sec)

Updated on: 22-Jun-2020

34 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements