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 Rolling Active Users
00:00
5 left

7-Day Rolling Active Users

MediumSQL · PostgreSQL

Problem

StreamWave tracks user activity across its app and wants a daily engagement trend. Write a SQL query to calculate the 7-day rolling average of daily active users.

A user is active on a date if they generated at least one event that day.

Requirements

  1. Count distinct active users per day from the user_events table. Events with a NULL user_id are anonymous and do not count toward active users.
  2. Return one row for every calendar date from the earliest to the latest event date, including dates with no events. Such a date has 0 daily active users.
  3. rolling_7_day_avg is the average of daily_active_users over the current date and the 6 calendar days before it. The first dates in the range have fewer than 6 earlier days, so average only the days available.
  4. Round rolling_7_day_avg to 4 decimal places.
  5. Return the columns event_date, daily_active_users, and rolling_7_day_avg, ordered by event_date ascending.

Schema

user_events
ColumnTypeDescription
event_idPKINTUnique event record ID
user_idINTUser who generated the event; may be NULL for anonymous events
event_nameVARCHAR(50)Type of user event
event_timeTIMESTAMPTimestamp when the event occurred
Tablesuser_events
Interviewer

Your question is 7-Day Rolling Active Users. 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.