SQL Quest › SQL Interview Questions › Subqueries & CTEs
Disputed Spend by Risk Tier and Category
Disputed VALUE, not disputed COUNT — and the value is not where you would look for it. The chargebacks table has no amount column. The money at stake in a dispute is the amount of the transaction it points at, so the figure comes from transactions and the chargeback row only says which transactions count. LEFT JOIN on txn_id (one chargeback per transaction here, 76 over 76 distinct ids, so this join does not fan out), then a conditional SUM picks the disputed amounts out.
Segment by the two merchant attributes an issuer actually acts on — risk_tier and category — and put each cell next to its tier's overall rate so a reader can tell a bad category from a bad tier.
Return risk_tier, category, txn_count, total_spend, disputed_spend, disputed_pct (100.0 × disputed ÷ total, rounded to 2 decimals) and tier_disputed_pct (the same rate for the whole tier, rounded to 2 decimals). 15 rows. Order by risk_tier ascending, then disputed_pct descending, then category ascending.
The benchmark column is a window over the AGGREGATED rows — SUM(disputed_spend) OVER (PARTITION BY risk_tier) / SUM(total_spend) OVER (PARTITION BY risk_tier). Averaging the cell percentages instead would weight a 78-transaction category the same as a 240-transaction one.
Solve it in the browser editor →
Runs on SQLite in your browser, graded against the expected result, no signup. A wrong answer gets a diagnosis, not just "incorrect".
Schema
transactions
| txn_id | account_id | amount | txn_at | merchant_id | lat | lng | status |
|---|---|---|---|---|---|---|---|
| 1 | 149 | 151.84 | 2026-03-04T12:39:39.078Z | 24 | 40.342 | -74.4092 | completed |
| 2 | 21 | 10.72 | 2026-03-04T13:04:50.641Z | 7 | 52.9366 | 13.3229 | completed |
| 3 | 35 | 158.78 | 2026-03-04T13:27:15.528Z | 25 | 52.7397 | 13.3134 | completed |
merchants
| merchant_id | name | category | country | risk_tier |
|---|---|---|---|---|
| 1 | BigBox Mart | Grocery | TR | high |
| 2 | Quick Stop | Electronics | JP | high |
| 3 | Aurora Cafe | Travel | TR | high |
chargebacks
| chargeback_id | txn_id | account_id | merchant_id | reason_code | opened_at | resolved_at | status | related_chargeback_id |
|---|---|---|---|---|---|---|---|---|
| 1 | 8 | 160 | 10 | service_not_received | 2026-03-06T16:52:17.429Z | 2026-04-06T16:52:17.429Z | split | NULL |
| 2 | 164 | 42 | 16 | fraud_card_not_present | 2026-03-16T23:50:46.792Z | 2026-04-22T23:50:46.792Z | open | 1 |
| 3 | 172 | 48 | 20 | fraud_card_present | 2026-03-12T05:22:57.195Z | 2026-04-14T05:22:57.195Z | cardholder_won | 2 |
Expected output: high Travel 90 17227.26 6003.77 34.85 8.6; high Electronics 84 33517.9 510 1.52 8.6; ...
Hint
Concepts
SELECT CTE JOIN LEFT JOIN CASE Window Functions PARTITION BY Multi-CTE
Practise the topic: SQL practice questions · CTE practice · JOIN practice · CASE WHEN practice · Window function practice · Advanced SQL interview questions
In these company practice sets
A SQL Quest challenge matched to patterns reported for these companies — not a question any of them has published.
Related questions
Where would this cost you points in an interview?
Ten questions, no signup: a Skillmap across nine SQL skills and the one to fix first.
Take the readiness test