Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
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
You need to log in / sign up to run or submit.Ln 1
Run your query to see results here.