cswork / libraryOpen practice workspace

Coding questions · Wells Fargo

Write SQL for rolling default rates

This question evaluates proficiency with SQL window functions and time-series aggregation, specifically the ability to compute rolling metrics across customer segments by aggregating defaults and accounts over a moving 12-month window.

Write a SQL query to compute 12‑month rolling default rates by customer segment. Assume table loans(month DATE, segment TEXT, defaults INT, accounts INT). For each segment and month, compute SUM(defaults) over the current and prior 11 months divided by SUM(accounts) over the same window.

Compute 12-Month Rolling Default Rates by Segment

You are given a loans table with monthly data by customer segment:

  • month: the calendar month of the data
  • segment: customer segment label
  • defaults: number of defaulted accounts in that month
  • accounts: number of active accounts in that month

Write a SQL query to compute, for each segment and month, the 12-month rolling default rate. For a given segment and month, the rolling default rate is defined as:

SUM(defaults) over the current month and prior 11 months
------------------------------------------------------
SUM(accounts) over the current month and prior 11 months

For months where there are fewer than 11 prior months of data, use all available prior months for that segment. Return one row per segment and month with the following columns:

  • month
  • segment
  • rolling_defaults (12-month window sum of defaults)
  • rolling_accounts (12-month window sum of accounts)
  • rolling_default_rate (rolling_defaults / rolling_accounts, rounded to 4 decimal places)

Table loans

columntypeconstraints
monthDATENOT NULL
segmentVARCHAR(50)NOT NULL
defaultsINTNOT NULL
accountsINTNOT NULL

Sample data:

accountsdefaultsmonthsegment
10022023-01-01Retail
10032023-02-01Retail
10012023-03-01Retail
10042023-04-01Retail
10022023-05-01Retail
10032023-06-01Retail
10022023-07-01Retail
10052023-08-01Retail
10012023-09-01Retail
10022023-10-01Retail
10032023-11-01Retail
10042023-12-01Retail
10022024-01-01Retail
10032024-02-01Retail
5012023-01-01Corporate
5022023-02-01Corporate
5012023-03-01Corporate
5012023-04-01Corporate
5022023-05-01Corporate
5022023-06-01Corporate
5032023-07-01Corporate
5012023-08-01Corporate
5022023-09-01Corporate
5012023-10-01Corporate
5022023-11-01Corporate
5012023-12-01Corporate
5022024-01-01Corporate
5012024-02-01Corporate

Expected output

One row per segment and month, showing the 12-month (or shorter at the beginning) rolling sums of defaults and accounts, and the corresponding rolling default rate rounded to 4 decimal places.

monthrolling_accountsrolling_default_raterolling_defaultssegment
2023-01-01500.021Corporate
2023-02-011000.033Corporate
2023-03-011500.02674Corporate
2023-04-012000.0255Corporate
2023-05-012500.0287Corporate
2023-06-013000.039Corporate
2023-07-013500.034312Corporate
2023-08-014000.032513Corporate
2023-09-014500.033315Corporate
2023-10-015000.03216Corporate
2023-11-015500.032718Corporate
2023-12-016000.031719Corporate
2024-01-016000.033320Corporate
2024-02-016000.031719Corporate
2023-01-011000.022Retail
2023-02-012000.0255Retail
2023-03-013000.026Retail
2023-04-014000.02510Retail
2023-05-015000.02412Retail
2023-06-016000.02515Retail
2023-07-017000.024317Retail
2023-08-018000.027522Retail
2023-09-019000.025623Retail
2023-10-0110000.02525Retail
2023-11-0111000.025528Retail
2023-12-0112000.026732Retail
2024-01-0112000.026732Retail
2024-02-0112000.026732Retail

Hints

  1. Use a window function with PARTITION BY segment and ORDER BY month.
  2. Define a window frame of ROWS BETWEEN 11 PRECEDING AND CURRENT ROW to capture a 12-month rolling window.