Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Analyze Inventory Shrinkage Drivers

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

Your question is Analyze Inventory Shrinkage Drivers. 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

People First Service Center needs a weekly view of inventory shrinkage to help Operations Managers prioritize loss prevention. Write a PostgreSQL query that summarizes shrinkage by week, store, product category, and store manager, then identifies the two largest loss drivers for each store and week.

Requirements

  1. Calculate shrink units as expected units minus counted units, ignoring overages by treating negative shrinkage as zero.
  2. Calculate monetary loss using shrink units multiplied by unit cost, and aggregate results by week, store, category, and manager.
  3. Use LEFT JOIN operations so audits with an unknown store or an unassigned manager remain visible, using clear fallback labels.
  4. Rank categories within each store and week by monetary loss and return only the top two drivers.

Use audits from January 2025 only. Exclude groups with no positive monetary loss.

Schema

inventory_audits
ColumnTypeDescription
audit_idPKINTInventory audit identifier
audit_dateDATEDate of the inventory audit
store_idINTAudited store identifier
categoryVARCHAR(50)Product category
expected_unitsINTUnits expected from inventory records
counted_unitsINTUnits counted during the audit
unit_costNUMERIC(10,2)Cost per unit
stores
ColumnTypeDescription
store_idPKINTStore identifier
store_nameVARCHAR(100)People First Service Center store name
manager_idINTAssigned manager identifier
managers
ColumnTypeDescription
manager_idPKINTManager identifier
manager_nameVARCHAR(100)Manager name
Tablesinventory_auditsstoresmanagers
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results