Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Top 3 Products by Month

MediumSQL · PostgreSQL00:00
Practice interviewer
In session
5 left
00:00

Your question is Top 3 Products by Month. Start with the requirements and the three tables on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Domo's Data Analyst team wants a monthly view of the products generating the most revenue. Write a PostgreSQL query that calculates product revenue from order line items and returns the top three products for each calendar month.

Requirements

  1. Include only orders with a status of completed.
  2. Calculate revenue as quantity * unit_price, grouped by month and product.
  3. Rank products independently within each month and return the top three, breaking revenue ties by the smaller product_id.
  4. Return the month, product name, revenue, and rank, ordered by month and rank.

Schema

orders
ColumnTypeDescription
order_idPKINTOrder identifier
order_dateDATEDate the order was placed
statusVARCHAR(20)Order processing status
order_items
ColumnTypeDescription
order_item_idPKINTLine item identifier
order_idINTReferences orders.order_id
product_idINTReferences products.product_id
quantityINTNumber of units purchased
unit_priceNUMERIC(10,2)Price per unit at purchase time
products
ColumnTypeDescription
product_idPKINTProduct identifier
product_nameVARCHAR(100)Domo product name
categoryVARCHAR(50)Product category
Tablesordersorder_itemsproducts
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results