Chapter 8 · Window Functions
32. Next Payment Date for Each Loan Payment
Hard40 XPFinance
The collections team wants to see each loan payment alongside the next scheduled payment date. For each payment, use LEAD to get the next payment_date on the same loan. Return loan_id, payment_date, amount, next_payment_date, and days_until_next (difference in days). Filter to loan_id = 145 for readability. Order by payment_date.
Tables
- 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.