Beginner

677. JOIN with NTILE

Divide JOINed records into ranked groups using NTILE.

📘 Lesson 677 of 728 ⏱ 14 min 🎯 Advanced

NTILE divides ordered rows into a specified number of approximately equal groups.

It is useful for segmentation such as top 25%, middle 50%, and bottom 25%.

Divide customers into four sales-performance groups.

SQL
SELECT
    c.CustomerName,
    SUM(o.OrderAmount) AS TotalSales,
    NTILE(4) OVER (
        ORDER BY SUM(o.OrderAmount) DESC
    ) AS SalesQuartile
FROM Customers c
LEFT JOIN Orders o
    ON c.CustomerID = o.CustomerID
GROUP BY c.CustomerName;

Used in customer segmentation, performance analysis, marketing, and risk classification.

  • NTILE divides rows into numbered groups.
  • ORDER BY determines group assignment.
  • NTILE(4) creates four groups.
  • Groups may not contain exactly equal numbers of rows.
  • Useful for percentile-style segmentation.
What does NTILE(4) do?

It divides the ordered result into four approximately equal groups.

Where is NTILE useful?

It is useful for customer segmentation, performance quartiles, and ranking-based analysis.

Have you finished this lesson?

Introduction to SQL – All Lessons