Learn how to order your results by ascending and descending order.
SQL Result Set in Ascending and Descending Order
You can add one more option to the ORDER BY clause. You can indicate whether you would like the results to be sorted in ascending (A to Z, 0 to 100) or descending (Z to A, 100 to 0) order.
By simply adding a keyword to the statement, you will sort appropriately. The default is ascending order, as shown in the previous examples.
To indicate descending order, use the following statement:
SELECT * FROM Customer
ORDER BY Lastname DESC, Firstname DESC
The result is the same information, reversed:
You will probably find yourself using ORDER BY frequently as you query your databases. It is very helpful in putting the information into usable shape that can be read.
ORDER BY clause used for PRICE column
Let us take a look at another example to clarify how the ORDER BY clause works.
In the first example, I order the rows based on the PRICE column:
SELECT * FROM COMPACT_DISC_STOCK
WHERE PRICE < 16.00
ORDER BY PRICE;
Notice that the PRICE column is specified in the ORDER BY clause.
Also notice that neither the ASC nor the DESC keyword has been specified, so the ASC keyword will be assumed. If you execute this query, you will receive results similar to the following:
The rows are listed according to the PRICE column. The values in the PRICE column appear in ascending order (lowest price to highest price). Because the WHERE clause was
specified, no rows with prices above 15.99 are included in the query results. Also, since only the PRICE column was included in the ORDER BY, the order of rows that have the same price
is unpredictable. For example, the five rows with a PRICE of 14.99 will all appear before those with a PRICE of 15.99, but those five rows might appear in any order.