Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
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 feedbackSuccess 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 Month-over-Month Revenue Comparison
00:00
5 left

SQL Month-over-Month Revenue Comparison

MediumSQL · PostgreSQL

Problem

Write a SQL query to compare current month revenue with the previous month for each product.

Use the latest month present in sales as the current month. Revenue is calculated as quantity * unit_price; products without sales in either comparison month must still appear.

Output

  1. One row per product, ordered by product_id ascending.
  2. Return product_id, product_name, current_month, previous_month, current_month_revenue, previous_month_revenue, revenue_change, and revenue_change_pct.
  3. Use zero for missing revenue and NULL for the percentage change when previous-month revenue is zero.

Schema

products
ColumnTypeDescription
product_idPKINTUnique product identifier
product_nameVARCHAR(100)Product name
sales
ColumnTypeDescription
sale_idPKINTUnique sale identifier
product_idINTProduct sold
sale_dateDATEDate of sale
quantityINTUnits sold
unit_priceNUMERIC(10,2)Price per unit
Tablesproductssales
Interviewer

Your question is SQL Month-over-Month Revenue Comparison. Start with the requirements and the two 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.