---
title: ES|QL LIMIT command
description: 
url: https://www.elastic.co/docs/reference/query-languages/esql/commands/limit
products:
  - Elasticsearch
---

# ES|QL LIMIT command
<applies-to>
  - Elastic Cloud Serverless: Generally available
  - Elastic Stack: Generally available
</applies-to>

The `LIMIT` processing command limits the number of rows returned.

## Syntax

<applies-switch>
  <applies-item title="{ stack: ga, "serverless": "ga"}" applies-to="Elastic Cloud Serverless: Generally available, Elastic Stack: Generally available">
    ```esql
    LIMIT max_number_of_rows
    ```
  </applies-item>

  <applies-item title="{ "stack": "preview 9.4+", "serverless": "preview" }" applies-to="Elastic Cloud Serverless: Preview, Elastic Stack: Planned">
    ```esql
    LIMIT max_number_of_rows [BY grouping_expr1[, ..., grouping_exprN]]
    ```
  </applies-item>
</applies-switch>


## Parameters

<definitions>
  <definition term="max_number_of_rows">
    The maximum number of rows to return. When `BY` is specified, the maximum
    number of rows to return **per group**.
  </definition>
  <definition term="grouping_exprX Elastic Cloud Serverless: Preview Elastic Stack: Planned">
    An expression that outputs the values to group by.
  </definition>
</definitions>


## Description

Use the `LIMIT` processing command to limit the number of rows returned.
When `BY` is specified, up to `max_number_of_rows` rows are retained for each
distinct combination of the grouping expressions.
Precede `LIMIT <N> BY` with a `SORT` to keep the top N for each group.
For instance,
```esql
FROM index | WHERE field = "value"
```

is equivalent to:
```esql
FROM index | WHERE field = "value" | LIMIT 1000
```

Queries do not return more than 10,000 rows, regardless of the `LIMIT` command’s value. This is a configurable upper limit.
To overcome this limitation:
- Reduce the result set size by modifying the query to only return relevant data. Use [`WHERE`](https://www.elastic.co/docs/reference/query-languages/esql/commands/where) to select a smaller subset of the data.
- Shift any post-query processing to the query itself. You can use the ES|QL [`STATS`](https://www.elastic.co/docs/reference/query-languages/esql/commands/stats-by) command to aggregate data in the query.

The upper limit only applies to the number of rows that are output by the query, not to the number of documents it processes: the query runs on the full data set.
Consider the following two queries:
```esql
FROM index | WHERE field0 == "value" | LIMIT 20000
```

and
```esql
FROM index | STATS AVG(field1) BY field2 | LIMIT 20000
```

In both cases, the filtering by `field0` in the first query or the grouping by `field2` in the second is applied over all the documents present in the `index`, irrespective of their number or indexes size. However, both queries will return at most 10,000 rows, even if there were more rows available to return.
The default and maximum limits can be changed using these dynamic cluster settings:
- `esql.query.result_truncation_default_size`
- `esql.query.result_truncation_max_size`

For time-series aggregations under the [`TS`](https://www.elastic.co/docs/reference/query-languages/esql/commands/ts) source command specifically, the default and maximum limits are higher and can be changed using these cluster settings:
- `esql.query.timeseries_result_truncation_default_size`
- `esql.query.timeseries_result_truncation_max_size`

Modifying these values involves trade-offs. A larger result-set involves a higher memory pressure and increased processing times; the internode traffic within and across clusters can also increase.
These limitations are similar to those enforced by the [search API for pagination](https://www.elastic.co/docs/reference/elasticsearch/rest-apis/paginate-search-results).

| Functionality                                               | Search                  | ES|QL                                                |
|-------------------------------------------------------------|-------------------------|------------------------------------------------------|
| Results returned by default                                 | 10                      | 1,000                                                |
| Results returned by default (time-series aggregations)      | 10                      | 10,000                                               |
| Default upper limit                                         | 10,000                  | 10,000                                               |
| Default upper limit (time-series aggregations)              | 10,000                  | 10,000,000                                           |
| Specify number of results                                   | `size`                  | `LIMIT`                                              |
| Change default number of results                            | n/a                     | esql.query.result_truncation_default_size            |
| Change default number of results (time-series aggregations) | n/a                     | esql.query.timeseries_result_truncation_default_size |
| Change default upper limit                                  | index-max-result-window | esql.query.result_truncation_max_size                |
| Change default upper limit  (time-series aggregations)      | index-max-result-window | esql.query.timeseries_result_truncation_max_size     |


## Examples


### Limit

```esql
FROM employees
| SORT emp_no ASC
| LIMIT 5
```


### With groups

<applies-to>
  - Elastic Cloud Serverless: Preview
  - Elastic Stack: Planned
</applies-to>

```esql
FROM employees
| SORT salary DESC
| LIMIT 1 BY gender
| KEEP first_name, last_name, salary, gender
```


| first_name:keyword | last_name:keyword | salary:integer | gender:keyword |
|--------------------|-------------------|----------------|----------------|
| Otmar              | Herbst            | 74999          | M              |
| Tzvetan            | Zielinski         | 74572          | F              |
| Lillian            | Haddadi           | 73717          | null           |


### Group by multiple values

<applies-to>
  - Elastic Cloud Serverless: Preview
  - Elastic Stack: Planned
</applies-to>

```esql
FROM employees
| SORT salary DESC
| LIMIT 1 BY languages, gender
```