Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Trending in AI
Hot
Agentic AI EngineerForward-Deployed EngineerGenAI EngineerAI Architect
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
Moving Average Calculation
00:00
5 left

Moving Average Calculation

MediumSQL · PostgreSQL

Problem

Calculate a moving average for TaskRabbit metrics using SQL.

Use the taskrabbit_metrics table and calculate a three-observation moving average for completed_tasks. Ignore rows where the metric value is NULL.

Output

  1. One row per completed_tasks observation.
  2. Columns: metric_date, metric_name, and moving_average.
  3. Round moving_average to two decimal places and order by metric_date ascending.

Output details

  • Use metric_value as the numeric field for completed_tasks.
  • Include the current observation and the two preceding rows in metric_date order, counting rows rather than dates.
  • Retain rows with NULL metric_value in the output, but exclude those values from averaging.
  • For equal metric_date values, apply no secondary tie-breaker.

Schema

taskrabbit_metrics
ColumnTypeDescription
metric_idPKINTUnique metric observation identifier
metric_nameVARCHAR(100)Name of the TaskRabbit metric
metric_dateDATEDate of the metric observation
metric_valueDECIMAL(12,2)Observed metric value
Tablestaskrabbit_metrics
Interviewer

Whenever you're ready, walk me through your approach or go straight to the code. The requirements and the one table 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.