NTILE()

SQL · Window Functions

Theory

NTILE(n) splits the ordered rows of a partition into n buckets that are as equal in size as possible, and returns the bucket number (1 to n) for each row. NTILE(4) gives quartiles, NTILE(10) gives deciles. Bucket 1 holds the first rows in the window ORDER BY, so with ORDER BY salary bucket 1 is the lowest-paid group.

When the rows do not divide evenly, the earlier buckets get one extra row each. With 6 rows and NTILE(4), 6 / 4 = 1 remainder 2, so buckets 1 and 2 get 2 rows and buckets 3 and 4 get 1 row. NTILE works on row counts, not values, so two people with the same salary can land in different buckets, and with fewer rows than buckets some buckets are just empty. If you need value-based cut-offs, use PERCENT_RANK or percentile functions instead.

Why we use it

Analysts often need to segment a list into equal-sized groups: top 25% customers by spend, salary quartiles, deciles for a lift chart. NTILE does this in one expression without calculating cut-off values first.

When to use it

Use NTILE for equal-count segments such as quartiles or A/B/C customer tiers. If ties must stay in the same group, or you need true value percentiles, use PERCENT_RANK, CUME_DIST or PERCENTILE_CONT instead.

How it works

  1. Rows sorted by the window ORDER BY
  2. Count rows in the partition (N) and read bucket count (n)
  3. Base size = N / n, remainder r = N mod n
  4. First r buckets get base size + 1 rows
  5. Fill buckets in order and label rows 1..n
  6. Each row has a bucket number

Syntax

NTILE(n) OVER (ORDER BY col)

Example

SELECT name, salary,
  NTILE(4) OVER (ORDER BY salary) AS quartile
FROM employees;

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic