In the preceding examples, the query for selected rows are displayed in no particular order. We can also select the rows to display in an ordered format using ORDER BY Clause.
The following example query will sort the rows in an ascending order based on the marks.
mysql> select name, marks from student order by marks;
+---------+-------+
| name | marks |
+---------+-------+
| michael | 75 |
| steve | 75 |
| anne | 80 |
| jack | 82 |
| david | 98 |
| mille | 98 |
| steve | 100 |
| anne | 100 |
+---------+-------+
8 rows in set (0.03 sec)
We can also sort the orders in descending order. In the below example query the marks are sorted in descending order.
mysql> select name, marks from student order by marks desc;
+---------+-------+
| name | marks |
+---------+-------+
| steve | 100 |
| anne | 100 |
| david | 98 |
| mille | 98 |
| jack | 82 |
| anne | 80 |
| michael | 75 |
| steve | 75 |
+---------+-------+
8 rows in set (0.00 sec)
Next we can see how to sort the multiple columns.
|