]> begriffs open source - ai-pg/blob - full-docs/txt/queries-order.txt
Convert HTML docs to more streamlined TXT
[ai-pg] / full-docs / txt / queries-order.txt
1
2 7.5. Sorting Rows (ORDER BY) #
3
4    After a query has produced an output table (after the select list has
5    been processed) it can optionally be sorted. If sorting is not chosen,
6    the rows will be returned in an unspecified order. The actual order in
7    that case will depend on the scan and join plan types and the order on
8    disk, but it must not be relied on. A particular output ordering can
9    only be guaranteed if the sort step is explicitly chosen.
10
11    The ORDER BY clause specifies the sort order:
12 SELECT select_list
13     FROM table_expression
14     ORDER BY sort_expression1 [ASC | DESC] [NULLS { FIRST | LAST }]
15              [, sort_expression2 [ASC | DESC] [NULLS { FIRST | LAST }] ...]
16
17    The sort expression(s) can be any expression that would be valid in the
18    query's select list. An example is:
19 SELECT a, b FROM table1 ORDER BY a + b, c;
20
21    When more than one expression is specified, the later values are used
22    to sort rows that are equal according to the earlier values. Each
23    expression can be followed by an optional ASC or DESC keyword to set
24    the sort direction to ascending or descending. ASC order is the
25    default. Ascending order puts smaller values first, where “smaller” is
26    defined in terms of the < operator. Similarly, descending order is
27    determined with the > operator. ^[6]
28
29    The NULLS FIRST and NULLS LAST options can be used to determine whether
30    nulls appear before or after non-null values in the sort ordering. By
31    default, null values sort as if larger than any non-null value; that
32    is, NULLS FIRST is the default for DESC order, and NULLS LAST
33    otherwise.
34
35    Note that the ordering options are considered independently for each
36    sort column. For example ORDER BY x, y DESC means ORDER BY x ASC, y
37    DESC, which is not the same as ORDER BY x DESC, y DESC.
38
39    A sort_expression can also be the column label or number of an output
40    column, as in:
41 SELECT a + b AS sum, c FROM table1 ORDER BY sum;
42 SELECT a, max(b) FROM table1 GROUP BY a ORDER BY 1;
43
44    both of which sort by the first output column. Note that an output
45    column name has to stand alone, that is, it cannot be used in an
46    expression — for example, this is not correct:
47 SELECT a + b AS sum, c FROM table1 ORDER BY sum + c;          -- wrong
48
49    This restriction is made to reduce ambiguity. There is still ambiguity
50    if an ORDER BY item is a simple name that could match either an output
51    column name or a column from the table expression. The output column is
52    used in such cases. This would only cause confusion if you use AS to
53    rename an output column to match some other table column's name.
54
55    ORDER BY can be applied to the result of a UNION, INTERSECT, or EXCEPT
56    combination, but in this case it is only permitted to sort by output
57    column names or numbers, not by expressions.
58
59    ^[6] Actually, PostgreSQL uses the default B-tree operator class for
60    the expression's data type to determine the sort ordering for ASC and
61    DESC. Conventionally, data types will be set up so that the < and >
62    operators correspond to this sort ordering, but a user-defined data
63    type's designer could choose to do something different.