Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
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 feedbackSuccess 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
Using SQL Window Functions
00:00
5 left

Using SQL Window Functions

MediumSQL · PostgreSQL

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
Interviewer

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