Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
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
You need to log in / sign up to run or submit.Ln 1
Run your query to see results here.