Syntax options:
ORDER BY integer
ORDER BY value-expr
Specifies the order of rows for the result table. SQL sorts
the rows from the intermediate result table by the values of
expressions specified in the ORDER BY clause. The intermediate
result table is the result table SQL produces when it evaluates
the preceding clause in the select expression (HAVING, GROUP BY,
WHERE, or FROM).
You can refer to columns in the ORDER BY clause in two ways:
o By a value expression
o By column number, where the integer you specify indicates the
left-to-right position of the column in the result table
You must use an integer to identify a column in the ORDER BY
clause if that column in the select list is derived from a
function, an arithmetic expression, or the result of a UNION,
MINUS, EXCEPT, or INTERSECT operator.
Whether you identify expressions in an ORDER BY clause using a
name or using a number, the expressions are called sort keys.
When you use multiple sort keys, SQL treats the first expression
as the major sort key and successive keys as minor sort keys.
That is, it first sorts the rows into groups based on the first
value expression. Then, it uses the second value expression to
sort the rows within each group, and so on. Unless you specify
a sort key for every column in the result table, rows with
identical values for the last sort key specified will be in
arbitrary order.
The following example illustrates using the ORDER BY clause with
a value expression.
SQL> SELECT * FROM EMPLOYEES
cont> ORDER BY EXTRACT (YEAR FROM BIRTHDAY),
cont> TRIM(FIRST_NAME) || TRIM(LAST_NAME);
00190 O'Sullivan Rick G.
78 Mason Rd. NULL Fremont
NH 03044 M 12-Jan-1923 1 None
00231 Clairmont Rick NULL
92 Madiso7 Drive NULL Chocorua
NH 03817 M 23-Dec-1924 2 None
00183 Nash Walter V.
197 Lantern Lane NULL Fremont
NH 03044 M 19-Jan-1925 1 None
00177 Kinmonth Louis NULL
76 Maple St. NULL Etna
NH 03750 M 7-Apr-1926 1 None
00240 Johnson Bill R.
20 South St NULL Milford
NH 03055 M 13-Apr-1927 2 None
.
.
.