docs/reference/query-languages/esql/_snippets/commands/layout/limit.md
serverless: ga
stack: ga
The LIMIT processing command limits the number of rows returned.
::::{applies-switch}
:::{applies-item} { stack: ga, "serverless": "ga"}
LIMIT max_number_of_rows
:::
:::{applies-item} { "stack": "preview 9.4+", "serverless": "preview" }
LIMIT max_number_of_rows [BY grouping_expr1[, ..., grouping_exprN]]
::: ::::
max_number_of_rows
: The maximum number of rows to return. When BY is specified, the maximum
number of rows to return per group.
grouping_exprX {applies_to}serverless: preview {applies_to}stack: preview 9.4+
: An expression that outputs the values to group by.
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.
:::{include} ../../common/result-set-size-limitation.md :::
:::{include} ../examples/limit.csv-spec/basic.md :::
stack: preview 9.4
serverless: preview
:::{include} ../examples/limit.csv-spec/limitBy.md :::
stack: preview 9.4
serverless: preview
:::{include} ../examples/limit.csv-spec/limitByMultipleGroups.md :::