skip to main content
Querying data stores with SQL : Supported SQL and Extensions : Select : From Clause : Order By Clause
  

Try Now
Order By Clause

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 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.

See also

Subqueries
SQL Expressions