Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Top Products by Monthly Revenue

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

Your question is Top Products by Monthly Revenue. 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

Wealthfront wants a monthly view of which products generate the most net revenue. Write a PostgreSQL query using layered CTEs and a window function to identify the top three products for each month.

Requirements

  1. Include only completed transactions occurring from January 1 through March 31, 2025.
  2. Join transactions to products and product categories, then aggregate net revenue by calendar month and product.
  3. Treat refunds as negative revenue and use COALESCE so nullable transaction amounts do not produce unexpected totals.
  4. Use ROW_NUMBER() partitioned by month and ordered by revenue descending, with product_id as a deterministic tie-breaker.
  5. Return only the top three products per month, ordered chronologically and then by rank.

Schema

transactions
ColumnTypeDescription
transaction_idPKINTEGERUnique transaction identifier
product_idINTEGERReferenced Wealthfront product
occurred_atTIMESTAMPTransaction timestamp
amountNUMERIC(12,2)Signed sale or refund amount
transaction_typeVARCHAR(20)Transaction classification
statusVARCHAR(20)Processing status
products
ColumnTypeDescription
product_idPKINTEGERUnique product identifier
product_nameVARCHAR(100)Wealthfront product name
category_idINTEGERReferenced product category
product_categories
ColumnTypeDescription
category_idPKINTEGERUnique category identifier
category_nameVARCHAR(100)Product category name
Tablestransactionsproductsproduct_categories
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results