Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

SQL: Store Performance and Margin

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

Your question is SQL: Store Performance and Margin. 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

Business Context

CVS Health operations leaders need a store-level view of sales, profitability, and inventory health across regions. The analysis should preserve stores with no sales or inventory snapshot so operational gaps remain visible.

Task

Write a PostgreSQL query for Q1 2025 that combines stores, sales, and inventory data without double-counting sales because of multiple inventory snapshots.

Requirements

  1. Aggregate net sales and cost by store, then calculate margin percentage.
  2. Select each store's most recent inventory snapshot in the period using a window function.
  3. Calculate regional average sales and margin, plus each store's sales rank within its region.
  4. Mark a store as UNDERPERFORMING when sales are below 75% of its regional average, margin is below the regional average, or latest inventory units equal zero. Otherwise mark it PERFORMING.
  5. Retain stores with no sales and order results by region, sales rank, and store ID.

Representative Data

Schema

stores
ColumnTypeDescription
store_idPKINTUnique CVS Health store identifier
store_nameVARCHAR(100)Store display name
regionVARCHAR(50)Operating region
sales
ColumnTypeDescription
sale_idPKINTUnique sales transaction identifier
store_idINTStore associated with the transaction
sale_dateDATETransaction date
net_salesNUMERIC(12,2)Net transaction sales amount
costNUMERIC(12,2)Cost of goods sold
inventory
ColumnTypeDescription
inventory_idPKINTUnique inventory snapshot identifier
store_idINTStore associated with the snapshot
snapshot_dateDATEDate inventory was measured
inventory_unitsINTUnits available at the snapshot
Tablesstoressalesinventory
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results