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.
Syntax
The basic syntax of the ORDER BY clause is as follows −
Parameters or Arguments:
- Expressions
- The columns or calculations that you wish to retrieve.
- Tables
- 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.
- ASC
- Optional. ASC sorts the result set in ascending order by expression. This is the default behavior, if no modifier is provider.
- DESC
- 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.
Output:
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.
Output: