Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
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
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess 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
Build my plan
3-Month Rolling Average in SQL
00:00
5 left

3-Month Rolling Average in SQL

MediumSQL · PostgreSQL

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
Interviewer

Your question is 3-Month Rolling Average in SQL. Start with the requirements and the two tables in the Question tab.

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.
CodePostgreSQL
Sign up free to run your codeLog inLn 1
Run your query to see results here.