When we use the SELECT statement to query the data from a table, the result set is not sorted. To sort the result set, we can use the ORDER BY clause.
The ORDER BY clause allows you to:
- Sort a result set by a single column or multiple columns.
2. Sort a result set by different columns in ascending or descending order.
Syntax:
SELECT column1, column2 ,...FROM table_name ORDER BY column1 [ASC|DESC], column2[ASC|DESC],...
The ASC stands for ascending and the DESC stands for descending. By default, the ORDER BY clause sorts the result set in the ascending order .
Example:
SELECT * FROM student ORDER BY student_name
will displays all the rows in student name wise ascending order.
Example:
SELECT * FROM student ORDER BY course asc, student_name desc
will displays all the rows storted first by course in ascending order, then by student name wise descending order.