After a query has produced an output table (after the select list has been processed) it can optionally be sorted. If sorting is not chosen, the rows will be returned in an unspecified order. The actual order in that case will depend on the scan and join plan types and the order on disk, but it must not be relied on. A particular output ordering can only be guaranteed if the sort step is explicitly chosen. Show The SELECT The sort expression(s) can be any expression that would be valid in the query's select list. An example is: SELECT a, b FROM table1 ORDER BY a + b, c; When more than one expression is specified, the later values are used to sort rows that are equal according to the earlier values. Each expression can be followed by an optional The SELECT a, b FROM table1 ORDER BY a + b, c;0 and SELECT a, b FROM table1 ORDER BY a + b, c;1 options can be used to determine whether nulls appear before or after non-null values in the sort ordering. By default, null values sort as if larger than any non-null value; that is, SELECT a, b FROM table1 ORDER BY a + b, c;0 is the default for DESC order, and SELECT a, b FROM table1 ORDER BY a + b, c;1 otherwise. Note that the ordering options are considered independently for each sort column. For example SELECT a, b FROM table1 ORDER BY a + b, c;5 means SELECT a, b FROM table1 ORDER BY a + b, c;6, which is not the same as SELECT a, b FROM table1 ORDER BY a + b, c;7. A SELECT a, b FROM table1 ORDER BY a + b, c;8 can also be the column label or number of an output column, as in: SELECT a + b AS sum, c FROM table1 ORDER BY sum; SELECT a, max(b) FROM table1 GROUP BY a ORDER BY 1; both of which sort by the first output column. Note that an output column name has to stand alone, that is, it cannot be used in an expression — for example, this is not correct: SELECT a + b AS sum, c FROM table1 ORDER BY sum + c; -- wrong This restriction is made to reduce ambiguity. There is still ambiguity if an SELECT a + b AS sum, c FROM table1 ORDER BY sum; SELECT a, max(b) FROM table1 GROUP BY a ORDER BY 1;0 to rename an output column to match some other table column's name.
SELECT a + b AS sum, c FROM table1 ORDER BY sum; SELECT a, max(b) FROM table1 GROUP BY a ORDER BY 1;2, SELECT a + b AS sum, c FROM table1 ORDER BY sum; SELECT a, max(b) FROM table1 GROUP BY a ORDER BY 1;3, or SELECT a + b AS sum, c FROM table1 ORDER BY sum; SELECT a, max(b) FROM table1 GROUP BY a ORDER BY 1;4 combination, but in this case it is only permitted to sort by output column names or numbers, not by expressions. If a single sort specification (one sort-key with associated direction) is identified, the rows are ordered by the values of that sort specification. If more than one sort specification is identified, the rows are ordered by the values of the first identified sort specification, then by the values of the second identified sort specification, and so on. Each sort-key cannot have a data type of CLOB, DBCLOB, BLOB, XML, distinct type on any of these types, or structured type (SQLSTATE 42907). A named column in the select list can be identified by a sort-key that is a simple-integer or a simple-column-name. An unnamed column in the select list must be identified by an simple-integer or, in some cases, by a sort-key-expression that matches the expression in the select list (see details of sort-key-expression). A column is unnamed if the AS clause is not specified and it is derived from a constant, an expression with operators, or a function. Ordering is performed in accordance with comparison rules. If an ORDER BY clause contains decimal floating-point columns, and multiple representations of the same number exist in these columns, the ordering of the multiple representations of the same number is unspecified. The null value is higher than all other values. If the ORDER BY clause does not completely order the rows, rows with duplicate values of all identified columns are displayed in an arbitrary order. simple-column-nameUsually identifies a column of the result table. In this case, simple-column-name must be the column name of a named column in the select list.The simple-column-name can also identify a column name of a table, view, or nested table identified in the FROM clause if the query is a subselect. This includes columns defined as implicitly hidden. An error occurs in the following situations:
Determining which column is used for ordering the result is described under Column names in sort keys in the Notes section. Any column-name within a sort-key-expression must conform to the rules described under Column names in sort keys in the Notes section. There are a number of special cases that further restrict the expressions that can be specified.
Note that this form is not allowed in a fullselect (other than the degenerative form of a fullselect). For example, the following is not valid: The following example is valid:
Notes
|