Back to Practice
#0085

Rank Products by Revenue

MediumSQL25 min20 XP

Problem

Return product, revenue, and revenue_rank where rank 1 is highest revenue. Products with equal revenue should share rank.

Why This Matters

Window functions let you compare rows without collapsing the final result.

Schema

sales(product TEXT, amount REAL)

Examples

Example 1
InputA=20, B=20, C=5
OutputA rank 1, B rank 1, C rank 3

RANK leaves a gap after ties.

Constraints

  • Aggregate before ranking.
  • Use RANK, not ROW_NUMBER.
  • Sort by rank then product.
CodeSQL
Visible browser tests run here when available.
Testcases1 visible / 3 hidden categories
Tie handling
Inputsales rows
Expectedshared rank for ties

A and B tie for rank 1.

Hidden Test Categories
no tiesthree-way tiemultiple rows per product