Replay each account's wallet transactions in order of `txn_ts` (ties broken by `txn_id`). CREDIT adds `amount`, DEBIT subtracts it; transactions with status 'FAILED' are ignored. Find the FIRST successful transaction after which the running balance is below 0 (exactly 0 is not negative). Return `account_id`, `txn_id`, `txn_ts`, `balance_after` (ROUND 2) for accounts that ever went negative. Order by `account_id`.
| Column | Type |
|---|---|
| txn_id | INTEGER |
| account_id | TEXT |
| txn_ts | TIMESTAMP |
| txn_type | TEXT |
| amount | REAL |
| status | TEXT |
| txn_id | account_id | txn_ts | txn_type | amount | status |
|---|---|---|---|---|---|
| 1 | A1 | 2026-10-01 09:00:00 | CREDIT | 100 | SUCCESS |
| 2 | A1 | 2026-10-01 10:00:00 | DEBIT | 30 | SUCCESS |
| 3 | A1 | 2026-10-02 08:00:00 | DEBIT | 80 | SUCCESS |
| 4 | A1 | 2026-10-03 08:00:00 | CREDIT | 50 | SUCCESS |
| 5 | A2 | 2026-10-01 09:00:00 | CREDIT | 50 | SUCCESS |
| 6 | A2 | 2026-10-01 09:30:00 | DEBIT | 60 | FAILED |
| 7 | A2 | 2026-10-01 10:00:00 | DEBIT | 40 | SUCCESS |
| 8 | A3 | 2026-10-01 11:00:00 | DEBIT | 20 | SUCCESS |
| account_id | txn_id | txn_ts | balance_after |
|---|---|---|---|
| A1 | 3 | 2026-10-02 08:00:00 | -10 |
| A3 | 8 | 2026-10-01 11:00:00 | -20 |
Solve it in the live editor on this page — run on sample data, then submit against hidden tests.
‹ Rolling 7-Day Orders with Missing Days · Card Used in 3 Countries Within an Hour ›
{{ totalCount }} questions in the style asked at Amazon, Microsoft, Google, Meta, Flipkart and more — {{ hardPct }}% Hard. Write SQL in a real database engine in your browser, run it on sample data, then evaluate against hidden tests.
Company tags show where this type of question is commonly reported in interviews. Questions, data and solutions are written by TechTown.
{{ cur.statement }}
No submissions yet. Press Evaluate to submit your query.
{{ solutionSql }}
{{ out.error }}
You have solved your 3 free questions — nice work! Log in (free) to unlock all 150 questions, keep your streak and appear on the leaderboard.