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
ExperiencesCoaching
New
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
Start free
SQL KPI Trends With YoY
00:00
5 left

SQL KPI Trends With YoY

MediumSQL · PostgreSQL

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.

Output details

  • Filter deliveries to records whose status is exactly Delivered.
  • Count each qualifying delivery row as one delivered_units.
  • Sum qualifying net_revenue values for total_revenue, reported to two decimal places.
  • Calculate revenue_per_vehicle as total revenue divided by delivered units, rounded to two decimal places.
  • Compare each month with the same calendar month one year earlier.
  • Round both year-over-year percentage values to two decimal places and sort months ascending.

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
Interviewer

Whenever you're ready, walk me through your approach or go straight to the code. The requirements and the three tables are in the Question tab.

Sign up free to run or submit. Log in
CodePostgreSQL
Sign up free to run your code. Your draft is saved.Run with a free accountLog inLn 1
Run your query to see results here.