Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Trending in AI
Hot
Agentic AI EngineerForward-Deployed EngineerGenAI EngineerAI Architect
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
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.

Output details

  • Return columns in this order: display_name, engagement_date, daily_engagement_minutes, running_total, moving_avg_3_days.
  • Order rows by display_name ascending, then engagement_date ascending.

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

Whenever you're ready, walk me through your approach or go straight to the code. The requirements and the two tables 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.