> ## Documentation Index
> Fetch the complete documentation index at: https://private-7c7dfe99-fix-nav-issues.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

> Documentation for LIMIT Clause

# LIMIT Clause

The `LIMIT` clause controls how many rows are returned from your query results.

<h2 id="basic-syntax">
  Basic syntax
</h2>

**Select first rows:**

```sql theme={null}
LIMIT m
```

Returns the first `m` rows from the result, or all records when there are fewer than `m`.

**Alternative TOP syntax (MS SQL Server compatible):**

```sql theme={null}
-- SELECT TOP number|percent column_name(s) FROM table_name
SELECT TOP 10 * FROM numbers(100);
SELECT TOP 0.1 * FROM numbers(100);
```

This is equivalent to `LIMIT m` and can be used for compatibility with Microsoft SQL Server queries.

**Select with offset:**

```sql theme={null}
LIMIT m OFFSET n
-- or equivalently:
LIMIT n, m
```

Skips the first `n` rows, then returns the next `m` rows.

In both forms, `n` and `m` must be non-negative integers.

<h2 id="negative-limits">
  Negative limits
</h2>

Select rows from the *end* of the result set using negative values:

| Syntax               | Result                                          |
| -------------------- | ----------------------------------------------- |
| `LIMIT -m`           | Last `m` rows                                   |
| `LIMIT -m OFFSET -n` | Last `m` rows after skipping the last `n` rows  |
| `LIMIT m OFFSET -n`  | First `m` rows after skipping the last `n` rows |
| `LIMIT -m OFFSET n`  | Last `m` rows after skipping the first `n` rows |

The `LIMIT -n, -m` syntax is equivalent to `LIMIT -m OFFSET -n`.

<h2 id="fractional-limits">
  Fractional limits
</h2>

Use decimal values between 0 and 1 to select a percentage of rows:

| Syntax                  | Result                                                    |
| ----------------------- | --------------------------------------------------------- |
| `LIMIT 0.1`             | First 10% of rows                                         |
| `LIMIT 1 OFFSET 0.5`    | The median row                                            |
| `LIMIT 0.25 OFFSET 0.5` | Third quartile (25% of rows after skipping the first 50%) |

<Note>
  * Fractions must be [Float64](/reference/data-types/float) values greater than 0 and less than 1.
  * Fractional row counts are rounded to the next whole number.
</Note>

<h2 id="combining-limit-types">
  Combining limit types
</h2>

You can mix standard integers with fractional or negative offsets:

```sql theme={null}
LIMIT 10 OFFSET 0.5    -- 10 rows starting from the halfway point
LIMIT 10 OFFSET -20    -- 10 rows after skipping the last 20
```

<h2 id="limit--with-ties-modifier">
  LIMIT ... WITH TIES
</h2>

The `WITH TIES` modifier includes additional rows that have the same `ORDER BY` values as the last row in your limit.

```sql theme={null}
SELECT * FROM (
    SELECT number % 50 AS n FROM numbers(100)
) ORDER BY n LIMIT 0, 5
```

```response theme={null}
┌─n─┐
│ 0 │
│ 0 │
│ 1 │
│ 1 │
│ 2 │
└───┘
```

With `WITH TIES`, all rows matching the last value are included:

```sql theme={null}
SELECT * FROM (
    SELECT number % 50 AS n FROM numbers(100)
) ORDER BY n LIMIT 0, 5 WITH TIES
```

```response theme={null}
┌─n─┐
│ 0 │
│ 0 │
│ 1 │
│ 1 │
│ 2 │
│ 2 │
└───┘
```

Row 6 is included because it has the same value (`2`) as row 5.

The same applies when the offset is specified with the `OFFSET` keyword:

```sql theme={null}
SELECT * FROM (
    SELECT number % 50 AS n FROM numbers(100)
) ORDER BY n LIMIT 3 OFFSET 2 WITH TIES
```

```response theme={null}
┌─n─┐
│ 1 │
│ 1 │
│ 2 │
│ 2 │
└───┘
```

Skipping the first 2 rows and taking 3 would normally return `1, 1, 2`, but the second `2` is included because it ties with the last row.

<Note>
  `WITH TIES` is not supported with negative limits.
</Note>

This modifier can be combined with the [`ORDER BY ... WITH FILL`](/reference/statements/select/order-by#order-by-expr-with-fill-modifier) modifier.

<h2 id="considerations">
  Considerations
</h2>

**Non-deterministic results:** Without an [`ORDER BY`](/reference/statements/select/order-by) clause, the rows returned may be arbitrary and vary between query executions.

**Server-side limit:** The number of rows returned can also be affected by the [limit](/reference/settings/session-settings#limit) setting.

<h2 id="see-also">
  See also
</h2>

* [LIMIT BY](/reference/statements/select/limit-by) — Limits rows per group of values, useful for getting top N results within each category.
