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
Write Rolling Average SQL
00:00
5 left

Write Rolling Average SQL

MediumSQL · PostgreSQL

Problem

Engie Impact uses customer energy data to monitor consumption patterns and support operational planning. Write a PostgreSQL query that calculates a 7-day rolling average of daily energy usage for each customer.

The rolling average should average recorded daily totals within the current date and the preceding six calendar days. Days without readings are not included as zero-usage days.

Requirements

  1. Aggregate multiple readings for the same customer and date into one daily usage total.
  2. Calculate the rolling average separately for each customer using a window function and a six-day preceding date range.
  3. Include the customer ID, customer name, usage date, daily usage in kWh, and rolling average in kWh.
  4. Exclude readings with no matching customer, while allowing NULL usage values to be ignored by the aggregate functions.
  5. Order the results by customer ID and usage date.

Schema

customers
ColumnTypeDescription
customer_idPKINTUnique customer identifier
customer_nameVARCHAR(100)Customer or account name
energy_usage
ColumnTypeDescription
reading_idPKINTUnique usage reading identifier
customer_idINTCustomer associated with the reading
usage_dateDATEDate of energy usage
usage_kwhNUMERIC(12,2)Energy consumed in kilowatt-hours
Tablescustomersenergy_usage
Interviewer

Your question is Write Rolling Average SQL. 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.