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 Before-After Policy Change
00:00
5 left

SQL Before-After Policy Change

MediumSQL · PostgreSQL

Problem

Write a SQL query to compare maintenance events before and after a policy change using CTEs and date-based aggregations.

Use the policy change with change_id = 1 and exclude maintenance events whose date is unknown.

Output

  1. One row per period and calendar month.
  2. Return period, event_month, event_count, total_downtime_hours, and average_cost_usd.
  3. Label periods before or after, and order by month followed by period.

Schema

policy_changes
ColumnTypeDescription
change_idPKINTUnique identifier for a policy change
policy_nameVARCHAR(100)Name of the changed maintenance policy
effective_dateDATEDate when the policy became effective
maintenance_events
ColumnTypeDescription
event_idPKINTUnique identifier for a maintenance event
aircraft_idVARCHAR(20)Aircraft associated with the event
event_dateDATEDate on which maintenance occurred
event_typeVARCHAR(50)Maintenance event classification
downtime_hoursDECIMAL(8,2)Aircraft downtime attributed to the event
cost_usdDECIMAL(12,2)Maintenance cost in US dollars
Tablespolicy_changesmaintenance_events
Interviewer

Your question is SQL Before-After Policy Change. 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.