SQL Quest › SQL Interview Questions › Subqueries & CTEs
Recursive CTE — Chargeback Chain Investigation
Chargeback chains map fraud rings. When a customer disputes a transaction, the chargeback team traces related disputes — same merchant, same account, prior disputes that triggered this one. Use a WITH RECURSIVE CTE to walk the dispute chain starting from chargeback_id = 1, following related_chargeback_id edges (downstream chargebacks that reference this one as their root).
The anchor is the seed (chargeback 1). The recursive step joins to the CTE itself, finding chargebacks whose related_chargeback_id matches a chargeback already in the chain. Limit depth < 5 to prevent runaway recursion if the data has cycles.
Show chargeback_id, txn_id, account_id, depth. Order by depth ascending, then chargeback_id ascending.
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
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: Connected chargeback cluster
Hint
Concepts
SELECT Recursive CTE JOIN UNION ALL
Practise the topic: SQL practice questions · CTE practice · JOIN practice · Advanced SQL interview questions
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