SQL Quest › SQL Interview Questions › Joins
Two Swipes at the Same Merchant Inside a Day
To compare a row to another row of the SAME table, join the table to itself. Here: the same card, the same merchant, twice within 24 hours — the review queue a disputes team works through before anything is called a duplicate.
The join condition carries the whole question. b.account_id = a.account_id AND b.merchant_id = a.merchant_id pairs them up; b.txn_id > a.txn_id is the part people forget, and without it every transaction matches itself and every genuine pair comes back twice, once in each direction. txn_id increases with time in this ledger, so that same predicate also makes a the earlier swipe. The window is (julianday(b.txn_at) − julianday(a.txn_at)) * 24 <= 24.
Return account_id, merchant (name), first_txn_id, second_txn_id, first_txn_at, second_txn_at, first_amount, second_amount and hours_apart (rounded to 2 decimals). 18 pairs. Order by hours_apart ascending, then first_txn_id ascending, then second_txn_id ascending.
Be honest about what this finds: no pair in this ledger repeats the same amount, so these are repeat purchases, not proven duplicate charges. A rule like this produces a queue for a human, not a verdict.
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 |
Expected output: 188 AppPay 1305 1308 … 143.66 72.9 0.03; 17 Quick Stop 1384 1390 … 68.43 121.71 0.09; ...
Hint
Concepts
SELECT Self-JOIN JOIN JULIANDAY Date Functions Self-Join
Practise the topic: SQL practice questions · JOIN practice · Date function practice · Advanced SQL interview questions
Read the concept: LEFT JOIN vs INNER JOIN
In these company practice sets
A SQL Quest challenge matched to patterns reported for these companies — not a question any of them has published.
Preparing for Capital One? How the CodeSignal data analyst assessment works.
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