HardCTEs High Frequency
High Net Worth Liquidity Segmentation
Frequently Asked By:StripeCoinbaseBloomberg
Interactive In-Browser Workspace
Practice this problem on live schema tables in PostgreSQL 16 WASM with instant row verification and visual EXPLAIN plans.
Problem Statement
1. Write a CTE `client_balances` that computes the total balance for each user across all accounts in `financial_accounts`.
2. In the outer query, categorize clients into wealth tiers:
- `>= 250000.00` -> `'Private Client'`
- `>= 100000.00` -> `'Premier'`
- ELSE -> `'Standard'`
3. Group by `wealth_tier`, counting how many clients fall in each tier and the total assets in that tier.
Return:
1. `wealth_tier`
2. `client_count`
3. `total_tier_assets`
Order results by `total_tier_assets` descending.
Why Technical Interviewers Ask This
Private wealth management routes clients to dedicated advisors based on total aggregated liquidity across checking, savings, and investment accounts.
Common Pitfalls & Interview Traps
Forgetting required sorting and deterministic tie-breaking.
Not accounting for NULL or zero-value denominators during calculations.
Using inefficient quadratic subqueries instead of single-pass ctes constructs.
Canonical Modern SQL Solution
PostgreSQL 16 / ANSI SQLWITH client_totals AS (
SELECT
user_id,
SUM(balance_usd) AS total_balance
FROM financial_accounts
GROUP BY user_id
),
tiered_clients AS (
SELECT
user_id,
total_balance,
CASE
WHEN total_balance >= 250000.00 THEN 'Private Client'
WHEN total_balance >= 100000.00 THEN 'Premier'
ELSE 'Standard'
END AS wealth_tier
FROM client_totals
)
SELECT
wealth_tier,
COUNT(*) AS client_count,
SUM(total_balance) AS total_tier_assets
FROM tiered_clients
GROUP BY wealth_tier
ORDER BY total_tier_assets DESC;Approach Breakdown
This query addresses the problem requirements by filtering and structuring data in CTEs. It meets the target criteria: Summary of client count and total assets grouped by wealth tier.Edge Cases to Verbally Clarify with Your Interviewer
The dataset contains zero matching records (must return valid empty result without crashing).
Multiple records tie on sorting criteria (must evaluate tie-breaking order deterministically).
Foreign keys or secondary attributes have NULL values.
Frequently Asked Follow-Up Questions
What makes this question a favorite among Stripe and Coinbase interviewers?
Interviewers use this problem to test whether candidates write defensive, scalable CTEs queries that run efficiently on multi-million row production databases without excessive memory usage.
How does the query optimizer execute this query?
Using single-pass table scans with index lookups or sort buffers. Running EXPLAIN (ANALYZE, BUFFERS) in the DB Exercises workbench visualizes the execution nodes and buffer hit cache.