SQL QuestSQL Interview Questions › Window Functions

Geographic Mismatch — Impossible Travel

HardProQuerying BasicsWindow FunctionsDate Functions

Same card, two cities, five minutes apart. Use LAG() over txn_at partitioned by account to grab each transaction's previous coordinates and time. Flag pairs where the geographic distance is more than 500 km AND the time gap is less than 30 minutes — physically impossible legitimate travel.

For distance, use a degree-distance approximation: SQRT((lat - prev_lat)^2 + (lng - prev_lng)^2) * 111 ≈ km (rough but fine for gross-mismatch detection). True Haversine accounts for longitude convergence at high latitudes; the approximation is close enough for fraud screening.

Show account_id, txn_id, prev_at, txn_at, km_from_prev (rounded to 0), minutes_from_prev (rounded to 1). Order by km_from_prev descending. Top 15.

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: Impossible-travel transaction pairs

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 LAG Date Functions

Practise the topic: SQL practice questions · Window function practice · Date function practice · Advanced SQL interview questions

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