0
0
SQLquery~10 mins

RANK and DENSE_RANK difference in SQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - RANK and DENSE_RANK difference
Start with ordered data
Apply RANK function
Assign ranks with gaps for ties
Apply DENSE_RANK function
Assign ranks without gaps for ties
Compare results
End
We start with ordered data, apply RANK which assigns ranks with gaps for ties, then apply DENSE_RANK which assigns ranks without gaps, and finally compare the two results.
Execution Sample
SQL
SELECT name, score,
       RANK() OVER (ORDER BY score DESC) AS rank,
       DENSE_RANK() OVER (ORDER BY score DESC) AS dense_rank
FROM players;
This query ranks players by score using both RANK and DENSE_RANK to show the difference in handling ties.
Execution Table
RowNameScoreRANK() ResultDENSE_RANK() ResultExplanation
1Alice10011Highest score, rank 1 for both
2Bob9022Second highest, no tie yet
3Charlie9022Tie with Bob, same rank
4David8043After tie, RANK skips rank 3, DENSE_RANK continues sequentially
5Eve7054No tie, ranks continue
6Frank7054Tie with Eve, same rank
7Grace6075After tie, RANK skips rank 6, DENSE_RANK continues sequentially
💡 All rows processed, showing how RANK leaves gaps after ties, DENSE_RANK does not.
Variable Tracker
VariableRow 1Row 2Row 3Row 4Row 5Row 6Row 7
RANK()1224557
DENSE_RANK()1223445
Key Moments - 2 Insights
Why does RANK() skip numbers after ties but DENSE_RANK() does not?
RANK() assigns the same rank to tied rows but leaves gaps in the sequence for the next rank, as seen in rows 3 and 4 in the execution_table. DENSE_RANK() assigns ranks without gaps, continuing the sequence immediately after ties.
What happens to the rank numbers when multiple rows have the same score?
Both RANK() and DENSE_RANK() assign the same rank number to all tied rows, as shown for Bob and Charlie with score 90. The difference is in how the next rank number is assigned after the tie.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the RANK() value for David (Row 4)?
A3
B2
C4
D5
💡 Hint
Check the RANK() Result column for Row 4 in the execution_table.
At which row does DENSE_RANK() assign rank 3?
ARow 3 (Charlie)
BRow 4 (David)
CRow 5 (Eve)
DRow 2 (Bob)
💡 Hint
Look at the DENSE_RANK() Result column in the execution_table.
If Eve's score changed to 85, how would RANK() for David change?
AIt would become 5
BIt would remain 4
CIt would become 3
DIt would become 2
💡 Hint
Consider how ranks shift when a new score is inserted between existing scores, referencing the variable_tracker.
Concept Snapshot
RANK() assigns ranks with gaps after ties.
DENSE_RANK() assigns ranks without gaps.
Both assign same rank to tied rows.
Use ORDER BY to define ranking order.
RANK() skips numbers after ties; DENSE_RANK() does not.
Full Transcript
This visual execution shows how RANK() and DENSE_RANK() assign ranks to rows ordered by score. Both functions assign the same rank to tied rows, but RANK() leaves gaps in the ranking sequence after ties, while DENSE_RANK() continues ranks sequentially without gaps. For example, Bob and Charlie tie with score 90 and both get rank 2. The next rank for David is 4 with RANK() but 3 with DENSE_RANK(). This difference is important when you want continuous ranking numbers or want to reflect gaps caused by ties.