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
Rolling Ticket Sales Query
00:00
5 left

Rolling Ticket Sales Query

MediumSQL · PostgreSQL

Problem

MLB wants a daily view of ticket sales for each stadium, including dates when a stadium has no game. Write a PostgreSQL query for April 1 through April 3, 2024.

Requirements

  1. Generate every date and stadium combination in the reporting range.
  2. Include only ticket sales from completed MLB games.
  3. Treat missing sales as zero, then calculate each stadium's rolling seven-calendar-day average. Because the date spine is complete, the window must include zero-sales dates.
  4. Return stadium name, date, daily ticket sales, and the rolling average, rounded to two decimals.
  5. Order by stadium name and report date.

Schema

stadiums
ColumnTypeDescription
stadium_idPKINTUnique stadium identifier
stadium_nameVARCHAR(100)MLB stadium name
cityVARCHAR(100)City where the stadium is located
games
ColumnTypeDescription
game_idPKINTUnique game identifier
stadium_idINTStadium hosting the game
game_dateDATEScheduled game date
statusVARCHAR(20)Game status, such as Completed or Postponed
ticket_sales
ColumnTypeDescription
sale_idPKINTUnique ticket-sale record identifier
game_idINTGame associated with the sale
tickets_soldINTNumber of tickets in the sale record
Tablesstadiumsgamesticket_sales
Interviewer

Your question is Rolling Ticket Sales Query. Start with the requirements and the three 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.