Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Investigate Inventory Below Threshold

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

Your question is Investigate Inventory Below Threshold. Start with the requirements and the four 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

Cardinal Health distribution operations need to identify products whose projected end-of-day inventory fell below the minimum stock threshold during the most recent seven calendar days. The inventory snapshot is recorded before that day's outgoing shipments.

Write a PostgreSQL query using the latest inventory_date as the reporting date.

Requirements

  1. Restrict inventory and outgoing shipments to the latest date and preceding six calendar days.
  2. Calculate projected stock as on_hand_quantity - outgoing shipment quantity for each product, distribution center, and day.
  3. Use a window function to identify the first day each product fell below its threshold.
  4. Return only products with a threshold breach, including the breach date, minimum projected stock, threshold, and total shipments during the seven-day period.
  5. Include products with no shipments by treating shipment quantity as zero, and order results by breach date and product ID.

Representative data

warehouse_inventory contains daily snapshots for products P-1001, P-1002, and P-1003. outgoing_shipments contains shipments inside and outside the reporting window, including a shipment with a NULL quantity.

Schema

warehouse_inventory
ColumnTypeDescription
inventory_idPKINTUnique inventory snapshot row
product_idVARCHAR(20)Cardinal Health product identifier
center_idVARCHAR(20)Distribution center identifier
inventory_dateDATEInventory snapshot date
on_hand_quantityINTUnits available before that day's shipments
minimum_stock_thresholdINTMinimum acceptable inventory level
outgoing_shipments
ColumnTypeDescription
shipment_idPKINTUnique outgoing shipment identifier
product_idVARCHAR(20)Shipped product identifier
center_idVARCHAR(20)Shipping distribution center
shipment_dateDATEShipment dispatch date
quantityINTNumber of units shipped
product_catalog
ColumnTypeDescription
product_idPKVARCHAR(20)Product identifier
product_nameVARCHAR(150)Product description
distribution_centers
ColumnTypeDescription
center_idPKVARCHAR(20)Distribution center identifier
center_nameVARCHAR(150)Distribution center name
Tableswarehouse_inventoryoutgoing_shipmentsproduct_catalogdistribution_centers
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results