You can also ORDER BY two or more columns, which creates
a nested sort. The default is still ascending, and the
column that is listed first in the ORDER BY clause takes precedence.
The
following query and Query and
the corresponding query results show nested sorts. To modify the order
in which selected data is displayed, change the order of the two columns
that are named in the ORDER BY clause. Figure 1: Query
SELECT stock_num, manu_code, description, unit_price
FROM stock
ORDER BY manu_code, unit_price;
In the query result, the manu_code column data
appears in alphabetical order and, within each set of rows with the
same manu_code (for example, ANZ, HRO), the unit_price is
listed in ascending order. Figure 2: Query result
The following query shows the reverse order of the columns
in the ORDER BY clause. Figure 3: Query
SELECT stock_num, manu_code, description, unit_price
FROM stock
ORDER BY unit_price, manu_code;
In the query result, the data appears in ascending order
of unit_price and, where two or more rows have the same unit_price (for
example, $20.00, $48.00, $312.00), the manu_code is in alphabetical
order. Figure 4: Query
result
The order of the columns in the ORDER BY clause is important,
and so is the position of the DESC keyword. Although the statements
in the following query contain the same components in the ORDER BY
clause, each produces a different result (not shown). Figure 5: Query
SELECT * FROM stock ORDER BY manu_code, unit_price DESC;
SELECT * FROM stock ORDER BY unit_price, manu_code DESC;
SELECT * FROM stock ORDER BY manu_code DESC, unit_price;
SELECT * FROM stock ORDER BY unit_price DESC, manu_code;