SQL Quest › SQL Interview Questions › Window Functions
First-to-Second Transaction Latency
Activation metric: how long after the first swipe does the second one come? A card that is used once and then sits for a week behaves differently from one used twice in an hour. Per account, find the first and second transactions in time order and the hours between them. Two window functions on one pass: LEAD(txn_at) to fetch the next transaction's timestamp, ROW_NUMBER() to keep only the first row per account. Order both windows by txn_at, txn_id so simultaneous timestamps cannot flip the answer.
Return account_id, first_txn_at, second_txn_at, and hours_to_second ((julianday(second) − julianday(first)) × 24, rounded to 1 decimal). Accounts with only one transaction are excluded. Order by hours_to_second ascending, then account_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
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 |
Expected output: account 10: 0.2 h, account 179: 0.3 h, account 152: 1.1 h ...
Hint
Concepts
SELECT Window Functions ROW_NUMBER LEAD CTE JULIANDAY Window Functions + CTE
Practise the topic: SQL practice questions · Window function practice · CTE practice · Ranking function practice · Advanced SQL interview questions
In these company practice sets
A SQL Quest challenge matched to patterns reported for these companies — not a question any of them has published.
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