Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Using SQL Window Functions

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

Your question is Using SQL Window Functions. 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

Transport for London wants to monitor short-term changes in passenger entries across its Underground stations. Write a PostgreSQL query that aggregates entries by station and date, then calculates a three-record rolling average for each station.

Requirements

  1. Join passenger_entries to stations to return the station name and line.
  2. Restrict the analysis to entries from 1 January 2025 through 5 January 2025, aggregate duplicate records for the same station and date, and treat NULL entry counts as zero.
  3. Use a window function partitioned by station and ordered by date to calculate the average for the current and previous two recorded service days.
  4. Return the station name, date, daily entries, and rolling average, ordered by station name and date.

Schema

stations
ColumnTypeDescription
station_idPKINTEGERUnique TfL station identifier
station_nameVARCHAR(100)Underground station name
lineVARCHAR(50)Primary Underground line serving the station
passenger_entries
ColumnTypeDescription
entry_idPKINTEGERUnique passenger entry record identifier
station_idINTEGERStation associated with the entry record
entry_dateDATEDate on which entries were recorded
entry_countINTEGERNumber of passenger entries, which may be NULL
Tablesstationspassenger_entries
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results