Database Reference
In-Depth Information
Figure 5-5.
Using comparison operators
Sorting Data
After you've filtered the data you want, you can sort the data by one or more columns and in a certain
direction. Since tables are by definition unsorted, the order in which rows is retrieved by a query is
unpredictable. To impose an ordering, you use the
ORDER BY
clause.
ORDER BY <column> [ASC | DESC] {, n}
The
<column>
is the column that should be used to sort the result. The
{, n}
syntax means you can
specify any number of columns separated by commas. The result will be sorted in the order in which you
specify the columns.
The following are the two sort directions:
•
ASC
: Ascending (1, 2, 3, 4, and so on)
•
DESC
: Descending (10, 9, 8, 7, and so on)
If you omit the
ASC
or
DESC
keyword, the sort order defaults to
ASC
. The following is the basic syntax
for queries:
SELECT <column>
FROM <table>
WHERE <predicate>
ORDER BY <column> ASC | DESC
Now that you've seen it, you'll put this syntax to use in an example.