Skip to main content
SQL โ€ข LESSON 156

NTILE

How can we divide employees into four equal compensation quartiles?

Advancedโฑ 3 Minutesโญ 1690 XP
๐Ÿค” THE QUESTION

How can we divide employees into four equal compensation quartiles?

๐Ÿ’ก WHAT IS IT?

NTILE(N) divides an ordered partition into N equal buckets numbered from 1 to N.

๐ŸŽฏ WHAT IS IT USED FOR?

Customer RFM quartile segmentation, performance quartiles, risk deciles.

๐Ÿ’ป EXAMPLE
SELECT
name,
salary,
NTILE(4) OVER (
    ORDER BY salary DESC
) AS salary_quartile
FROM employees;

๐ŸŽฏ Mission Objectives

Practice typing production-grade SQL code for NTILE.

  • โœ“ NTILE(4)
  • โœ“ Quartile bucketing
  • โœ“ Statistical distribution
  • โœ“ Customer/Staff segmentation