Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Trending in AI
Hot
Agentic AI EngineerForward-Deployed EngineerGenAI EngineerAI Architect
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
ExperiencesCoaching
New
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
Start free
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

Whenever you're ready, walk me through your approach or go straight to the code. The requirements and the three tables are in the Question tab.

Sign up free to run or submit. Log in
CodePostgreSQL
Sign up free to run your code. Your draft is saved.Run with a free accountLog inLn 1
Run your query to see results here.