- Categories:
ORDER BY¶
Specifies an ordering of the rows of the result table from a SELECT list.
Syntax¶
Sorting by specific columns
Where:
Sorting by all columns
Parameters¶
column_aliasColumn alias appearing in the query block’s SELECT list.
positionPosition of an expression in the SELECT list.
exprAny expression on tables in the current scope.
{ ASC | DESC }Optionally returns the values of the sort key in ascending (lowest to highest) or descending (highest to lowest) order.
Default: ASC
NULLS { FIRST | LAST }Optionally specifies whether NULL values are returned before/after non-NULL values, based on the sort order (ASC or DESC).
Default: Depends on the sort order (ASC or DESC); see the usage notes below for details
ALLSorts the results by all of the columns specified in the SELECT list. The results are sorted by the columns in the order in which they appear.
For example, suppose that the SELECT list contains:
The results are sorted first by
col_1, then bycol_2, and then bycol_3.Note
You cannot specify ORDER BY ALL if a column in the SELECT list uses an aggregate function.
Usage notes¶
All data is sorted according to the numeric byte value of each character in the ASCII table. UTF-8 encoding is supported.
For numeric values, leading zeros before the decimal point and trailing zeros (
0) after the decimal point have no effect on sort order.
When NULLS FIRST or NULLS LAST isn’t specified, the ordering of NULL values depends on the setting of the DEFAULT_NULL_ORDERING parameter and the sort order:
When the sort order is ASC (the default) and the DEFAULT_NULL_ORDERING parameter is set to
LAST(the default), NULL values are returned last. Therefore, unless specified otherwise, NULL values are considered to be higher than any non-NULL values.When the sort order is ASC and the DEFAULT_NULL_ORDERING parameter is set to
FIRST, NULL values are returned first.When the sort order is DESC and the DEFAULT_NULL_ORDERING parameter is set to
FIRST, NULL values are returned last.When the sort order is DESC and the DEFAULT_NULL_ORDERING parameter is set to
LAST, NULL values are returned first.
The sort order isn’t guaranteed to be consistent for values of different data types in semi-structured data, such as an array that contains elements of different data types.
Top-K pruning can improve the performance of queries that include both LIMIT and ORDER BY clauses. For more information, see Top-K pruning for improved query performance.
An ORDER BY can be used at different levels in a query, for example in a subquery or inside an OVER() subclause. An ORDER BY inside a subquery or subclause applies only within that subquery or subclause. For example, the ORDER BY in the following query orders results only within the subquery, not the outermost level of the query:
In this example, the ORDER BY is specified in the subquery, so the subquery returns the names in order of monthly sales. The ORDER BY in the subquery does not apply to the outer query. This query returns the names of the three branches that had the highest monthly sales, but not necessarily in order by monthly sales.
Sorting can be expensive. If you want the results of the outer query sorted, use an
ORDER BYclause only at the top level of the query, and avoid usingORDER BYclauses in subqueries unless necessary.
Examples¶
The following examples demonstrate how to use ORDER BY to sort the results:
Sorting by string values¶
The following example sorts the results by string values:
Sorting by numeric values¶
The following example sorts the results by numeric values:
Sorting NULLS first or last¶
The following example configures all queries in the session to sort NULLS last by setting the DEFAULT_NULL_ORDERING
parameter to LAST.
The following example overrides the DEFAULT_NULL_ORDERING parameter by specifying NULLS FIRST in a query:
The following example sets the DEFAULT_NULL_ORDERING parameter to FIRST to sort NULLS first:
The following example overrides the DEFAULT_NULL_ORDERING parameter by specifying NULLS LAST in a query:
Sorting by all columns in the SELECT list¶
To run the examples in this section, create the following table:
The following example sorts the results by all columns in the table:
As shown below, the results are sorted first by the a column, then by the s column, and then by the b column (the
order in which the columns were defined in the table).
The following example sorts the results in ascending order:
The following example sets the DEFAULT_NULL_ORDERING parameter to sort NULL values last for all queries executed during the session:
The following example specifies NULLS FIRST in a query to override that setting:
The following example returns the columns in the order b, s, and a. The results are sorted first by b, then by
s, and then by a: