Skip to main content
All Problems
hard0% acceptance

Customer Account Summary with Multiple CTEs

Build a comprehensive customer summary. CTE 1: account_summary — count accounts and sum balances per customer. CTE 2: loan_summary — count loans and sum principal per customer. Main query: join both CTEs to customers. Return first_name, last_name, account_count, total_balance, loan_count, total_principal. Include customers even if they have no loans. Sort by total_balance descending. Limit to 20.

Schema

🔑idINT
first_nameTEXT
last_nameTEXT
emailTEXT
phoneTEXTNULL
date_of_birthTEXT
cityTEXT
stateTEXT
countryTEXT
created_atTIMESTAMP
postgresql

Run your query to see results

Press Ctrl + Enter to execute