SORT BY Clause

Description

The SORT BY clause is used to return 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

  1. SORT BY { expression [ sort_direction | nulls_sort_order ] [ , ... ] }

Parameters

  • SORT BY

    Specifies a comma-separated list of expressions along with optional parameters sort_direction and nulls_sort_order which are used to sort the rows within each partition.

  • sort_direction

    Optionally specifies whether to sort the rows in ascending or descending order. 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.

    Syntax: [ ASC | DESC ]

  • 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.

    1. If NULLS FIRST is specified, then NULL values are returned first regardless of the sort order.
    2. If NULLS LAST is specified, then NULL values are returned last regardless of the sort order.

    Syntax: [ NULLS { FIRST | LAST } ]

Examples

  1. CREATE TABLE person (zip_code INT, name STRING, age INT);
  2. INSERT INTO person VALUES
  3. (94588, 'Zen Hui', 50),
  4. (94588, 'Dan Li', 18),
  5. (94588, 'Anil K', 27),
  6. (94588, 'John V', NULL),
  7. (94511, 'David K', 42),
  8. (94511, 'Aryan B.', 18),
  9. (94511, 'Lalit B.', NULL);
  10. -- Use `REPARTITION` hint to partition the data by `zip_code` to
  11. -- examine the `SORT BY` behavior. This is used in rest of the
  12. -- examples.
  13. -- Sort rows by `name` within each partition in ascending manner
  14. SELECT /*+ REPARTITION(zip_code) */ name, age, zip_code FROM person SORT BY name;
  15. +--------+----+--------+
  16. | name| age|zip_code|
  17. +--------+----+--------+
  18. | Anil K| 27| 94588|
  19. | Dan Li| 18| 94588|
  20. | John V|null| 94588|
  21. | Zen Hui| 50| 94588|
  22. |Aryan B.| 18| 94511|
  23. | David K| 42| 94511|
  24. |Lalit B.|null| 94511|
  25. +--------+----+--------+
  26. -- Sort rows within each partition using column position.
  27. SELECT /*+ REPARTITION(zip_code) */ name, age, zip_code FROM person SORT BY 1;
  28. +--------+----+--------+
  29. | name| age|zip_code|
  30. +--------+----+--------+
  31. | Anil K| 27| 94588|
  32. | Dan Li| 18| 94588|
  33. | John V|null| 94588|
  34. | Zen Hui| 50| 94588|
  35. |Aryan B.| 18| 94511|
  36. | David K| 42| 94511|
  37. |Lalit B.|null| 94511|
  38. +--------+----+--------+
  39. -- Sort rows within partition in ascending manner keeping null values to be last.
  40. SELECT /*+ REPARTITION(zip_code) */ age, name, zip_code FROM person SORT BY age NULLS LAST;
  41. +----+--------+--------+
  42. | age| name|zip_code|
  43. +----+--------+--------+
  44. | 18| Dan Li| 94588|
  45. | 27| Anil K| 94588|
  46. | 50| Zen Hui| 94588|
  47. |null| John V| 94588|
  48. | 18|Aryan B.| 94511|
  49. | 42| David K| 94511|
  50. |null|Lalit B.| 94511|
  51. +----+--------+--------+
  52. -- Sort rows by age within each partition in descending manner, which defaults to NULL LAST.
  53. SELECT /*+ REPARTITION(zip_code) */ age, name, zip_code FROM person SORT BY age DESC;
  54. +----+--------+--------+
  55. | age| name|zip_code|
  56. +----+--------+--------+
  57. | 50| Zen Hui| 94588|
  58. | 27| Anil K| 94588|
  59. | 18| Dan Li| 94588|
  60. |null| John V| 94588|
  61. | 42| David K| 94511|
  62. | 18|Aryan B.| 94511|
  63. |null|Lalit B.| 94511|
  64. +----+--------+--------+
  65. -- Sort rows by age within each partition in descending manner keeping null values to be first.
  66. SELECT /*+ REPARTITION(zip_code) */ age, name, zip_code FROM person SORT BY age DESC NULLS FIRST;
  67. +----+--------+--------+
  68. | age| name|zip_code|
  69. +----+--------+--------+
  70. |null| John V| 94588|
  71. | 50| Zen Hui| 94588|
  72. | 27| Anil K| 94588|
  73. | 18| Dan Li| 94588|
  74. |null|Lalit B.| 94511|
  75. | 42| David K| 94511|
  76. | 18|Aryan B.| 94511|
  77. +----+--------+--------+
  78. -- Sort rows within each partition based on more than one column with each column having
  79. -- different sort direction.
  80. SELECT /*+ REPARTITION(zip_code) */ name, age, zip_code FROM person
  81. SORT BY name ASC, age DESC;
  82. +--------+----+--------+
  83. | name| age|zip_code|
  84. +--------+----+--------+
  85. | Anil K| 27| 94588|
  86. | Dan Li| 18| 94588|
  87. | John V|null| 94588|
  88. | Zen Hui| 50| 94588|
  89. |Aryan B.| 18| 94511|
  90. | David K| 42| 94511|
  91. |Lalit B.|null| 94511|
  92. +--------+----+--------+