Chapter 9 · Interview Level
50. Loan Risk Classification with CASE
Hard40 XPFinance
The risk team needs loans classified into risk tiers. Use CASE WHEN to assign a risk_tier: 'High Risk' if interest_rate > 15 or the loan has no payments, 'Medium Risk' if interest_rate between 8 and 15, 'Low Risk' otherwise. Return loan id, loan_type, interest_rate, principal, payment_count, and risk_tier. Sort by risk_tier, interest_rate DESC. Limit to 40.
Tables
- PKidINT
- FKcustomer_id→ customers.idINT
- loan_typeTEXT
- principalREAL
- interest_rateREAL
- term_monthsINT
- start_dateTEXT
- statusTEXT
- PKidINT
- FKloan_id→ loans.idINT
- payment_dateTEXT
- amountREAL
- principal_paidREAL
- interest_paidREAL
- remaining_balanceREAL
Need help?
Ask Sol
AI tutor — looks at your current query and nudges you, without spoiling the answer.
Solution
Try the hints first — solving it yourself is what makes it stick.