SQL QuestSQL Interview Questions › Joins

Two Swipes at the Same Merchant Inside a Day

HardProQuerying BasicsJoinsDate Functions

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_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

Expected output: 188 AppPay 1305 1308 … 143.66 72.9 0.03; 17 Quick Stop 1384 1390 … 68.43 121.71 0.09; ...

Hint

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

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

In these company practice sets

Capital One · Plaid

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

Related questions

Multi-CTE Revenue PipelineHard · FreeEmployees Earning More Than ManagerHard · ProEmployees with Similar SalariesHard · ProMoving Average with Dynamic WindowHard · ProDetect Repeat Buyers Within 7 DaysHard · ProRecursive Team Size RollupHard · 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