Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

SQL Month-over-Month Revenue Comparison

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

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

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
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results