Medium Window functions Premium

SQL interview question: RANK vs DENSE_RANK

Product Rankings with Tie Handling

The marketplace team needs to rank products by rating, showing both RANK and DENSE_RANK to understand tie handling.

Rank products by rating (highest first) showing both ranking methods.

Return product_name, rating, rank_position, and dense_rank_position. Order by rating descending. Limit to 10.

Note: RANK skips numbers after ties (1,1,3) while DENSE_RANK doesn't (1,1,2).

Demonstrates difference between RANK() and DENSE_RANK()

Tables

products 20 rows
  • product_id integer PK
  • product_name text
  • category text
  • price real
  • rating real
product_idproduct_namecategorypricerating
1MacBook ProElectronics1999.994.8
2iPad AirElectronics599.994.8
3AirPods ProElectronics249.994.7

Keep going