Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Running Average for Last 5 Days

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

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

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
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results