Show TOC

ORDER BY ClauseLocate 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

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

More information: Open SQL Database Dependencies

<order by clause> ::= 
         ORDER BY <sort specification> 
                       ( ',' <sort specification> )*.

<sort specification> ::= 
         <display name> ( ASC | DESC )?.

         

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

Sample Code
SELECT employee_name, salary AS sal 
               FROM employees 
              ORDER BY sal DESC

            

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