1994.4.16
by Marisa Plumb
new function and clause files, some placeholders for right now |
1 |
Order By
|
1994.4.19
by Marisa Plumb
new orderby doc |
2 |
========
|
3 |
||
4 |
The ORDER BY keyword is used to sort the result-set by column; by default, it sorts the records in ascending order. |
|
5 |
||
6 |
SQL ORDER BY Syntax: ::
|
|
7 |
||
8 |
SELECT column_name(s)
|
|
9 |
FROM table_name
|
|
10 |
ORDER BY column_name(s) ASC|DESC;
|
|
11 |
||
12 |
**ORDER BY Example**
|
|
13 |
||
14 |
The "Persons" table: |
|
15 |
||
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 |
+---------+------------+----------+----------+--------+ |
|
25 |
||
26 |
To select all the persons from the table above, and also sort them by their last name, use the following SELECT statement: ::
|
|
27 |
||
28 |
SELECT * FROM Persons
|
|
29 |
ORDER BY LastName;
|
|
30 |
||
31 |
The result-set will look like this: |
|
32 |
||
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 |
+---------+------------+----------+----------+--------+ |
|
42 |
||
43 |
ORDER BY DESC can be used to reverse the order of the result set. ::
|
|
44 |
||
45 |
SELECT * FROM Persons
|
|
46 |
ORDER BY LastName DESC;
|