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
Three-Month Moving Averages by User
00:00
5 left

Three-Month Moving Averages by User

MediumSQL · PostgreSQL

Problem

Arity analyzes monthly mileage from connected-vehicle telemetry to support driver risk and usage insights. Write a PostgreSQL query that calculates a running three-month moving average of mileage for each active driver.

Requirements

  1. Include only drivers whose is_active value is TRUE.
  2. Return the driver ID, calendar month, reported mileage, and moving average.
  3. Partition calculations by user_id and order each driver's records chronologically.
  4. Use the current month and up to the two preceding monthly records, rounding the average to two decimal places. NULL mileage values should be ignored by AVG.

Assume arity_monthly_mileage contains at most one row per driver per calendar month.

Schema

arity_users
ColumnTypeDescription
user_idPKINTEGERUnique driver identifier
user_nameVARCHAR(100)Driver display name
is_activeBOOLEANWhether the driver is currently active
arity_monthly_mileage
ColumnTypeDescription
mileage_idPKINTEGERUnique mileage record identifier
user_idINTEGERReferences arity_users.user_id
month_startDATEFirst calendar day of the measurement month
miles_drivenNUMERIC(10,1)Total miles driven during the month
Tablesarity_usersarity_monthly_mileage
Interviewer

Your question is Three-Month Moving Averages by User. 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.