SQL Topic
Ranking
Ranking questions use RANK, DENSE_RANK, and ROW_NUMBER to order rows within partitions — top N per group, nth highest salary, leaderboards with ties.
When is Ranking used?
'Top N per category', 'nth highest', 'rank scores with ties', and any per-group ordering that must survive filtering.
Core syntax
RANK() OVER (PARTITION BY dept ORDER BY salary DESC) — wrap it in a subquery and filter rnk = N.Common mistakes
- Choosing ROW_NUMBER when ties must share a rank (use RANK or DENSE_RANK).
- Filtering the window function in WHERE instead of an outer query.
- Forgetting a tiebreaker in ORDER BY inside OVER for deterministic results.
Practice questions
No dedicated practice questions yet — explore the related topics and the full question catalogue.