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
SQL Rolling Average with Windows
00:00
5 left

SQL Rolling Average with Windows

MediumSQL · PostgreSQL

Problem

Business context

Harnham wants to monitor daily applications to its Recruiter Cloud platform and identify whether recent activity is improving compared with the previous week.

Task

Write a PostgreSQL query that joins the daily metric records to the product catalog, calculates a seven-day rolling average of applications, and compares each available day with the rolling average from seven days earlier.

Requirements

  1. Filter to the Harnham Recruiter Cloud product and the applications metric.
  2. Calculate a seven-calendar-day rolling average using the metric date, ignoring NULL metric values through PostgreSQL aggregate behavior.
  3. Use a window function to retrieve the rolling average from seven days earlier.
  4. Return results from 2025-01-08 onward, ordered by metric date, with the percentage change versus the prior week.

Output details

  • Return columns in this order: metric_date, rolling_7_day_average, prior_week_average, percent_change_vs_prior_week.
  • Round rolling_7_day_average and percent_change_vs_prior_week to two decimal places.
  • Order results by metric_date ascending.
  • Treat the prior-week value as the seventh preceding available result, and leave it NULL when unavailable.
  • Return NULL for percent_change_vs_prior_week when prior_week_average is NULL or zero.

Schema

product_catalog
ColumnTypeDescription
product_idPKINTEGERUnique product identifier
product_nameVARCHAR(100)Harnham product name
daily_metrics
ColumnTypeDescription
metric_idPKINTEGERUnique metric record identifier
product_idINTEGERReferences product_catalog.product_id
metric_nameVARCHAR(50)Name of the measured metric
metric_dateDATECalendar date of the measurement
metric_valueNUMERIC(12,2)Metric value, which may be unavailable
Tablesdaily_metricsproduct_catalog
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.