A clause used in SQL SELECT statements to sort query results.
SQL can sort query results using the
ORDER BY clause of a
SELECT statement. A SQL statement with an
ORDER BY clause has a form similar to:
ORDER BY column1
ORDER BY column1 will sort the result-set in ascending order. This can be explicitly stated by adding
ASC to the
ORDER BY clause. Query results can also be sorted in descending order by adding
DESC to the
ORDER BY clause:
ORDER by column1 DESC.
Some RDBMS software can also specify whether
NULL values appear first or last in the ordering:
ORDER BY SHIP_DATE NULLS FIRST
Ordering can be done on multiple columns by separating each column by a comma:
ORDER BY column1, column2
You can mix ordering directions per column (
ORDER BY column1 ASC, column2 DESC), however it should be noted that for some RDBMS, this can cause potential performance issues with the query.
One such example is that in MySQL, a query mixing
DESC ordering can no longer use indexes to resolve the result-set order[src].