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 30-Day Sales Volume
00:00
5 left

Rolling 30-Day Sales Volume

MediumSQL · PostgreSQL

Problem

Write a SQL query to calculate the rolling 30-day sales volume for each product.

Requirements

  1. Only count sales whose product_id exists in products. Sales with an unknown or NULL product_id are excluded.
  2. daily_sales_volume is the total quantity for a product on a sale_date. Negative quantities are returns and reduce the total. NULL quantities are ignored; if every quantity for that product and date is NULL, daily_sales_volume is NULL, but the row is still returned.
  3. rolling_30_day_sales_volume is the sum of the product's daily volumes over the current sale_date and the 29 calendar days before it. This is a calendar window, not the last 30 rows. NULL daily volumes are ignored; if every daily volume in the window is NULL, the result is NULL.
  4. Return one row per product and date that has at least one sale. Products with no sales do not appear.
  5. Columns: product_id, product_name, sales_date (the sale_date value), daily_sales_volume, and rolling_30_day_sales_volume.
  6. Sort by product_id, then sales_date ascending.

Schema

products
ColumnTypeDescription
product_idPKINTUnique inventory product identifier
product_nameVARCHAR(100)Product name
skuVARCHAR(40)Stock keeping unit
categoryVARCHAR(60)Product category
sales
ColumnTypeDescription
sale_idPKINTUnique sales record identifier
product_idINTReferenced inventory product identifier
sale_dateDATECalendar date of the sale
quantityINTUnits sold in the record
Tablesproductssales
Interviewer

Your question is Rolling 30-Day Sales Volume. 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.