Into Statement  «Prev  Next»
 Lesson 9 Ascending and descending order Objective 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:
 Lastname Firstname PhoneNumber ZIP Code Wynsoup Stephen 520-555-1212 85744 Wynsoup Julie 201-453-7641 85744 Caitlin Elizabeth 444-231-5642 95439 Brennan Jeffrey 123-321-5678 04325 Andrews Julie 309-123-4567 85744

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.

## Ordering Rows

```SELECT * FROM room
WHERE room_no >= 'R3'
ORDER BY capacity
```

## Choice of NULLs

For the purposes of the ORDER BY clause, nulls are considered to be either less than or greater than all non-nulls; the choice is implementation-defined. The first two rows in the result could have been in either order. To ensure that they appear as shown, the ORDER BY clause should be
```ORDER BY capacity, room_no
```

The result is then sorted into room_no within capacity order. Ascending order is assumed by default. Ascending or descending order can be specified by the keywords ASC and DESC, as in:
```ORDER BY capacity ASC, room_no DESC
```

ORDER BY does not, of course, affect the order of the rows in the base table (for which no inherent order should be assumed) but is used to determine the order in which the rows appear in the result table.

### 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.

### Order by Clause - Quiz

Here is a short quiz about ordering data.
Order by Clause - Quiz