Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
SQL Window Functions for Rolling Metrics
00:00
5 left

SQL Window Functions for Rolling Metrics

MediumSQL · PostgreSQL

Problem

Snapmint's risk team wants to monitor daily loan defaults and smooth short-term fluctuations. Write a PostgreSQL query that calculates the 30-day rolling average of daily default events for the reporting period from February 1 through February 10, 2025.

Requirements

  1. Join loan_defaults to loans so only defaults belonging to known loans are counted.
  2. Include every calendar date in the reporting period, including dates with zero defaults.
  3. Calculate the daily default count and the rolling average across the current date and previous 29 calendar days.
  4. Round the rolling average to two decimal places and order the result chronologically.

Schema

loans
ColumnTypeDescription
loan_idPKINTUnique Snapmint loan identifier
customer_idVARCHAR(20)Snapmint customer identifier
product_nameVARCHAR(50)Financing product associated with the loan
loan_defaults
ColumnTypeDescription
default_event_idPKINTUnique default event identifier
loan_idINTLoan associated with the default event
default_dateDATEDate the loan entered default
default_amountNUMERIC(12,2)Outstanding amount at default
Tablesloansloan_defaults
Interviewer

Your question is SQL Window Functions for Rolling Metrics. 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
You need to log in / sign up to run or submit.Ln 1
Run your query to see results here.