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

Roles this comes up in

Companies these come from

Other topics