Practice Free
HardCTEs High Frequency

Delinquency Aging Buckets (30/60/90 Days Past Due)

Frequently Asked By:StripeCoinbaseBloomberg
Interactive In-Browser Workspace

Practice this problem on live schema tables in PostgreSQL 16 WASM with instant row verification and visual EXPLAIN plans.

Solve in Workbench

Problem Statement

Write a query using a CTE `classified_loans` to categorize each loan in `consumer_loans` into an `aging_bucket`: - If `days_overdue >= 90`, bucket is `'90+ DPD'` - If `days_overdue >= 60`, bucket is `'60-89 DPD'` - If `days_overdue >= 30`, bucket is `'30-59 DPD'` - Otherwise, bucket is `'1-29 DPD'` In the outer query, group by `aging_bucket` where `days_overdue >= 1`. Return: 1. `aging_bucket` 2. `loan_count`: Total loans in bucket 3. `total_unpaid_principal`: Sum of `unpaid_principal` Order results by `total_unpaid_principal` descending.

Why Technical Interviewers Ask This

Credit risk modeling teams group delinquent consumer installment loans into standardized delinquency aging buckets to calculate required loan loss capital provisions.

Common Pitfalls & Interview Traps

Forgetting required sorting and deterministic tie-breaking.
Not accounting for NULL or zero-value denominators during calculations.
Using inefficient quadratic subqueries instead of single-pass ctes constructs.

Canonical Modern SQL Solution

PostgreSQL 16 / ANSI SQL
WITH classified_loans AS (
        SELECT 
          loan_id,
          unpaid_principal,
          days_overdue,
          CASE 
            WHEN days_overdue >= 90 THEN '90+ DPD'
            WHEN days_overdue >= 60 THEN '60-89 DPD'
            WHEN days_overdue >= 30 THEN '30-59 DPD'
            ELSE '1-29 DPD'
          END AS aging_bucket
        FROM consumer_loans
        WHERE days_overdue >= 1
      )
      SELECT 
        aging_bucket,
        COUNT(*) AS loan_count,
        SUM(unpaid_principal) AS total_unpaid_principal
      FROM classified_loans
      GROUP BY aging_bucket
      ORDER BY total_unpaid_principal DESC;
Approach Breakdown
This query addresses the problem requirements by filtering and structuring data in CTEs. It meets the target criteria: Aggregate outstanding unpaid principal grouped by loan delinquency aging bucket.

Edge Cases to Verbally Clarify with Your Interviewer

The dataset contains zero matching records (must return valid empty result without crashing).
Multiple records tie on sorting criteria (must evaluate tie-breaking order deterministically).
Foreign keys or secondary attributes have NULL values.

Frequently Asked Follow-Up Questions

What makes this question a favorite among Stripe and Coinbase interviewers?

Interviewers use this problem to test whether candidates write defensive, scalable CTEs queries that run efficiently on multi-million row production databases without excessive memory usage.

How does the query optimizer execute this query?

Using single-pass table scans with index lookups or sort buffers. Running EXPLAIN (ANALYZE, BUFFERS) in the DB Exercises workbench visualizes the execution nodes and buffer hit cache.

More FAANG SQL Interview Questions

View all questions