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
SQL for Running Average
00:00
5 left

SQL for Running Average

MediumSQL · PostgreSQL

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
Interviewer

Your question is SQL for Running Average. 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.