Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Top Products Rolling Window SQL

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

Your question is Top Products Rolling Window SQL. Start with the requirements and the two 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

Bain & Company's product analytics team uses Bain Insights to monitor recent e-commerce performance. Write a PostgreSQL query that identifies the highest-grossing products in each category over a rolling 30-day period ending on 2025-02-15.

Requirements

  1. Join order_items to products and calculate gross revenue as quantity * unit_price.
  2. Include only order items from 2025-01-16 through 2025-02-15, inclusive.
  3. Aggregate revenue by product and category, rank products within each category, and return the top three products per category.
  4. Return category, product_name, gross_revenue, and the rank, ordered by category and rank.

Schema

products
ColumnTypeDescription
product_idPKINTUnique product identifier
product_nameVARCHAR(100)Product name
categoryVARCHAR(50)Product category
order_items
ColumnTypeDescription
order_item_idPKINTUnique order line identifier
product_idINTReferenced product identifier
order_dateDATEDate on which the item was purchased
quantityINTNumber of units purchased
unit_priceDECIMAL(10,2)Price per purchased unit
Tablesproductsorder_items
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results