DENSE_RANK — Complete Guide
DENSE_RANK — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of MySQL Tutorial on Toolliyo Academy.
On this page
MySQL Tutorial · Lesson 38 of 100
DENSE_RANK
Basics ✓ → Advanced
Advanced · 2 — Production · ~6 min · MySQL — Functions & Window Functions
What is this?
DENSE_RANK() like RANK but without gaps after ties: 1,1,2,3. Tied rows share rank; next rank is always previous + 1.
Why should you care?
Tiered shipping discounts for “top 3 cities by volume” need dense ranks so exactly three tiers exist even when #2 ties.
See it live — copy this example
Run in MySQL Workbench or the mysql CLI.
SELECT city,
COUNT(*) AS cust_count,
DENSE_RANK() OVER (ORDER BY COUNT(*) DESC) AS dense_rk,
RANK() OVER (ORDER BY COUNT(*) DESC) AS rank_with_gaps
FROM customers
GROUP BY city;
What happened?
- Side-by-side dense_rk and rank_with_gaps show gap behavior when cities tie on cust_count.
- DENSE_RANK keeps consecutive tier numbers.
Practice next
- Craft tie counts between cities.
- Run both window functions.
- Document which rank function product wants.
- PARTITION BY state if column added.
- Combine with NTILE(4) for quartile buckets.
Remember
DENSE_RANK: ties yes, gaps no. Contrast RANK for skip behavior. Common in tier and banding rules.
DataFlow loyalty tiers
Top 3 dense rank cities get free shipping promo — ties still count as one tier step.
Outcome: Marketing rules match SQL output exactly.
Interview prep for this lesson
Practice these questions aloud after reading—each links to a full structured answer.
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!