sql

Top 3 products per category by revenue

SQL & Data Modeling · Window Functions · medium · Original Uplift question · PostgreSQL · 12 min

Return the top 3 products by total revenue within each category. Include ties at the third position. Order by category, then revenue descending.

Fixture

sales

category, product, amount

create table sales (category text, product text, amount numeric);
insert into sales values
 ('books','A',100),('books','B',300),('books','C',300),('books','D',50),
 ('toys','E',400),('toys','F',200),('toys','G',600);

Evaluation criteria

  • Aggregates revenue per (category, product)
  • Ranks within each category (PARTITION BY category)
  • Uses DENSE_RANK (or RANK) so ties at 3rd are retained
  • Ordered by category then revenue desc

Detailed grading feedback appears after you submit.

Source

Original Uplift question based on a common interview pattern.

Your answer

You get one graded attempt here without an account. Create a free account for unlimited attempts, saved progress and community write-ups.

Answers stay hidden until you ask. Reveal the full model write-up when you are ready to compare.

Community

How other people solved this

There is rarely one right query or one right framing. Read how others approached it, and share your own once you have submitted an answer.

Reading is open to everyone. Sign in to share your approach or mark a write-up as helpful.

Loading write-ups…