Chapter 7 · CTEs
14. Loan Repayment Progress Report
Hard40 XPFinance
Build a loan repayment progress report. CTE 1: compute total_paid (sum of amount from payments) per loan. CTE 2: join to loans for loan details. Main query: return loan_id, loan_type, principal, total_paid, remaining (principal - total_paid), and progress_pct (total_paid / principal * 100 rounded to 1 decimal). Include loans with no payments. Sort by progress_pct descending.
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.