Build a risk report using multiple CTEs. CTE 1: customer_balances computes total_balance per customer from accounts. CTE 2: customer_loans computes total loan principal per customer from loans. Main query: join both CTEs to customers and show first_name, last_name, total_balance, total_principal, and a debt_to_asset_ratio (total_principal / total_balance, rounded to 2 decimals). Only include customers with total_balance > 0. Sort by ratio 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