Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Rolling 7-Day Metric With Windows

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

Your question is Rolling 7-Day Metric With Windows. 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

Business Context

Amazon Seller Central needs a report showing which product categories generate the most completed order revenue for each seller.

Task

Write a PostgreSQL query that joins sellers, categories, and orders to rank each seller's categories by completed order revenue during January 2025.

Requirements

  1. Include only orders with status = 'Completed' and an order_date from 2025-01-01 through 2025-01-31.
  2. Calculate total revenue for each seller and category using SUM(order_amount).
  3. Use DENSE_RANK() to rank categories separately within each seller, with the highest revenue ranked first.
  4. Return the seller, category, total revenue, and category rank. Sort results by seller and rank. Rows with category_rank <= 2 represent each seller's top two revenue ranks, including ties.

Schema

sellers
ColumnTypeDescription
seller_idPKINTUnique seller identifier
seller_nameVARCHAR(100)Seller display name
categories
ColumnTypeDescription
category_idPKINTUnique product category identifier
category_nameVARCHAR(100)Product category name
orders
ColumnTypeDescription
order_idPKINTUnique order identifier
seller_idINTReferences sellers.seller_id
category_idINTReferences categories.category_id
order_dateDATEDate the order was placed
statusVARCHAR(20)Order fulfillment status
order_amountNUMERIC(12,2)Revenue attributed to the order
Tablesorderssellerscategories
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results