---
title: "NTILE"
description: "NTILE(n) is a window function that divides the rows in a partition into n roughly equal-sized buckets and returns the bucket number for each row."
canonical: "https://motherduck.com/glossary/ntile/"
related:
  - title: "Window functions | MotherDuck Docs"
    url: "https://motherduck.com/docs/sql-reference/duckdb-sql-reference/window-functions/"
  - title: "SQL Golf: 5 SQL Tricks You Should (Probably) Never Use"
    url: "https://motherduck.com/blog/its-a-sql-golf-quackmas/"
  - title: "DuckLake Architecture Deep Dive: Catalog, Storage, Compute | MotherDuck"
    url: "https://motherduck.com/videos/ducklake-architecture-deep-dive/"
---

# NTILE

> NTILE(n) is a window function that divides the rows in a partition into n roughly equal-sized buckets and returns the bucket number for each row.

## Overview
`NTILE(n)` splits an ordered set of rows into `n` groups of as-equal-as-possible size and labels each row with its group number, from 1 to `n`. It's the SQL building block for quartiles, deciles, and percentile-style bucketing.

```sql
SELECT
  customer_id,
  lifetime_value,
  NTILE(4) OVER (ORDER BY lifetime_value DESC) AS value_quartile
FROM customers;
```

If the row count doesn't divide evenly by `n`, DuckDB and standard SQL both distribute the remainder to the earliest buckets, so some early groups may have one more row than later ones.

## Common uses
- **Quartile/decile segmentation**: `NTILE(4)` for customer value quartiles, `NTILE(10)` for deciles.
- **A/B or cohort bucketing**: assign rows to a fixed number of experiment groups deterministically.
- **Load balancing**: split a large ordered dataset into `n` roughly even chunks for parallel processing.

```sql
SELECT value_quartile, COUNT(*), AVG(lifetime_value)
FROM (
  SELECT lifetime_value, NTILE(4) OVER (ORDER BY lifetime_value DESC) AS value_quartile
  FROM customers
)
GROUP BY ALL
ORDER BY ALL;
```

## DuckDB specifics
DuckDB implements `NTILE(n)` as a standard window function, usable with `PARTITION BY` to bucket independently within each group (e.g., quartiles computed separately per region). DuckDB's `GROUP BY ALL` and `ORDER BY ALL` shortcuts pair well with `NTILE` output when summarizing bucket statistics, as shown above, avoiding the need to repeat column lists.