Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Rolling Ticket Sales Query

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

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

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
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results