Κοινή χρήση μέσω


SORT BY clause

Applies to: check marked yes Databricks SQL check marked yes Databricks Runtime

Returns the result rows sorted within each partition in the user specified order. When there is more than one partition SORT BY may return result that is partially ordered. This is different than ORDER BY clause which guarantees a total order of the output.

Syntax

SORT BY { expression [ sort_direction nulls_sort_oder ] } [, ...]

sort_direction
 [ ASC | DEC ]

nulls_sort_order
 [ NULLS FIRST | NULLS LAST ]

Parameters

  • expression

    An expression of any type used to establish a partition local order in which results are returned.

    If the expression is a literal INT value it is interpreted as a column position in the select list.

  • sort_direction

    Specifies the sort order for the sort by expression.

    • ASC: The sort direction for this expression is ascending.
    • DESC: The sort order for this expression is descending.

    If sort direction is not explicitly specified, then by default rows are sorted ascending.

  • nulls_sort_order

    Optionally specifies whether NULL values are returned before/after non-NULL values. If null_sort_order is not specified, then NULLs sort first if sort order is ASC and NULLS sort last if sort order is DESC.

    • NULLS FIRST: NULL values are returned first regardless of the sort order.
    • NULLS LAST: NULL values are returned last regardless of the sort order.

When specifying more than one expression sorting occurs left to right. All rows within the partition are sorted by the first expression. If there are duplicate values for the first expression the second expression is used to resolve order within the group of duplicates and so on. The resulting order not deterministic if there are duplicate values across all order by expressions.

Examples

> CREATE TEMP VIEW person (zip_code, name, age)
    AS VALUES (94588, 'Zen Hui', 50),
              (94588, 'Dan Li', 18),
              (94588, 'Anil K', 27),
              (94588, 'John V', NULL),
              (94511, 'David K', 42),
              (94511, 'Aryan B.', 18),
              (94511, 'Lalit B.', NULL);

-- Use `REPARTITION` hint to partition the data by `zip_code` to
-- examine the `SORT BY` behavior. This is used in rest of the
-- examples.

-- Sort rows by `name` within each partition in ascending manner
> SELECT /*+ REPARTITION(zip_code) */ name, age, zip_code FROM person
    SORT BY name;
   Anil K   27    94588
   Dan Li   18    94588
   John V NULL    94588
  Zen Hui   50    94588
 Aryan B.   18    94511
  David K   42    94511
 Lalit B. NULL    94511

-- Sort rows within each partition using column position.
> SELECT /*+ REPARTITION(zip_code) */ name, age, zip_code FROM person
    SORT BY 1;
   Anil K   27    94588
   Dan Li   18    94588
   John V null    94588
  Zen Hui   50    94588
 Aryan B.   18    94511
  David K   42    94511
 Lalit B. null    94511

-- Sort rows within partition in ascending manner keeping null values to be last.
> SELECT /*+ REPARTITION(zip_code) */ age, name, zip_code FROM person
    SORT BY age NULLS LAST;
   18   Dan Li    94588
   27   Anil K    94588
   50  Zen Hui    94588
 NULL   John V    94588
   18 Aryan B.    94511
   42  David K    94511
 NULL Lalit B.    94511

-- Sort rows by age within each partition in descending manner, which defaults to NULL LAST.
> SELECT /*+ REPARTITION(zip_code) */ age, name, zip_code FROM person
    SORT BY age DESC;
   50  Zen Hui    94588
   27   Anil K    94588
   18   Dan Li    94588
 NULL   John V    94588
   42  David K    94511
   18 Aryan B.    94511
 NULL Lalit B.    94511

-- Sort rows by age within each partition in descending manner keeping null values to be first.
> SELECT /*+ REPARTITION(zip_code) */ age, name, zip_code FROM person
    SORT BY age DESC NULLS FIRST;
 NULL   John V    94588
   50  Zen Hui    94588
   27   Anil K    94588
   18   Dan Li    94588
 NULL Lalit B.    94511
   42  David K    94511
   18 Aryan B.    94511

-- Sort rows within each partition based on more than one column with each column having
-- different sort direction.
> SELECT /*+ REPARTITION(zip_code) */ name, age, zip_code FROM person
    SORT BY name ASC, age DESC;
   Anil K   27    94588
   Dan Li   18    94588
   John V null    94588
  Zen Hui   50    94588
 Aryan B.   18    94511
  David K   42    94511
 Lalit B. null    94511