Find the bugs: Deduplicating an event log
An event log sometimes contains duplicate records. Two records count as copies of the same event only if all three fields match: user_id, event_type, and created_at. From each group of duplicates, keep the record with the smallest id. Events without a user must remain in the result too. A developer wrote a query to remove duplicates from the log. Find the lines where the developer made mistakes.
Find the errorsDuplicates and top NJuniorAnalystData EngineerBackendQA
Input data · iv_events
| id | user_id | event_type | created_at |
|---|---|---|---|
| 1 | 1 | login | 2026-01-31 23:59:00 |
| 2 | 1 | login | 2026-01-31 23:59:00 |
| 3 | 1 | view | 2026-02-01 00:00:00 |
| 4 | 2 | login | 2026-02-01 08:00:00 |
| 5 | 2 | view | 2026-02-01 08:05:00 |
| 6 | 2 | view | 2026-02-01 08:05:00 |
| 7 | 3 | login | 2026-02-02 10:00:00 |
| 8 | NULL | view | 2026-02-03 11:00:00 |
| 9 | 1 | purchase | 2026-02-03 12:00:00 |
| 10 | 2 | logout | 2026-02-28 23:59:59 |
SELECT id, user_id, event_type, created_at
FROM iv_events
WHERE id IN (
SELECT MIN(id)
FROM iv_events
GROUP BY user_id
)
ORDER BY id