PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms/Coinbase

Write SQL to rank top products per category

Last updated: Mar 29, 2026

Quick Overview

This question evaluates SQL proficiency in aggregation, time-based filtering, joining transactional and reference tables, and ranking/windowing techniques to compute and deterministically order product revenue with tie-breaking by product_id.

  • medium
  • Coinbase
  • Coding & Algorithms
  • Machine Learning Engineer

Write SQL to rank top products per category

Company: Coinbase

Role: Machine Learning Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

You are given two tables: **products** - `product_id` (int, primary key) - `category` (string) **order_items** - `order_id` (int) - `product_id` (int) - `quantity` (int) - `unit_price` (decimal) - `created_at` (timestamp) **Task** 1. Compute each product’s **total revenue in calendar year 2024**, where revenue for an item is `quantity * unit_price`. 2. For each `category`, return the **top 3 products by total revenue** in 2024. 3. If multiple products tie, break ties by smaller `product_id` first. **Output** Return rows with: - `category` - `product_id` - `total_revenue` - `rank_in_category` (1 = highest revenue within the category) Sort the final output by `category`, then `rank_in_category`, then `product_id`. **Notes/Constraints** - A product with no sales in 2024 should not appear. - Use a solution that involves an aggregation step and a window function (e.g., `ROW_NUMBER`, `RANK`, or `DENSE_RANK`).

Quick Answer: This question evaluates SQL proficiency in aggregation, time-based filtering, joining transactional and reference tables, and ranking/windowing techniques to compute and deterministically order product revenue with tie-breaking by product_id.

Related Interview Questions

  • Implement an In-Memory Database - Coinbase (hard)
  • Implement a Coin-Constrained Jump Strategy - Coinbase (hard)
  • Implement Game Physics and Block Mining - Coinbase (hard)
  • Compute Total Manual Distance - Coinbase (medium)
  • Implement a Flappy Bird Jump Agent - Coinbase
Coinbase logo
Coinbase
Dec 4, 2025, 12:00 AM
Machine Learning Engineer
Onsite
Coding & Algorithms
5
0

You are given two tables:

products

  • product_id (int, primary key)
  • category (string)

order_items

  • order_id (int)
  • product_id (int)
  • quantity (int)
  • unit_price (decimal)
  • created_at (timestamp)

Task

  1. Compute each product’s total revenue in calendar year 2024 , where revenue for an item is quantity * unit_price .
  2. For each category , return the top 3 products by total revenue in 2024.
  3. If multiple products tie, break ties by smaller product_id first.

Output Return rows with:

  • category
  • product_id
  • total_revenue
  • rank_in_category (1 = highest revenue within the category)

Sort the final output by category, then rank_in_category, then product_id.

Notes/Constraints

  • A product with no sales in 2024 should not appear.
  • Use a solution that involves an aggregation step and a window function (e.g., ROW_NUMBER , RANK , or DENSE_RANK ).

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Coinbase•More Machine Learning Engineer•Coinbase Machine Learning Engineer•Coinbase Coding & Algorithms•Machine Learning Engineer Coding & Algorithms
PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.