001Level 1

Chapter 7 · CTEs

15. Customer Lifetime Value Estimate

Hard40 XPFinance

Estimate customer lifetime value using multiple CTEs. CTE 1: total_deposits — sum of deposit transactions per customer (through accounts). CTE 2: total_interest_earned — sum of interest transactions per customer. Main query: join both CTEs to customers and return first_name, last_name, total_deposits, total_interest_earned, and estimated_value (deposits + interest). Sort by estimated_value DESC. Limit to 15.

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
  • FKaccount_id→ accounts.idINT
  • transaction_dateTEXT
  • typeTEXT
  • amountREAL
  • balance_afterREAL
  • descriptionTEXT
  • reference_numberTEXT

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.