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
Get started
SQL for Hub Performance
00:00
5 left

SQL for Hub Performance

MediumSQL · PostgreSQL

Problem

Write a SQL query to analyze hub performance by joining orders, deliveries, and inventory tables to calculate daily delivery TAT, wastage percentage, and stock-out rate.

Use completed deliveries for TAT, inventory quantities for wastage, and stock-out minutes over a 24-hour operating day. Include dates represented by any source table.

Output

  1. One row per hub and calendar date.
  2. Columns: hub_id, performance_date, avg_delivery_tat_minutes, wastage_percentage, and stock_out_rate.
  3. Include available hub-date combinations even when one metric has no source data. Sort by hub_id, then performance_date.

Output details

  • Treat deliveries with status delivered, requiring non-NULL dispatched_at and delivered_at, and derive their date from delivered_at.
  • Use inventory_date as the inventory performance date.
  • Round all three metric values to exactly 2 decimal places.
  • Calculate wastage percentage as 100 times total wastage_qty divided by total opening_stock + received_qty, returning NULL for a zero denominator.
  • Calculate stock-out rate as 100 times total stockout_minutes divided by 1440, without clamping negative values.
  • Sort hub_id and performance_date in ascending order and retain each hub-date combination only once.

Schema

orders
ColumnTypeDescription
order_idPKINTUnique order identifier
hub_idVARCHAR(10)Fulfillment hub identifier
order_dateDATEDate the order was placed
deliveries
ColumnTypeDescription
delivery_idPKINTUnique delivery identifier
order_idINTRelated order identifier
dispatched_atTIMESTAMPTimestamp when the delivery was dispatched
delivered_atTIMESTAMPTimestamp when the order was delivered
statusVARCHAR(20)Delivery status
inventory
ColumnTypeDescription
inventory_idPKINTUnique inventory record identifier
hub_idVARCHAR(10)Inventory hub identifier
inventory_dateDATEInventory measurement date
opening_stockINTUnits available at the start of the day
received_qtyINTUnits received during the day
wastage_qtyINTUnits discarded during the day
stockout_minutesINTMinutes without available stock
Tablesordersdeliveriesinventory
Interviewer

Your question is SQL for Hub Performance. 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
Sign up free to run your codeLog inLn 1
Run your query to see results here.