Show TOC

Syntax documentationORDER BY Clause Locate this document in the navigation structure

The ORDER BY clause allows to retrieve the rows of a result set in a specified order. It is good programming style to use an ORDER BY clause only if the database can cheaply do the sorting using a suitable index.

Caution Caution

Sorting NULL values in a result set is database-dependent.

More information: Open SQL Database Dependencies

End of the caution.

Syntax Syntax

  1. <order by clause> ::= 
             ORDER BY <sort specification> 
                           ( ',' <sort specification> )*.
    
    <sort specification> ::= 
             <display name> ( ASC | DESC )?.
    
End of the code.

<display name> is the name of a column in the result set. Ordering with respect to undefined <display name>s is not possible as this column cannot be named in a <sort specification>.

If a <display name> is ambiguous (that is, the same name is used in different <as clause>'s in the same statement) it cannot be used in a <sort specification>.

Example Example

  1. SELECT employee_name, salary AS sal 
                   FROM employees 
                  ORDER BY sal DESC
    
End of the code.

ORDER BY Clause. This query selects the name and the salary of all employees. The result set is sorted descending by the salary.

More Information

Select List