Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

SQL KPI Trends With YoY

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

Your question is SQL KPI Trends With YoY. 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

Write a SQL query to calculate monthly KPI trends with year-over-year comparison for Volvo Group.

Use completed deliveries for valid Volvo Group vehicle models sold through active dealer locations. Include months with no prior-year comparison, and calculate delivery volume, revenue, revenue per vehicle, and year-over-year percentage changes.

Output

  1. One row per calendar month with columns month_start, delivered_units, total_revenue, revenue_per_vehicle, prior_year_units, prior_year_revenue, units_yoy_pct, and revenue_yoy_pct.
  2. Include all qualifying months, sort chronologically, and return NULL percentage values when no prior-year baseline exists or the baseline is zero.

Schema

vehicle_deliveries
ColumnTypeDescription
delivery_idPKINTUnique delivery identifier
model_idINTVehicle model reference
dealer_idINTDealer location reference
delivery_dateDATEDate of delivery
statusVARCHAR(20)Delivery status
net_revenueNUMERIC(14,2)Net revenue recognized for the delivery
vehicle_models
ColumnTypeDescription
model_idPKINTUnique vehicle model identifier
model_nameVARCHAR(80)Vehicle model name
vehicle_typeVARCHAR(40)Vehicle category
brandVARCHAR(60)Vehicle brand
dealer_locations
ColumnTypeDescription
dealer_idPKINTUnique dealer identifier
dealer_nameVARCHAR(100)Dealer name
countryVARCHAR(50)Dealer country
regionVARCHAR(50)Commercial region
activeBOOLEANWhether the dealer is currently active
Tablesvehicle_deliveriesvehicle_modelsdealer_locations
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results