# LIMIT

The LIMIT command is used to restrict the number of rows returned by a query.

## Syntax

`LIMIT max_number_of_rows`

### Parameters

#### max_number_of_rows

This parameter specifies the maximum number of rows to be returned.

## Examples

This example demonstrates how to limit the number of rows returned to 5.

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

This example shows how to limit the number of rows after applying a filter:

```esql
FROM employees
| WHERE department == "Engineering"
| LIMIT 10
```

This example demonstrates limiting the number of rows after performing an aggregation:

```esql
FROM employees
| STATS avg_salary = AVG(salary) BY department
| LIMIT 3
```

This example shows how to limit the number of rows after sorting the data:

```esql
FROM employees
| SORT hire_date DESC
| LIMIT 7
```

This example demonstrates the use of `LIMIT` in conjunction with multiple other commands:

```esql
FROM employees
| WHERE hire_date > "2020-01-01"
| SORT salary DESC
| KEEP first_name, last_name, salary
| LIMIT 5
```

## Limitations

There is no way to achieve pagination with LIMIT, there is no offset parameter.

A query will never return more than 10,000 rows. This limitation only applies to the number of rows retrieved by the query. The query and any aggregations will still run on the full dataset.

To work around this limitation:

- Reduce the size of the result set by modifying the query to only return relevant data. This can be achieved by using the WHERE command to select a smaller subset of the data.
- Shift any post-query processing to the query itself. The ES|QL STATS ... BY command can be used to aggregate data within the query.

## Notes

The default and maximum limits can be adjusted using the following dynamic cluster settings:

- `esql.query.result_truncation_default_size`
- `esql.query.result_truncation_max_size`
