SORT BY clause - Amazon OpenSearch Service

SORT BY clause

Note

To see which AWS data source integrations support this SQL command, see Supported OpenSearch SQL commands and functions.

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

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 the sort order is ASC and NULLS sort last if the 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

CREATE TABLE person (zip_code INT, name STRING, age INT); INSERT INTO person VALUES (94588, 'Shirley Rodriguez', 50), (94588, 'Juan Li', 18), (94588, 'Anil K', 27), (94588, 'John D', NULL), (94511, 'David K', 42), (94511, 'Aryan B.', 18), (94511, 'Lalit B.', NULL); -- Sort rows by `name` within each partition in ascending manner SELECT name, age, zip_code FROM person SORT BY name; +------------------+----+--------+ | name| age|zip_code| +------------------+----+--------+ | Anil K| 27| 94588| | Juan Li| 18| 94588| | John D|null| 94588| | Shirley Rodriguez| 50| 94588| | Aryan B.| 18| 94511| | David K| 42| 94511| | Lalit B.|null| 94511| +------------------+----+--------+ -- Sort rows within each partition using column position. SELECT name, age, zip_code FROM person SORT BY 1; +----------------+----+----------+ | name| age|zip_code| +------------------+----+--------+ | Anil K| 27| 94588| | Juan Li| 18| 94588| | John D|null| 94588| | Shirley Rodriguez| 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 age, name, zip_code FROM person SORT BY age NULLS LAST; +----+------------------+--------+ | age| name|zip_code| +----+------------------+--------+ | 18| Juan Li| 94588| | 27| Anil K| 94588| | 50| Shirley Rodriguez| 94588| |null| John D| 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 age, name, zip_code FROM person SORT BY age DESC; +----+------------------+--------+ | age| name|zip_code| +----+------------------+--------+ | 50| Shirley Rodriguez| 94588| | 27| Anil K| 94588| | 18| Juan Li| 94588| |null| John D| 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 age, name, zip_code FROM person SORT BY age DESC NULLS FIRST; +----+------------------+--------+ | age| name|zip_code| +----+------------------+--------+ |null| John D| 94588| | 50| Shirley Rodriguez| 94588| | 27| Anil K| 94588| | 18| Juan 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 name, age, zip_code FROM person SORT BY name ASC, age DESC; +------------------+----+--------+ | name| age|zip_code| +------------------+----+--------+ | Anil K| 27| 94588| | Juan Li| 18| 94588| | John D|null| 94588| | Shirley Rodriguez| 50| 94588| | Aryan B.| 18| 94511| | David K| 42| 94511| | Lalit B.|null| 94511| +------------------+----+--------+