SQL interview question: find duplicate events to delete
Problem
Shared data rulesFind the events from Sep 21–27, 2026 to delete as duplicates. A duplicate is an event with the same user_id, event_ts and event_name as an event with a smaller event_id.
Example
events| event_id | user_id | event_ts | event_name | Row note |
|---|---|---|---|---|
| 11 | 7 | 2026-09-22 13:00 | add_to_cart | |
| 12 | 7 | 2026-09-22 13:00 | add_to_cart | ← copy of 11 |
| 13 | 7 | 2026-09-22 13:00 | add_to_cart | ← copy of 11 |
| 14 | NULL | 2026-09-23 18:00 | session_start | ← no user_id — leave alone |
| 15 | NULL | 2026-09-23 18:00 | session_start | ← no user_id — leave alone |
| 16 | 7 | 2026-09-22 13:00 | session_start | ← different name — not a duplicate |
- Event 12: copy of 11
- Event 13: copy of 11
- Event 14: no user_id — leave alone
- Event 15: no user_id — leave alone
- Event 16: different name — not a duplicate
| event_id | kept_event_id |
|---|---|
| 12 | 11 |
| 13 | 11 |
Of events 11–13, event 11 stays; 12 and 13 are deleted.
Output columns
kept_event_id- the event that stays: the group’s smallest event_id
Solve it free
Your query runs on PostgreSQL in your browser. No sign-up needed.
Skills it tests
More interview questions
- SQL: Products with no salesMedium · Anti-join: what’s missing
- SQL: Longest consecutive order-day streakHard · Consecutive streaks
- SQL: Weekly cohort retention tableHard · Retention
- SQL: Sessionization from event gapsHard · Time intervals
- SQL: Time between consecutive purchasesMedium · Compare with previous