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
Get started
SQL Rolling Averages and Anomalies
00:00
5 left

SQL Rolling Averages and Anomalies

MediumSQL · PostgreSQL

Problem

S&P Global teams monitor product-level financial metrics across platforms such as Capital IQ and Market Intelligence. Write a PostgreSQL query that calculates a trailing three-observation average and flags unusually large fluctuations.

Requirements

  1. Join financial_metrics to spg_products and include only active products with non-null metric values.
  2. Calculate the average of the three preceding observations for each product, ordered by metric_date.
  3. Flag a row as anomalous when its metric differs from the trailing average by more than 20 percent. Rows without three preceding observations are not anomalous.
  4. Return the product name, date, metric value, rounded trailing average, and anomaly flag in chronological order per product.

Output details

  • Return columns in this order: product_name, metric_date, metric_value, rolling_average, and is_anomalous.
  • Sort by product_name ascending, then metric_date ascending.
  • Round rolling_average to exactly two decimal places.
  • Represent unavailable trailing averages as NULL and set is_anomalous to FALSE when fewer than three preceding observations exist.
  • Set is_anomalous to FALSE when the trailing average is zero.

Schema

spg_products
ColumnTypeDescription
product_idPKINTUnique product identifier
product_nameVARCHAR(100)S&P Global product name
is_activeBOOLEANWhether the product is monitored
financial_metrics
ColumnTypeDescription
metric_idPKINTUnique metric observation identifier
product_idINTReferenced product identifier
metric_dateDATEDate of the observation
metric_valueDECIMAL(14,2)Observed financial metric value
Tablesspg_productsfinancial_metrics
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.

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.