Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

SQL Revenue by Product Line

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

Your question is SQL Revenue by Product Line. 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

Milwaukee Tool's Marketing Analytics team needs a monthly view of revenue across product lines, including revenue attributed to valid active campaigns. Write a PostgreSQL query that combines sales, products, and campaigns.

Requirements

  1. Include only completed sales from 2025.
  2. Calculate total revenue as quantity * unit_price, grouped by product line and calendar month.
  3. Count revenue as campaign-attributed only when the referenced campaign is active and the sale date falls within its campaign dates. Treat all other sales as organic.
  4. Return results ordered chronologically by month, then alphabetically by product line.

Schema

sales
ColumnTypeDescription
sale_idPKINTUnique sale identifier
product_idINTReferences products.product_id
campaign_idINTOptional reference to campaigns.campaign_id
sale_dateDATEDate when the sale occurred
quantityINTNumber of units sold
unit_priceNUMERIC(10,2)Price for one unit
sale_statusVARCHAR(20)Sale lifecycle status
products
ColumnTypeDescription
product_idPKINTUnique product identifier
product_nameVARCHAR(150)Milwaukee Tool product name
product_lineVARCHAR(80)Product family or platform
campaigns
ColumnTypeDescription
campaign_idPKINTUnique campaign identifier
campaign_nameVARCHAR(120)Marketing campaign name
channelVARCHAR(40)Primary campaign channel
start_dateDATECampaign start date
end_dateDATECampaign end date
statusVARCHAR(20)Campaign status
Tablessalesproductscampaigns
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results