Ranking Functions: ROW_NUMBER, RANK, DENSE_RANK & NTILE in MySQL 8.0
Overview & Learning Objectives
Ranking is one of the most common questions in data analytics: What are the top 3 best-selling products in each region? Who are the top 10% highest-spending customers? MySQL 8.0 provides four ranking window functions: `ROW_NUMBER()` (strictly unique sequential integers), `RANK()` (shares rank on ties and skips subsequent positions), `DENSE_RANK()` (shares rank on ties without skipping numbers), and `NTILE(n)` (buckets rows into n quantiles).
- Master the critical distinctions between `ROW_NUMBER()`, `RANK()`, and `DENSE_RANK()` during ties.
- Solve the classic "Top N per Category" business interview problem.
- Segment customer databases into quartiles or deciles using `NTILE()`.
- Deduplicate records cleanly using `ROW_NUMBER() OVER (...)`.
Industry Standard
Standard: Analytics engineering teams at Amazon and Flipkart use `ROW_NUMBER() OVER (PARTITION BY id ORDER BY updated_at DESC)` to deduplicate streaming clickstream records.
Real-World Example: ScoopCraft Dark-Store Product Sales Leaderboard
ScoopCraft ranks flavor sales within each store to isolate the top 2 best-sellers per location.
| flavor_name | dark_store | revenue | ROW_NUMBER | RANK | DENSE_RANK |
|---|---|---|---|---|---|
| Belgian Choco | Koramangala Hub | ₹50,000.00 | 1 | 1 | 1 |
| Mango Sorbet | Koramangala Hub | ₹35,000.00 | 2 | 2 (Tie) | 2 (Tie) |
| Almond Crunch | Koramangala Hub | ₹35,000.00 | 3 | 2 (Tie) | 2 (Tie) |
| Vanilla Bean | Koramangala Hub | ₹20,000.00 | 4 | 4 (Skipped 3) | 3 (No gap) |
Functions & Syntax Covered in this Chapter
Here is the definition, execution syntax, parameter breakdown, and real-world business example for each function/syntax covered in this chapter:
ROW_NUMBER()
Definition: Assigns a unique, consecutive integer starting from 1 to each row within a partition ordered by criteria. Ties receive distinct sequential numbers.
ROW_NUMBER() OVER (PARTITION BY store_id ORDER BY sales DESC)
PARTITION BY: Divides rows into groups.ORDER BY: Determines the sequence in which numbers are assigned.
ROW_NUMBER() OVER (PARTITION BY store_id ORDER BY revenue DESC)RANK()
Definition: Assigns ranks where duplicate values receive identical rank numbers, with subsequent ranks skipping positions (1, 2, 2, 4).
RANK() OVER (PARTITION BY region ORDER BY revenue DESC)
ORDER BY: Columns evaluated for tied values.
RANK() OVER (ORDER BY sales DESC)DENSE_RANK()
Definition: Assigns ranks where duplicate values receive identical rank numbers, but subsequent ranks never skip positions (1, 2, 2, 3).
DENSE_RANK() OVER (ORDER BY salary DESC)
ORDER BY: Columns evaluated for tied values.
DENSE_RANK() OVER (ORDER BY commission DESC)NTILE(n)
Definition: Divides an ordered partition into n approximately equal buckets, assigning bucket numbers from 1 to n.
NTILE(4) OVER (ORDER BY customer_spend DESC)
n: Integer number of buckets (e.g. 4 for quartiles, 10 for deciles, 100 for percentiles).
NTILE(4) OVER (ORDER BY lifetime_spend DESC)Common Mistakes & Limitations
- Using `RANK()` instead of `DENSE_RANK()` when seeking top 3 distinct metric tiers, accidentally missing tier 3 if a tie occurs at tier 2.
- Forgetting the `ORDER BY` clause inside the `OVER()` specification of ranking functions, which makes rankings completely non-deterministic.
Keyboard Shortcuts (MySQL Workbench)
| Action | Windows | Mac |
|---|---|---|
| Execute Query | Ctrl + Enter | Cmd + Enter |
Pro Tips & Modern Best Practices
- Use `ROW_NUMBER() OVER (PARTITION BY business_key ORDER BY modified_date DESC)` to pick the newest state for deduplication.
- Combine `NTILE(10)` with customer lifetime spend to easily identify the top 10% (Decile 1) VIP whales.