SQL QuestSQL Interview Questions › Subqueries & CTEs

Disputed Spend by Risk Tier and Category

HardProQuerying BasicsSubqueries & CTEsJoinsConditional LogicWindow Functions

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_idaccount_idamounttxn_atmerchant_idlatlngstatus
1149151.842026-03-04T12:39:39.078Z2440.342-74.4092completed
22110.722026-03-04T13:04:50.641Z752.936613.3229completed
335158.782026-03-04T13:27:15.528Z2552.739713.3134completed

merchants

merchant_idnamecategorycountryrisk_tier
1BigBox MartGroceryTRhigh
2Quick StopElectronicsJPhigh
3Aurora CafeTravelTRhigh

chargebacks

chargeback_idtxn_idaccount_idmerchant_idreason_codeopened_atresolved_atstatusrelated_chargeback_id
1816010service_not_received2026-03-06T16:52:17.429Z2026-04-06T16:52:17.429ZsplitNULL
21644216fraud_card_not_present2026-03-16T23:50:46.792Z2026-04-22T23:50:46.792Zopen1
31724820fraud_card_present2026-03-12T05:22:57.195Z2026-04-14T05:22:57.195Zcardholder_won2

Expected output: high Travel 90 17227.26 6003.77 34.85 8.6; high Electronics 84 33517.9 510 1.52 8.6; ...

Hint

This is a Pro challenge — the hint, the step-by-step tutor and the reference solution open in the app.

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

Capital One · Stripe

A SQL Quest challenge matched to patterns reported for these companies — not a question any of them has published.

Related questions

Cumulative Distinct Customers Over TimeHard · FreeRunning Total RevenueHard · FreeYear-over-Year GrowthHard · Free7-Day Rolling Revenue AverageHard · FreeMulti-CTE Revenue PipelineHard · FreeWealthy Survivor ProfileHard · Pro

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