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:
SELECT column1 ,column2 FROM [table] 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.
Ordering can be done on multiple columns by separating each column by a comma:
SELECT column1 ,column2 FROM [table] 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].