InterviewPrepKit

Home / SQL / Window Functions

RANK vs DENSE_RANK on Tied Scores

medium
Solving tips
  • RANK leaves gaps after ties (1,1,3,...); DENSE_RANK does not (1,1,2,...) — pick based on whether skipped positions matter.
  • Ties are defined purely by the ORDER BY expression of the window; two rows tie only if those values are equal.
  • Neither function needs PARTITION BY unless you want the ranking to restart per group.

You have exam scores with several ties. The interviewer wants you to show, side by side, how RANK and DENSE_RANK number the tied rows differently.

Schema

CREATE TABLE exam_scores (
    student TEXT,
    score   INT
);
studentscore
Ann95
Bob88
Cara95
Dan88
Eve72
Finn88

Task

Rank students by score from highest to lowest. Return student, score, the RANK value as rnk, and the DENSE_RANK value as dense_rnk. Order the output by score descending, then student ascending.

Expected output

studentscorernkdense_rnk
Ann9511
Cara9511
Bob8832
Dan8832
Finn8832
Eve7263
Your workspace Not runnable by design — this is your interview scratchpad. Saved on this device.