SQL QuestSQL Interview Questions › Subqueries & CTEs

Recursive CTE — Chargeback Chain Investigation

HardProQuerying BasicsSubqueries & CTEsJoins

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_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: Connected chargeback cluster

Hint

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

Concepts

SELECT Recursive CTE JOIN UNION ALL

Practise the topic: SQL practice questions · CTE practice · JOIN practice · Advanced SQL interview questions

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