Java Reference
In-Depth Information
24.4.3 ORDER BY Clause
The rows in the result of a query can be sorted into ascending or descending order by using
the optional ORDER BY clause . The basic form of a query with an ORDER BY clause is
SELECT columnName1 , columnName2 , FROM tableName ORDER BY column ASC
SELECT columnName1 , columnName2 , FROM tableName ORDER BY column DESC
where ASC specifies ascending order (lowest to highest), DESC specifies descending order
(highest to lowest) and column specifies the column on which the sort is based. For exam-
ple, to obtain the list of authors in ascending order by last name (Fig. 24.15), use the query
SELECT AuthorID, FirstName, LastName
FROM Authors
ORDER BY LastName ASC
AuthorID
FirstName
LastName
1
Paul
Deitel
2
Harvey
Deitel
3
Abbey
Deitel
5
Michael
Morgano
4
Dan
Quirk
Fig. 24.15 | Sample data from table Authors in ascending order by LastName .
Sorting in Descending Order
The default sorting order is ascending, so ASC is optional. To obtain the same list of au-
thors in descending order by last name (Fig. 24.16), use the query
SELECT AuthorID, FirstName, LastName
FROM Authors
ORDER BY LastName DESC
AuthorID
FirstName
LastName
4
Dan
Quirk
5
Michael
Morgano
1
Paul
Deitel
2
Harvey
Deitel
3
Abbey
Deitel
Fig. 24.16 | Sample data from table Authors in descending order by LastName .
Sorting By Multiple Columns
Multiple columns can be used for sorting with an ORDER BY clause of the form
ORDER BY column1 sortingOrder , column2 sortingOrder ,
where sortingOrder is either ASC or DESC . The sortingOrder does not have to be identical for
each column. The query
 
 
Search WWH ::




Custom Search