Ranking
SQL Ranking Interview Questions
Ranking is picking the top row, or the top few, within each group: the best-selling product per region, each customer’s first order. It is the most realistic task on this list, and it is asked because LIMIT cannot do it.
What ranking questions ask you to do
Across the questions here, these are the shapes that come up:
- Return the single best row per group
- Return the top few per group
- Find the second highest value
- Find the most recent record per customer
- Decide what should happen when two rows tie
Common mistakes with ranking
Ties decide which function you need. ROW_NUMBER picks one of the tied rows and which one is not guaranteed, so the same query can return different rows on different runs. RANK keeps all of them and skips the numbers afterwards. DENSE_RANK keeps all of them without skipping.
Questions to practice
- Busiest stock per clientSQL
Zerodhamedium - Cheapest channel per campaignSQL
Nykaamedium - Deepest cut per productSQL
Myntramedium - Everyone in the top threeSQL
DraftKingsmedium - Longest incident per servicePandas
PagerDutymedium - Packages still movingSQL
Amazonmedium - Place within the contestPandas
FanDuelmedium - Prize ranks with tiesSQL
DraftKingsmedium - Slowest ticket per projectSQL
Atlassianmedium - Top earner per departmentPandas
Salesforcemedium - Top partners per citySQLPremium
Swiggymedium - Top reason per categorySQLPremium
Flipkartmedium - Top track per listenerSQL
Spotifymedium - Largest order per customerPandas
Amazonhard - Slowest package per facilitySQL
Amazonhard
Questions last reviewed 19 October 2026.