Order By clause
- Last Updated: November 2, 2020
- 1 minute read
Purpose
The Order By clause specifies how the rows are to be sorted.
Syntax
ORDER BY sort_expression [DESC | ASC] [,...]
where:
- sort_expression
- is either the name of a column, a column alias, a SQL expression, or the positioned number of the column or expression in the select list to use.
The default is to perform
an ascending (ASC) sort.
Example
To
sort by last_name and then by first_name,
you could use either of the following Select statements:
SELECT emp_id, last_name, first_name FROM emp
ORDER BY last_name, first_name
or
SELECT emp_id, last_name, first_name FROM emp
ORDER BY 2,3
In the second example, last_name is
the second item in the Select list, so ORDER BY 2,3 sorts
by last_name and then by first_name.