Order By Clause
- Last Updated: May 15, 2020
- 1 minute read
- DataDirect Connectors
- JDBC
- IBM Db2 5.1
- MySQL 5.1
- Progress OpenEdge 5.1
- SAP Sybase 5.1
- Documentation
Purpose
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 behavior is 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.