Skip to main content
PostgreSQL & Redis Engineering Predict Output P0 Medium

ROW_NUMBER() vs RANK() vs DENSE_RANK() on ties

Predict the three rank columns for scores [100, 90, 90, 80] ordered DESC.

Official Reference

Code Snippet

SELECT score,
  ROW_NUMBER() OVER (ORDER BY score DESC) AS rn,
  RANK()       OVER (ORDER BY score DESC) AS rnk,
  DENSE_RANK() OVER (ORDER BY score DESC) AS drnk
FROM results;

Expected Output & Explanation

100 | rn:1 | rnk:1 | drnk:1
 90 | rn:2 | rnk:2 | drnk:2
 90 | rn:3 | rnk:2 | drnk:2
 80 | rn:4 | rnk:4 | drnk:3

ROW_NUMBER assigns unique sequential numbers (ties broken arbitrarily without a tie-breaker). RANK leaves gaps after ties (1, 2, 2, 4). DENSE_RANK leaves no gaps (1, 2, 2, 3). Use DENSE_RANK when finding 'top 3 distinct scores' and ROW_NUMBER when picking exactly 1 winner per group.

Follow-up Interview Question · How do you make ROW_NUMBER() deterministic when scores tie?

Module Context & References

SQL & PostgreSQL · Query behavior & trap drills Predict query results for NULL logic, JOIN filters, aggregates and window functions
Personal Notes · Markdown

Technologies & Interview Topics Covered

Switch between short-term interview tracks and long-term 80/20 engineering depth to practice JavaScript internals, React rendering and hooks, timed frontend machine coding UI challenges, MeetaPro FG300, Blind 75 & NeetCode 150 DSA patterns, Python & FastAPI APIs, PostgreSQL query optimization & MVCC, Redis caching, Applied AI RAG pipelines & evaluations, and distributed system design.