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
Window Functions for Engagement
00:00
5 left

Window Functions for Engagement

MediumSQL · PostgreSQL

Problem

AARP.org wants to monitor engagement trends for active users. Write a PostgreSQL query that aggregates engagement by user and day, then calculates both cumulative engagement and a three-day moving average.

Requirements

  1. Join users to engagement events and include only users whose account_status is active.
  2. Aggregate engagement minutes by user and engagement_date for January 1 through January 5, 2025. Treat a day containing only NULL minutes as zero.
  3. Calculate each user's running total using a window function ordered by date.
  4. Calculate a three-row moving average of daily engagement, including the current day and up to the two preceding observed engagement dates. Round it to two decimal places.

Schema

users
ColumnTypeDescription
user_idPKINTEGERUnique user identifier
display_nameVARCHAR(100)User's display name
account_statusVARCHAR(20)Current account status
engagement_events
ColumnTypeDescription
event_idPKINTEGERUnique engagement event identifier
user_idINTEGERReferences users.user_id
engagement_dateDATEDate of the engagement event
engagement_minutesINTEGERMinutes attributed to the event
Tablesusersengagement_events
Interviewer

Your question is Window Functions for Engagement. 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.