Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

SQL for Running Average

MediumSQL · PostgreSQL00:00
Practice interviewer
In session
5 left
00:00

Your question is SQL for Running Average. Start with the requirements and the two tables on the right.

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.

Problem

EXL Service uses an operations dashboard to monitor daily service performance. Write a PostgreSQL query that calculates a rolling average for the current date and the previous four calendar days.

Requirements

  1. First calculate the average metric value for each date because multiple observations can occur on the same date.
  2. Use the calendar table so dates without observations remain in the result.
  3. Calculate the five-day rolling average with a window function ordered by calendar date. Ignore missing or NULL daily averages when calculating the average.
  4. Round both the daily average and rolling average to two decimal places, and return results in chronological order.

Schema

exl_service_calendar
ColumnTypeDescription
calendar_datePKDATECalendar date used to preserve dates without observations
service_daily_metrics
ColumnTypeDescription
metric_idPKINTUnique service metric observation identifier
metric_dateDATEDate on which the observation was recorded
metric_nameVARCHAR(50)Name of the monitored service metric
metric_valueDECIMAL(10,2)Observed metric value
Tablesexl_service_calendarservice_daily_metrics
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results