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
SQL Revenue by Product Line
00:00
5 left

SQL Revenue by Product Line

MediumSQL · PostgreSQL

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
Interviewer

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