The ORDER BY clause sorts query results by one or more columns up to 8,060 bytes. For more information about the maximum ORDER BY clause size, see SELECT.
A sort can be ascending (ASC) or descending (DESC). If neither is specified, ASC is assumed.
This query returns results ordered by ascending pub_id:
Here is the result set:
If more than one column is named in the ORDER BY clause, sorts are nested. The following statement sorts the rows in the titles table, first by publisher in descending order, and then by type in ascending order within each publisher, and finally by price (also ascending, because DESC is not specified).
Here is the result set:
You cannot use ORDER BY on columns that have the text or image data types.
Subqueries, aggregates, and constant expressions are not allowed in the ORDER BY list.
You can however, use a user-specified name in the select list for aggregates or expressions, as in the example below:
FICO® Scores generally range from 300 to 850, with higher scores demonstrating lower credit risk and lower scores demonstrating higher credit risk.