The ORDER BY clause sorts the rows returned in the rowset according to a specified set of criteria.
ORDER BY Sort_Column1 [ ASC | DESC ][,Sort_Column2 [ ASC | DESC ] ] ...
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. |
In the following example:
SELECT Col1, Col2, Col3, Col4, Col5 FROM SCOPE() WHERE Col1 > 10 ORDER BY Col1, Col2 DESC, Col3, Col4, Col5 ASC
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