Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
Top Products by Monthly Revenue
00:00
5 left

Top Products by Monthly Revenue

MediumSQL · PostgreSQL

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
Interviewer

Your question is Top Products by Monthly Revenue. Start with the requirements and the three tables in the Question tab.

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.
CodePostgreSQL
Sign up free to run your codeLog inLn 1
Run your query to see results here.