Chapter 8 · Window Functions
41. Rank Customers by Net Worth
Hard40 XPFinance
Rank all customers by their total net worth (sum of all account balances minus sum of all active loan principals). Return first_name, last_name, total_balance, total_loan_principal, net_worth, and a dense_rank (net_worth_rank). Use CTEs and window functions. Include customers even if they have no loans (treat as 0). Order by net_worth_rank ascending.
Tables
- PKidINT
- first_nameTEXT
- last_nameTEXT
- emailTEXT
- phoneTEXT
- date_of_birthTEXT
- cityTEXT
- stateTEXT
- countryTEXT
- created_atTIMESTAMP
- PKidINT
- FKcustomer_id→ customers.idINT
- account_typeTEXT
- balanceREAL
- currencyTEXT
- opened_atTEXT
- statusTEXT
- 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.