Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

SQL Rolling Defect Rate Query

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

Your question is SQL Rolling Defect Rate Query. Start with the requirements and the two 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

Mercedes-Benz Group operations teams need a daily quality metric across active manufacturing lines. Write a PostgreSQL query that calculates the overall defect rate for each production date and its rolling seven-day average.

Requirements

  1. Include production from active lines only. Lines with is_active = FALSE or NULL must be excluded.
  2. Calculate each daily defect rate as total defective units divided by total produced units across all active lines.
  3. Calculate a rolling average over the current date and the preceding six calendar days using a window function.
  4. Return dates in ascending order and round both rates to six decimal places.

Schema

manufacturing_lines
ColumnTypeDescription
line_idPKINTEGERManufacturing line identifier
line_nameVARCHAR(100)Mercedes-Benz production line name
plant_codeVARCHAR(20)Manufacturing plant code
is_activeBOOLEANWhether the line is currently active
daily_production
ColumnTypeDescription
production_idPKINTEGERProduction record identifier
line_idINTEGERManufacturing line associated with the record
production_dateDATEProduction date
units_producedINTEGERUnits produced on the line
defective_unitsINTEGERUnits identified as defective
maintenance_noteTEXTOptional production or maintenance note
Tablesmanufacturing_linesdaily_production
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results