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

Running Average for Last 5 Days

EasySQL · PostgreSQL

Problem

EXL Service Philippines analysts need a daily sales metric for an operations reporting dashboard. Write a PostgreSQL query that calculates the running average of sales for the current day and the four preceding calendar days.

Requirements

  1. Return every date in reporting_calendar, including dates with no transactions.
  2. Calculate daily_amount, treating missing transaction days as zero and ignoring NULL transaction amounts.
  3. Calculate five_day_running_avg using the current date and up to four preceding calendar dates.
  4. Round the running average to two decimal places and sort the output chronologically.

The window covers the current date and the 4 calendar days before it; a date that is not in reporting_calendar is simply absent from the window (the average is over the calendar dates present).

Schema

sales_transactions
ColumnTypeDescription
transaction_idPKINTUnique transaction identifier
transaction_dateDATEDate on which the transaction occurred
amountNUMERIC(12,2)Sales amount for the transaction
reporting_calendar
ColumnTypeDescription
calendar_datePKDATECalendar date required in the report
Tablessales_transactionsreporting_calendar
Interviewer

Your question is Running Average for Last 5 Days. 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.