Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

3-Month Rolling Average in SQL

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

Your question is 3-Month Rolling Average in SQL. 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

The wants to monitor monthly engagement across its user-facing products. Write a PostgreSQL query that calculates each user's rolling average number of interactions for the three calendar months ending in April 2025.

Requirements

  1. Aggregate interaction events by user and calendar month.
  2. Include every user and every month from January through April 2025, including users or months with no interactions.
  3. Use a window function to calculate the average for the current month and the two preceding calendar months.
  4. Return only the April 2025 result, with one row per user.
  5. Treat missing monthly interaction counts as zero, and sort by user_id.

Schema

users
ColumnTypeDescription
user_idPKINTEGERUnique user identifier
user_nameVARCHAR(100)User display name
signup_dateDATEDate the user registered
user_interactions
ColumnTypeDescription
interaction_idPKINTEGERUnique interaction identifier
user_idINTEGERUser associated with the interaction
interaction_atTIMESTAMPTimestamp when the interaction occurred
interaction_typeVARCHAR(40)Interaction category
Tablesusersuser_interactions
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results