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

Window Functions for Rolling Averages

HardSQL · PostgreSQL

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.

Output details

  • Include only users with non-NULL signup dates on or before 2025-02-05.
  • Round rolling_avg_units to two decimal places.
  • Order by cohort_month ascending, activity_date ascending, cohort_rank ascending, then user_id ascending.

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
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.