SQL | ORDER BY
The ORDER BY statement in SQL is used to sort the fetched data in either ascending or descending according to one or more columns.
- The Order by clause by default sorts the retrieved data in ascending order.
- To sort the data in descending order DESC keyword is used with Order by clause.
The basic syntax of the ORDER BY clause is as follows −
Parameters or Arguments:
- The columns or calculations that you wish to retrieve.
- The tables that you wish to retrieve records from. There must be at least one table listed in the FROM clause.
- WHERE conditions
- Optional. Conditions that must be met for the records to be selected.
- Optional. ASC sorts the result set in ascending order by expression. This is the default behavior, if no modifier is provider.
- Optional. DESC sorts the result set in descending order by expression.
Sort according to multiple columns: To sort in ascending or descending order we can use the keywords ASC or DESC respectively. To sort according to multiple columns, separate the names of columns by (,) operator.
- Sort according to single column: In this example we will fetch all data from the table Student and sort the result in descending order according to the column ROLL_NO.
Sort according to single column: In this example we will fetch all data from the table Student and sort the result in descending order according to the column ROLL_NO.