SQL QuestSQL Interview Questions › Window Functions

First-to-Second Transaction Latency

HardProQuerying BasicsWindow FunctionsSubqueries & CTEs

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

Expected output: account 10: 0.2 h, account 179: 0.3 h, account 152: 1.1 h ...

Hint

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

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

Capital One · Revolut

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

Related questions

Cumulative Distinct Customers Over TimeHard · FreeSalary Rank Within DepartmentHard · FreeRunning Total RevenueHard · FreeYear-over-Year GrowthHard · Free7-Day Rolling Revenue AverageHard · FreeMulti-CTE Revenue PipelineHard · Free

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