---
title: LIMIT clause
description: Constrain the number of rows returned by a query with the LIMIT clause in Databricks SQL and Databricks Runtime.
canonical_url: https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-limit
---

# LIMIT clause

**Applies to:** ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) Databricks SQL ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) Databricks Runtime

Constrains the number of rows returned by the [Query](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-query.md). In
general, this clause is used in conjunction with [ORDER BY](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-orderby.md) 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**

  A literal expression that returns an integer.
  If the expression is not foldable, is not of integer type, evaluates to `NULL`, or evaluates to a negative value, Databricks raises [INVALID\_LIMIT\_LIKE\_EXPRESSION](https://docs.databricks.com/aws/en/error-messages/invalid-limit-like-expression-error-class.md).

## Common error conditions

* [INVALID\_LIMIT\_LIKE\_EXPRESSION](https://docs.databricks.com/aws/en/error-messages/invalid-limit-like-expression-error-class.md)

## Examples

```sql
> CREATE TEMP VIEW person (name, age)
    AS VALUES ('Zen Hui', 25),
              ('Anil B' , 18),
              ('Shone S', 16),
              ('Mike A' , 25),
              ('John A' , 18),
              ('Jack N' , 16);

-- Select the first two rows.
> SELECT name, age FROM person ORDER BY name LIMIT 2;
 Anil B  18
 Jack N  16

-- Select the 4th and 5th rows by alphabetical order.
> SELECT name, age FROM person ORDER BY name LIMIT 2 OFFSET 3;
  Mike A  25
 Shone S  16

-- Specifying ALL option on LIMIT returns all the rows.
> SELECT name, age FROM person ORDER BY name LIMIT ALL;
  Anil B  18
  Jack N  16
  John A  18
  Mike A  25
 Shone S  16
 Zen Hui  25

-- A function expression as an input to LIMIT.
> SELECT name, age FROM person ORDER BY name LIMIT length('SPARK');
  Anil B  18
  Jack N  16
  John A  18
  Mike A  25
 Shone S  16

-- A non-literal expression as an input to LIMIT is not allowed.
> SELECT name, age FROM person ORDER BY name LIMIT length(name);
  Error: INVALID_LIMIT_LIKE_EXPRESSION

-- A negative LIMIT is not allowed.
> SELECT name, age FROM person LIMIT -1;
  Error: INVALID_LIMIT_LIKE_EXPRESSION
```

## Related articles

* [OFFSET](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-offset.md)
* [Query](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-query.md)
