Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
SQL Variance vs Target
00:00
5 left

SQL Variance vs Target

MediumSQL · PostgreSQL

Problem

Write a SQL query to compare store-level sales, labor cost, and food waste by week and identify the stores with the largest variance from target.

Assume a store should be evaluated only when actual metrics and positive targets exist for the same week. Define overall variance as the average of the absolute percentage variances across sales, labor cost, and food waste.

Output

  1. Return one row per highest-variance store and week, including ties.
  2. Include week_start, store_id, store_name, the three percentage variances, overall_variance_pct, and variance_rank.
  3. Sort by week_start, then overall_variance_pct descending, then store_id.

Schema

stores
ColumnTypeDescription
store_idPKINTUnique McDonald's store identifier
store_nameVARCHAR(100)Store display name
cityVARCHAR(80)Store city
regionVARCHAR(50)Operating region
weekly_store_metrics
ColumnTypeDescription
metric_idPKINTUnique weekly metrics record
store_idINTReferenced store
week_startDATEMonday starting the reporting week
salesDECIMAL(12,2)Weekly sales amount
labor_costDECIMAL(12,2)Weekly labor cost
food_wasteDECIMAL(12,2)Weekly food waste amount
weekly_store_targets
ColumnTypeDescription
target_idPKINTUnique weekly target record
store_idINTReferenced store
week_startDATEMonday starting the target week
sales_targetDECIMAL(12,2)Target weekly sales
labor_cost_targetDECIMAL(12,2)Target weekly labor cost
food_waste_targetDECIMAL(12,2)Target weekly food waste
Tablesstoresweekly_store_metricsweekly_store_targets
Interviewer

Your question is SQL Variance vs Target. Start with the requirements and the three 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
You need to log in / sign up to run or submit.Ln 1
Run your query to see results here.