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 Production and Quality Rates
00:00
5 left

SQL for Production and Quality Rates

MediumSQL · PostgreSQL

Problem

Write a SQL query to calculate weekly production output, defect rate, and on-time shipment rate by plant and product line.

Use production_runs, shipments, and plants. Define the production week from production_date and the shipment week from promised_ship_date. Defect rate is defective units divided by produced units. On-time shipment rate is units shipped on or before the promised date divided by total units shipped. Include weeks represented by either production or shipments.

Output

  1. One row per plant, product line, and week.
  2. Return plant_name, product_line, week_start, production_output, defect_rate, and on_time_shipment_rate.
  3. Sort by plant name, product line, and week start ascending.

Output details

  • Use Monday as the start of each calendar week, with week_start representing that Monday.
  • Round defect_rate and on_time_shipment_rate to 4 decimal places.
  • Return NULL for either rate when its denominator is zero.
  • Treat NULL defect_units as zero when calculating defect_rate.
  • Treat shipments with NULL actual_ship_date as not on time.
  • Return NULL for metrics when the corresponding plant, product line, and week has no activity.

Schema

plants
ColumnTypeDescription
plant_idPKINTUnique plant identifier
plant_nameVARCHAR(100)Plant name
regionVARCHAR(50)Geographic region
production_runs
ColumnTypeDescription
run_idPKINTUnique production run identifier
plant_idINTPlant where production occurred
product_lineVARCHAR(80)Product line manufactured
production_dateDATEProduction date
units_producedINTUnits produced in the run
defect_unitsINTUnits identified as defective
shipments
ColumnTypeDescription
shipment_idPKINTUnique shipment identifier
plant_idINTPlant shipping the units
product_lineVARCHAR(80)Product line shipped
promised_ship_dateDATECustomer-promised shipment date
actual_ship_dateDATEActual shipment date
units_shippedINTUnits included in the shipment
Tablesplantsproduction_runsshipments
Interviewer

Whenever you're ready, walk me through your approach or go straight to the code. The requirements and the three tables are in the Question tab.

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.