Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Window Functions for Engagement

MediumSQL · PostgreSQL00:00
Practice interviewer
In session
5 left
00:00

Your question is Window Functions for Engagement. Start with the requirements and the two tables on the right.

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.

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
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results