Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
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
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess 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
Build my plan
SQL Rolling Defect Rate Query
00:00
5 left

SQL Rolling Defect Rate Query

MediumSQL · PostgreSQL

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
Interviewer

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