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
30-Day Moving Average of Bookings
00:00
5 left

30-Day Moving Average of Bookings

MediumSQL · PostgreSQL

Problem

Business Context

You’re on the analytics engineering team at a two-sided logistics marketplace (think “Uber for warehouse labor”) that processes ~2M task bookings per month across multiple metro areas. Operations leaders track booking volume to forecast staffing needs and to detect demand shocks (weather events, promotions, large enterprise contracts). Daily booking counts are noisy, so the team uses a 30-day moving average as the primary trend metric in dashboards.

Bookings are stored at the event level (one row per booking). However, the dashboard needs a daily time series that includes days with zero bookings (e.g., holidays or system outages), because missing dates can make the moving average misleading.

Task

Write a SQL query to compute the daily booking count and the 30-day moving average of daily bookings.

Requirements

  1. Produce one row per calendar day between 2024-01-01 and 2024-01-07 (inclusive) using the dim_dates table.
  2. Count bookings by the date portion of booked_at (treat booked_at as UTC).
  3. Include days with 0 bookings.
  4. Compute ma_30d_bookings as the average of daily_bookings over the current day and the prior 29 days (a 30-day trailing window).
  5. Return columns: dt, daily_bookings, ma_30d_bookings.
  6. Order results by dt ascending.

Schema

dim_dates
ColumnTypeDescription
dtPKDATECalendar date (one row per day)
task_bookings
ColumnTypeDescription
booking_idPKBIGINTUnique booking identifier
task_idBIGINTTask being booked
booked_atTIMESTAMPTimestamp when the booking was created (UTC)
statusVARCHAR(20)Booking state (e.g., confirmed, cancelled)
Tablesdim_datestask_bookings
Interviewer

Your question is 30-Day Moving Average of Bookings. 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.