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 Incident Trends Query
00:00
5 left

SQL Incident Trends Query

HardSQL · PostgreSQL

Problem

Write a SQL query to track incident counts by site and week, including a rolling 4-week average and week-over-week change.

Use the provided sites, calendar_weeks, and incidents tables. Include every site for every listed week, including weeks with zero incidents. Week-over-week change means the current count minus the previous week's count.

Output

  1. One row per site and calendar week.
  2. Columns: site_code, week_start, incident_count, rolling_4_week_avg, and week_over_week_change.
  3. Sort by site_code, then week_start ascending. The first week's change for each site should be NULL.

Schema

sites
ColumnTypeDescription
site_idPKINTUnique site identifier
site_codeVARCHAR(20)Short site code used in reporting
site_nameVARCHAR(100)Site display name
calendar_weeks
ColumnTypeDescription
week_startPKDATEMonday starting the reporting week
incidents
ColumnTypeDescription
incident_idPKINTUnique incident identifier
site_idINTSite where the incident occurred
occurred_atTIMESTAMPTimestamp when the incident occurred
severityVARCHAR(20)Incident severity
Tablessitescalendar_weeksincidents
Interviewer

Your question is SQL Incident Trends Query. 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.