ORDER BY clause
- Last Updated: December 12, 2014
- 2 minute read
- OpenEdge
- Version 13.0
- Documentation
Allows ordering of the rows selected by the SELECT statement.
Unless an ORDER BY clause is specified, the rows
of the result set might be returned in an unpredictable order as
determined by the access paths chosen and other decisions made by
the query optimizer. The decisions made will be affected by the
statistics generated from table and index data examined by the UPDATE
STATISTICS command.
Syntax
|
Parameters
- expr
-
Expression of one or more columns of the tables specified in the
FROMclause of theSELECTstatement. - posn
-
Integer column position of the columns selected by the
SELECTstatement. - ASC | DESC
-
Indicates whether to order by ascending order
(ASC)or descending order. The default isASC.
Notes
- The
ORDER BYclause, if specified, should follow all other clauses of theSELECTstatement. - The selected rows are ordered on the basis of the first expr or posn. If the values are the same, then the second expr or posn is used in the ordering.
- A query expression can be followed by an optional
ORDER BYclause. If the query expression contains set operators (such as,UNION), then theORDER BYclause can specify column names, aliases, and positions in the following ways:- An alias from
the first
SELECTstatement of the set operator query expression can be used as theORDER BYkey. In this case, the alias indicates the ordering based on the corresponding column from all the query expressions (SELECTstatements) in the set operator. - A column name from the first
SELECTstatement of the set operator query expression can be explicitly used as anORDER BYkey. In this case, the column name indicates the ordering based on the corresponding column from all the query expressions (SELECTstatements) in the set operator. - An ordinal position number can be specified as an
ORDER BYkey.
- An alias from
the first
- A query expression is considered invalid if the
ORDER BYclause:- Has an expression
- Refers to a column name or alias that is not from the first
SELECTstatement - Has a position number that is greater than the number of columns projected
Examples
ORDER BY clause
The following example demonstrates the ORDER
BY clause in the SELECT statement:
|
ORDER BY clause in query expressions containing set operators
The following examples demonstrate the ORDER
BY clause in query expressions containing set operators:
|