ORDER BY
constructsupportedQueries & DMLSpecifies an ordering of the rows of the result table from a SELECT list.
Syntax
supportedpartialnot supportednot scored
Sorting by specific columns
SELECT ...
FROM ...
ORDER BY orderItem [ , orderItem , ... ]
[ ... ]
Where
orderItem ::= { <column_alias> | <position> | <expr> } [ { ASC | DESC } ] [ NULLS { FIRST | LAST } ]
Sorting by all columns
SELECT ...
FROM ...
ORDER BY ALL [ { ASC | DESC } ] [ NULLS { FIRST | LAST } ]
[ ... ]
Parameters
| Name | Required | Description |
|---|---|---|
column_alias | Column alias appearing in the query block’s SELECT list. | |
position | Position of an expression in the SELECT list. | |
expr | Any 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 | |
ALL | Sorts 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:Copy code SELECT col_1, col_2, col_3 FROM my_table ORDER BY ALL; The results are sorted first by col_1, then by col_2, and then by col_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 clause can be used at different levels in a query, such as in a subquery or inside an OVER() clause for a window function. An ORDER BY clause inside a subquery or an OVER() clause applies only in that context. For example, the ORDER BY clause in the following query orders results only within the subquery, not the outermost level of the query: Copy code SELECT * FROM ( SELECT branch_name FROM branch_offices ORDER BY monthly_sales DESC LIMIT 3 ); In this example, the ORDER BY clause is specified in the subquery, so the subquery returns the names in order of monthly sales. The ORDER BY clause 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 BY clause only at the top level of the query, and avoid using ORDER BY clauses in subqueries unless necessary. Similarly, when ORDER BY and LIMIT (or FETCH) clauses are at different nesting levels, results can be unpredictable. For details and examples, see the LIMIT / FETCH usage notes.
Parameters
column_alias: Column alias appearing in the query block’s SELECT list. position: Position of an expression in the SELECT list. expr: Any 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 ALL: Sorts 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:Copy code SELECT col_1, col_2, col_3 FROM my_table ORDER BY ALL; The results are sorted first by col_1, then by col_2, and then by col_3. Note You cannot specify ORDER BY ALL if a column in the SELECT list uses an aggregate function.