Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

SQL Rolling Average with Windows

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

Your question is SQL Rolling Average with Windows. 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

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.

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
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results