ORDER BY Clause

The ORDER BY clause sorts the rows returned in the rowset according to a specified set of criteria.

Syntax

ORDER BY Sort_Column1 [ ASC | DESC ][,Sort_Column2 [ ASC | DESC ] ] ...

Elements

Sort_Column   Specifies the name of the column to be sorted or the ordinal position of the column.
   
ASC | DESC   Specifies the sorting order, either ascending (ASC) or descending (DESC). If you do not specify the sort order, the columns are sorted in ascending order. However, if a column is explicitly marked ascending or descending, succeeding columns will use that same sort order until another column in the list is explicitly marked in the other order.

Example

  1. In the following example:

    • The order of Col1 is ascending (by default).
    • The order of Col2 is descending (explicitly stated).
    • The order of Col3 and Col4 is descending (implicit, same as last keyword).
    • The order of Col5 is ascending (explicitly stated).
    SELECT Col1, Col2, Col3, Col4, Col5
    FROM SCOPE()
    WHERE Col1 > 10
    ORDER BY Col1, Col2 DESC, Col3, Col4, Col5 ASC
  2. The following example is equivalent to the previous example, but refers to the columns by their ordinal position.

    SELECT Col1, Col2, Col3, Col4, Col5
    FROM SCOPE()
    WHERE Col1 > 10
    ORDER BY 1, 2 DESC, 3, 4, 5 ASC

© 1997 by Microsoft Corporation. All rights reserved.