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 7-Day Spending SQL
00:00
5 left

Rolling 7-Day Spending SQL

MediumSQL · PostgreSQL

Problem

Given a database schema with user actions and transaction histories, write a SQL query to calculate the rolling 7-day average of user spending for Garner health analytics.

Use completed transactions linked to spending actions. Treat dates without spending as zero and return results only for users with at least one qualifying transaction.

Output

  1. One row per user and spending date
  2. Columns: user_id, spending_date, daily_spending, rolling_7_day_avg
  3. Include dates from each user's first qualifying transaction through their last
  4. Order by user_id, then spending_date

Schema

users
ColumnTypeDescription
user_idPKINTUnique Garner user identifier
user_nameVARCHAR(100)User display name
user_actions
ColumnTypeDescription
action_idPKINTUnique user action identifier
user_idINTUser associated with the action
action_typeVARCHAR(30)Action classification
action_dateDATEDate the action occurred
transactions
ColumnTypeDescription
transaction_idPKINTUnique transaction identifier
user_idINTUser who made the transaction
action_idINTLinked user action
transaction_dateDATEDate the transaction was recorded
amountNUMERIC(12,2)Transaction amount in dollars
statusVARCHAR(20)Transaction processing status
Tablesusersuser_actionstransactions
Interviewer

Your question is Rolling 7-Day Spending SQL. Start with the requirements and the three 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.