Premium problem26. Third Completed Purchase

Medium Locked

transactions(txn_id, user_id, amount, status, txn_at)

For each user, return their third completed transaction in chronological order. Where two share a timestamp, the lower txn_id comes first. Users with fewer than three completed transactions are left out.

Return user_id, txn_id, txn_at and amount, ordered by user_id.

Failed transactions do not count toward the third, so the numbering has to happen after filtering by status.

Tables

transactions

txn_id  user_id  amount  status     txn_at
------  -------  ------  ---------  -------------------
1       10       10.00   completed  2026-01-01 10:00:00
2       10       20.00   completed  2026-01-02 10:00:00
3       10       30.00   failed     2026-01-03 10:00:00
4       10       40.00   completed  2026-01-04 10:00:00
5       11       50.00   completed  2026-02-01 10:00:00
7       11       70.00   completed  2026-02-02 10:00:00
6       11       60.00   completed  2026-02-02 10:00:00
8       12       80.00   completed  2026-03-01 10:00:00
... 1 more row(s)

Expected result

user_id  txn_id  txn_at               amount
-------  ------  -------------------  ------
10       4       2026-01-04 10:00:00  40.00
11       7       2026-02-02 10:00:00  70.00

Premium problem

This one's part of Premium. Unlock the full MySQL track plus every other premium problem on the site.

Write one SELECT query