Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
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 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
Build my plan
Rolling Failure Rate With SQL
00:00
5 left

Rolling Failure Rate With SQL

MediumSQL · PostgreSQL

Problem

Business Context

Atlas Copco SMARTLINK provides operational data for monitoring industrial equipment. The service team wants to identify machines with elevated failure rates by tracking daily rates and their rolling 30-day averages.

Task

Write a PostgreSQL query that calculates the daily failure rate and rolling average failure rate for active SMARTLINK-connected machines between 2025-01-01 and 2025-02-28.

Requirements

  1. Join machine metadata with daily operational metrics and include only active machines.
  2. Calculate the daily failure rate as failures divided by inspections, expressed as a percentage. If inspections are zero or the failure count is unavailable, return NULL.
  3. Use a window function to calculate the average daily failure rate over the current date and the preceding 29 calendar days for each machine.
  4. Return results ordered by machine and metric date. A missing failure count must remain unavailable rather than being treated as zero.

Schema

smartlink_machines
ColumnTypeDescription
machine_idPKINTEGERUnique machine identifier
machine_nameVARCHAR(100)Machine name shown in SMARTLINK
is_activeBOOLEANWhether the machine is currently active
machine_daily_metrics
ColumnTypeDescription
metric_idPKINTEGERUnique daily metric record
machine_idINTEGERReferenced machine identifier
metric_dateDATEDate of the measurements
inspection_countINTEGERNumber of inspections
failure_countINTEGERNumber of failures, possibly unavailable
Tablessmartlink_machinesmachine_daily_metrics
Interviewer

Your question is Rolling Failure Rate With SQL. 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.