Learn SQL/Advanced/Window Functions/SQL RANK vs DENSE_RANK

SQL RANK vs DENSE_RANK

Both treat ties as equal — they only disagree about what happens right after.

What & Why

RANK and DENSE_RANK agree completely on how to handle the tie itself. They disagree on the row immediately after it.

See How It Works

BUSINESS QUESTION

Product wants both tie-aware ranking conventions shown across feedback ratings 5, 4, 4, and 3.

iduser_idfeature_idratingnps_scorecommentcreated_at
1101159Clear and useful2024-01-25 12:15:00+00
210224NULLExport worked well2024-02-20 15:40:00+00
3103147Helpful setup flow2024-03-18 09:05:00+00
410433NULLNeeds clearer timing2024-04-08 17:30:00+00
EXAMPLE QUERY
SELECT
  id AS feedback_id,
  rating,
  RANK() OVER (ORDER BY rating DESC) AS rank_with_gaps,
  DENSE_RANK() OVER (ORDER BY rating DESC) AS dense_rank
FROM product.feedback
ORDER BY rank_with_gaps, feedback_id;
RESULT — rank gaps compared with dense ranks
feedback_idratingrank_with_gapsdense_rank
1511
2422
3422
4343

The rating-4 tie makes RANK skip position 3, while DENSE_RANK assigns the next distinct rating position 3.

This lesson's practice is part of Pro.

Advanced business practice

Sign up free to try it on a real business scenario