πͺ£ NTILE() is a SQL window function that divides the rows in a result set into a specified number of approximately equal groups (buckets). Each row is assigned a bucket number starting from 1.
π What is NTILE()?
NTILE() distributes rows into a given number of groups based on the ordering defined in the OVER() clause. It is commonly used for percentile analysis, quartiles, deciles, and segmenting ranked data.
Information
π― Why Use NTILE()?
NTILE() is useful when you need to divide data into equal-sized groups for reporting and analysis.
- π Create quartiles, quintiles, or deciles.
- π Segment customers by spending.
- π Categorize employees by salary.
- π Perform percentile analysis.
- π Build business intelligence reports.
π Sample Table
| EmployeeID | EmployeeName | Department | Salary |
|---|---|---|---|
| 101 | Alice | Sales | 95000 |
| 102 | Bob | Sales | 85000 |
| 103 | Charlie | IT | 80000 |
| 104 | David | IT | 70000 |
| 105 | Emma | HR | 65000 |
| 106 | Frank | HR | 60000 |
| 107 | Grace | Finance | 55000 |
| 108 | Henry | Finance | 50000 |
π Basic Syntax
NTILE() Syntax
SELECT
column1,
column2,
NTILE(number_of_groups) OVER (
ORDER BY column_name
) AS Bucket
FROM table_name;The argument passed to NTILE() specifies how many buckets the result set should be divided into.
π‘ Example: Divide Employees into Four Salary Groups
Quartile Example
SELECT
EmployeeName,
Salary,
NTILE(4) OVER (
ORDER BY Salary DESC
) AS SalaryQuartile
FROM Employees;π Example Output
| EmployeeName | Salary | SalaryQuartile |
|---|---|---|
| Alice | 95000 | 1 |
| Bob | 85000 | 1 |
| Charlie | 80000 | 2 |
| David | 70000 | 2 |
| Emma | 65000 | 3 |
| Frank | 60000 | 3 |
| Grace | 55000 | 4 |
| Henry | 50000 | 4 |
π‘ Example: Divide Data into Three Groups
Three Buckets
SELECT
EmployeeName,
Salary,
NTILE(3) OVER (
ORDER BY Salary DESC
) AS SalaryGroup
FROM Employees;Because eight rows cannot be divided evenly into three groups, the earlier buckets receive one additional row.
π‘ Example: Partition by Department
Use PARTITION BY to divide employees into buckets separately within each department.
NTILE() with PARTITION BY
SELECT
EmployeeName,
Department,
Salary,
NTILE(2) OVER (
PARTITION BY Department
ORDER BY Salary DESC
) AS SalaryGroup
FROM Employees;π Bucket Distribution Example
| Total Rows | NTILE Value | Distribution |
|---|---|---|
| 8 | 2 | 4 + 4 |
| 8 | 3 | 3 + 3 + 2 |
| 8 | 4 | 2 + 2 + 2 + 2 |
| 10 | 4 | 3 + 3 + 2 + 2 |
Remember
βοΈ NTILE() vs Other Ranking Functions
| Function | Purpose |
|---|---|
| ROW_NUMBER() | Assigns a unique sequential number to every row. |
| RANK() | Ranks rows with gaps after ties. |
| DENSE_RANK() | Ranks rows without gaps after ties. |
| NTILE() | Divides rows into approximately equal-sized groups. |
πΌ Real-World Applications
- π° Divide customers into spending quartiles.
- π Create employee salary bands.
- π¦ Segment loan applicants by credit score.
- π Perform percentile and decile analysis.
- π Classify products into performance tiers.
- π Group students by exam performance.
ποΈ Database Compatibility
| Database System | NTILE() Support |
|---|---|
| MySQL | β Supported in MySQL 8.0 and later. |
| PostgreSQL | β Fully supported. |
| SQL Server | β Supported (SQL Server 2005 and later). |
| Oracle | β Fully supported. |
| SQLite | β Supported in SQLite 3.25.0 and later. |
β οΈ Common Mistakes
- β Confusing buckets with ranking values.
- β Omitting the ORDER BY clause inside OVER().
- β Assuming every bucket always contains the same number of rows.
- β Forgetting PARTITION BY when groups should be calculated independently.
Warning
β οΈ Best Practices
Best Practice
π Key Points to Remember
- π NTILE() divides rows into approximately equal-sized groups.
- π Bucket numbering starts at 1.
- π It is a SQL window function.
- π ORDER BY determines how rows are distributed.
- π PARTITION BY creates separate bucket calculations for each group.
- π It is ideal for quartiles, deciles, percentiles, and data segmentation.