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

7-Day Moving Average of Daily GMV

EasySQL · PostgreSQL

Problem

Business Context

You’re a data engineer supporting the analytics team at a large e-commerce marketplace (~5M daily active users, ~10M order items/day). Finance uses daily GMV (gross merchandise value) to forecast revenue and detect demand shocks. However, daily GMV is noisy due to promotions and weekend effects, so stakeholders want a 7-day moving average to smooth the trend.

The source of truth is an orders table where each row is an order (not an item). Orders can be paid, refunded, or canceled. Only paid orders should contribute to GMV.

Task

Write a SQL query that returns daily paid GMV and its 7-day trailing moving average (including the current day) for a given date range.

Requirements

  1. Aggregate daily GMV as the sum of order_total_usd for orders where status = 'paid'.
  2. Return one row per order_date (UTC date) within 2024-01-01 to 2024-01-10 (inclusive).
  3. Compute gmv_7d_moving_avg as the average of daily_gmv_usd over the window current day and previous 6 days.
  4. If fewer than 7 prior days exist (early in the range), compute the average over the available days.
  5. Order results by order_date ascending.

Schema

orders
ColumnTypeDescription
order_idPKBIGINTPrimary key
user_idBIGINTBuyer user id
order_tsTIMESTAMPOrder creation timestamp in UTC
statusVARCHAR(20)Order status: paid, refunded, canceled
order_total_usdDECIMAL(10,2)Total order amount in USD
Tablesorders
Interviewer

Your question is 7-Day Moving Average of Daily GMV. Start with the requirements and the one table 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.