LIMIT clause - Amazon OpenSearch Service

LIMIT clause

Note

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

The LIMIT clause is used to constrain the number of rows returned by the SELECT statement. In general, this clause is used in conjunction with ORDER BY to ensure that the results are deterministic.

Syntax

LIMIT { ALL | integer_expression }

Parameters

  • ALL

    If specified, the query returns all the rows. In other words, no limit is applied if this option is specified.

  • integer_expression

    Specifies a foldable expression that returns an integer.

Examples

CREATE TABLE person (name STRING, age INT); INSERT INTO person VALUES ('Jane Doe', 25), ('Pat C', 18), ('Nikki W', 16), ('John D', 25), ('Juan L', 18), ('Jorge S', 16); -- Select the first two rows. SELECT name, age FROM person ORDER BY name LIMIT 2; +-------+---+ | name|age| +-------+---+ | Pat C| 18| |Jorge S| 16| +------+---+ -- Specifying ALL option on LIMIT returns all the rows. SELECT name, age FROM person ORDER BY name LIMIT ALL; +--------+---+ | name|age| +--------+---+ | Pat C| 18| | Jorge S| 16| | Juan L| 18| | John D| 25| | Nikki W| 16| |Jane Doe| 25| +--------+---+ -- A function expression as an input to LIMIT. SELECT name, age FROM person ORDER BY name LIMIT length('OPENSEARCH'); +-------+---+ | name|age| +-------+---+ | Pat C| 18| |Jorge S| 16| | Juan L| 18| | John D| 25| |Nikki W| 16| +-------+---+