Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Rolling Default Rate Query

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

Your question is Rolling Default Rate Query. 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

American Credit Acceptance wants to monitor recent credit performance by loan origination cohort. Write a PostgreSQL query that calculates the rolling 30-day default rate for each cohort and reporting date.

A loan enters a default event on the first date where its status is DEFAULT. The rolling window includes the reporting date and the preceding 29 calendar days.

Requirements

  1. Derive each loan cohort from the month of origination_date.
  2. Identify each loan's first default date, ignoring later repeated default statuses.
  3. Produce one row per cohort and reporting date where at least one loan in that cohort has originated by that date.
  4. Calculate cohort loan count, the number of first defaults during the rolling 30-day window, and the default rate as rolling defaults divided by cohort loan count.
  5. Use 0 for cohorts with no defaults in the window, round the rate to four decimal places, and order by cohort month and reporting date.

Schema

loans
ColumnTypeDescription
loan_idPKBIGINTUnique loan identifier
origination_dateDATEDate the loan was originated
product_codeVARCHAR(20)American Credit Acceptance loan product code
loan_status_daily
ColumnTypeDescription
status_idPKBIGINTUnique status observation
loan_idBIGINTLoan associated with the status
status_dateDATEDate of the status observation
status_codeVARCHAR(20)Observed loan status
Tablesloansloan_status_daily
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results