4
The ORDER BY keyword is used to sort the result-set by column; by default, it sorts the records in ascending order.
6
SQL ORDER BY Syntax: ::
10
ORDER BY column_name(s) ASC|DESC;
16
+---------+------------+----------+----------+--------+
17
|Id |LastName |FirstName |Address | City |
18
+=========+============+==========+==========+========+
19
| 1 | Larson | Sue |3 Cherry | Chicago|
20
+---------+------------+----------+----------+--------+
21
| 2 | Roberts | Teri |21 Brown | Chicago|
22
+---------+------------+----------+----------+--------+
23
| 3 | Peterson | Kari |30 Mell | Reno |
24
+---------+------------+----------+----------+--------+
26
To select all the persons from the table above, and also sort them by their last name, use the following SELECT statement: ::
31
The result-set will look like this:
33
+---------+------------+----------+----------+--------+
34
|Id |LastName |FirstName |Address | City |
35
+=========+============+==========+==========+========+
36
| 1 | Larson | Sue |3 Cherry | Chicago|
37
+---------+------------+----------+----------+--------+
38
| 3 | Peterson | Kari |30 Mell | Reno |
39
+---------+------------+----------+----------+--------+
40
| 2 | Roberts | Teri |21 Brown | Chicago|
41
+---------+------------+----------+----------+--------+
43
ORDER BY DESC can be used to reverse the order of the result set. ::
46
ORDER BY LastName DESC;