001Level 1

Chapter 7 · CTEs

13. Multi-CTE Risk Report

Hard40 XPFinance

Build a risk report using multiple CTEs. CTE 1: customer_balances computes total_balance per customer from active accounts. CTE 2: customer_loans computes total loan principal per customer from active 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.

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.

Peek at the solution?

You can still earn the XP afterwards by submitting a working query — but you'll learn more if you give it one more try.