Chapter 7 · CTEs
18. Recursive Loan Payment Schedule Projection
Using a recursive CTE, project the first 12 months of repayments for the loan with id = 1. Assume the principal is repaid in equal monthly installments (principal / term_months) and interest is charged each month on the balance still owed (interest_rate is an annual percentage, so monthly interest = balance * interest_rate / 100 / 12). For each month return month_number, principal_paid, interest, payment (principal_paid + interest), and remaining_balance after the payment, all rounded to 2 decimals. Sort by month_number.
Tables
- PKidINT
- FKcustomer_id→ customers.idINT
- loan_typeTEXT
- principalREAL
- interest_rateREAL
- term_monthsINT
- start_dateTEXT
- statusTEXT
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.