Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Window Functions for Moving Averages

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

Your question is Window Functions for Moving Averages. 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

Waymo One operations teams monitor rider wait times across operational territories. Write a PostgreSQL query that calculates a rolling average of daily completed-ride wait times for each territory.

Requirements

  1. Join ride wait records to territory metadata and include only completed rides from August 1 through August 3, 2026.
  2. Calculate the average wait time for each territory and service date. PostgreSQL AVG should ignore NULL wait values.
  3. Use a window function to calculate the moving average across the current date and the two preceding reported dates within each territory.
  4. Return territory ID, territory name, service date, daily average wait, and moving average wait, ordered by territory and date. Exclude territories with no matching qualifying ride records.

Schema

territories
ColumnTypeDescription
territory_idPKINTEGEROperational territory identifier
territory_nameVARCHAR(80)Waymo One operational territory name
ride_wait_times
ColumnTypeDescription
wait_idPKINTEGERWait-time record identifier
territory_idINTEGERAssociated operational territory
service_dateDATEDate of the ride
wait_minutesNUMERIC(6,2)Rider wait time in minutes
ride_statusVARCHAR(20)Ride lifecycle status
Tablesterritoriesride_wait_times
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results