data2al icon data2al Data Engineering Notes and Code Patterns

Concept note

Choose the Right Ranking Function for Ties

A side-by-side ranking pattern for deciding between ROW_NUMBER, RANK, and DENSE_RANK.

2026-01-19
Alan
SQL
Intermediate
SQL Window-Functions Ranking Analytics

Use this when you need ranked output and the handling of ties changes the result.

Example

select
    student_name,
    score,
    row_number() over (order by score desc) as row_num,
    rank() over (order by score desc) as score_rank,
    dense_rank() over (order by score desc) as dense_score_rank
from exam_results;

Why this pattern helps

  • shows tie behavior directly in one result set
  • prevents accidental ranking bugs in leaderboards and reporting
  • makes it easier to choose the right function before production use

Notes

  • use row_number() when every row must be unique
  • use rank() when ties should create skipped positions
  • use dense_rank() when ties should share a rank without gaps

Similar Posts