Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Window Functions for Rolling Averages

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

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

Dell Technologies uses Dell APEX usage analytics to evaluate engagement across customer signup cohorts. Write a PostgreSQL query that creates a daily user activity series for a fixed reporting period, calculates each user's 7-day rolling average, and ranks users within their cohort for each day.

Requirements

  1. Use 2025-02-01 through 2025-02-05 as the reporting period.
  2. Aggregate multiple usage events for the same user and day. Treat missing or NULL usage as zero.
  3. Include users with no usage events during the reporting period.
  4. Calculate a calendar-based rolling average using the current day and up to the six preceding days.
  5. Rank users within each signup-month cohort and activity date by rolling average descending. Ties must receive the same rank.
  6. Return cohort_month, activity_date, user_id, rolling_avg_units, and cohort_rank, ordered by cohort, date, and rank.

Schema

apex_users
ColumnTypeDescription
user_idPKINTEGERUnique APEX user identifier
signup_dateDATEDate the user joined Dell APEX
regionVARCHAR(30)Customer operating region
apex_usage_events
ColumnTypeDescription
event_idPKINTEGERUnique usage event identifier
user_idINTEGERReferences apex_users.user_id
event_dateDATEDate of the usage event
units_usedINTEGERAPEX capacity units consumed in the event
Tablesapex_usersapex_usage_events
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results