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
ExperiencesCoaching
New
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
Start free
7-Day Rolling Average Balance Query
00:00
5 left

7-Day Rolling Average Balance Query

MediumSQL · PostgreSQL

Problem

Given a table of daily user balances, write a query to calculate the 7-day rolling average balance for every user at Current (NY).

Assume each row represents a user's balance on a calendar date. The seven-day period includes the current date and six preceding calendar days; missing dates are excluded, and NULL balances are ignored.

Output

  1. One row per user and balance date.
  2. Columns: user_id, balance_date, and rolling_avg_balance.
  3. Sort by user_id, then balance_date ascending.

Output details

  • Round rolling_avg_balance to 2 decimal places.
  • Return NULL when a user's seven-day window contains no non-NULL balances.
  • Preserve duplicate input rows rather than deduplicating user-date records.

Schema

daily_user_balances
ColumnTypeDescription
balance_idPKINTUnique balance record identifier
user_idINTCurrent user identifier
balance_dateDATEDate represented by the balance
balanceDECIMAL(12,2)User balance on the recorded date
Tablesdaily_user_balances
Interviewer

Whenever you're ready, walk me through your approach or go straight to the code. The requirements and the one table are in the Question tab.

Sign up free to run or submit. Log in
CodePostgreSQL
Sign up free to run your code. Your draft is saved.Run with a free accountLog inLn 1
Run your query to see results here.