Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Break Down Revenue and Cost Variance

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

Your question is Break Down Revenue and Cost Variance. 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

You are given monthly sales and cost data for product lines. Write a PostgreSQL query that breaks the month-over-month change in revenue and cost into key components: price effect, volume effect, and mix effect for each product line. Return the current month, prior month, product line, and the component values needed to explain the change.

Use the provided tables and keep the result at the product-line level. The query should compare each month to the immediately previous month for the same product line.

Schema

product_lines
ColumnTypeDescription
product_line_idPKINTPrimary key for the product line
product_line_nameVARCHAR(100)Readable product line name
monthly_financials
ColumnTypeDescription
financial_idPKINTPrimary key for the monthly financial record
product_line_idINTReferences product_lines.product_line_id
month_startDATEFirst day of the month
units_soldINTUnits sold during the month
revenueDECIMAL(12,2)Monthly revenue amount
costDECIMAL(12,2)Monthly cost amount
Tablesproduct_linesmonthly_financials
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results