ORDER BY 子句ORDER BY clause

以用户指定顺序按排序方式返回结果行。Returns the result rows in a sorted manner in the user specified order. SORT by 子句不同,此子句保证输出中的总顺序。Unlike the SORT BY clause, this clause guarantees a total order in the output.

语法Syntax

ORDER BY { expression [ sort_direction | nulls_sort_oder ] [ , ... ] }

参数Parameters

  • ORDER BYORDER BY

    以逗号分隔的表达式列表,以及 sort_direction 用于对行进行排序的可选参数和 nulls_sort_orderA comma-separated list of expressions along with optional parameters sort_direction and nulls_sort_order which are used to sort the rows.

  • sort_directionsort_direction

    根据需要指定是否按升序或降序对行进行排序。Optionally specifies whether to sort the rows in ascending or descending order. 排序方向的有效值 ASC 为升序和 DESC 降序。The valid values for the sort direction are ASC for ascending and DESC for descending. 如果未显式指定排序方向,则默认情况下,行按升序排序。If sort direction is not explicitly specified, then by default rows are sorted ascending.

    语法:[ ASC | DESC ]Syntax: [ ASC | DESC ]

  • nulls_sort_ordernulls_sort_order

    选择性地指定是否在非 NULL 值之前/之后返回 NULL 值。Optionally specifies whether NULL values are returned before/after non-NULL values. 如果 null_sort_order 未指定,则在排序顺序为时首先为 null,如果 ASC 排序顺序为,则为最后一个值排序 DESCIf null_sort_order is not specified, then NULLs sort first if sort order is ASC and NULLS sort last if sort order is DESC.

    1. 如果 NULLS FIRST 指定了,则将首先返回 NULL 值,而不考虑排序顺序。If NULLS FIRST is specified, then NULL values are returned first regardless of the sort order.
    2. 如果 NULLS LAST 指定了,则将最后返回 NULL 值,而不考虑排序顺序。If NULLS LAST is specified, then NULL values are returned last regardless of the sort order.

    语法:[ NULLS { FIRST | LAST } ]Syntax: [ NULLS { FIRST | LAST } ]

示例Examples

CREATE TABLE person (id INT, name STRING, age INT);
INSERT INTO person VALUES
    (100, 'John', 30),
    (200, 'Mary', NULL),
    (300, 'Mike', 80),
    (400, 'Jerry', NULL),
    (500, 'Dan',  50);

-- Sort rows by age. By default rows are sorted in ascending manner with NULL FIRST.
SELECT name, age FROM person ORDER BY age;
+-----+----+
| name| age|
+-----+----+
|Jerry|null|
| Mary|null|
| John|  30|
|  Dan|  50|
| Mike|  80|
+-----+----+

-- Sort rows in ascending manner keeping null values to be last.
SELECT name, age FROM person ORDER BY age NULLS LAST;
+-----+----+
| name| age|
+-----+----+
| John|  30|
|  Dan|  50|
| Mike|  80|
| Mary|null|
|Jerry|null|
+-----+----+

-- Sort rows by age in descending manner, which defaults to NULL LAST.
SELECT name, age FROM person ORDER BY age DESC;
+-----+----+
| name| age|
+-----+----+
| Mike|  80|
|  Dan|  50|
| John|  30|
|Jerry|null|
| Mary|null|
+-----+----+

-- Sort rows in ascending manner keeping null values to be first.
SELECT name, age FROM person ORDER BY age DESC NULLS FIRST;
+-----+----+
| name| age|
+-----+----+
|Jerry|null|
| Mary|null|
| Mike|  80|
|  Dan|  50|
| John|  30|
+-----+----+

-- Sort rows based on more than one column with each column having different
-- sort direction.
SELECT * FROM person ORDER BY name ASC, age DESC;
+---+-----+----+
| id| name| age|
+---+-----+----+
|500|  Dan|  50|
|400|Jerry|null|
|100| John|  30|
|200| Mary|null|
|300| Mike|  80|
+---+-----+----+