SQL interview question: find duplicate events to delete

EasyDuplicates

Problem

Shared data rules

Find 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

Input: events
event_iduser_idevent_tsevent_nameRow note
1172026-09-22 13:00add_to_cart
1272026-09-22 13:00add_to_cart← copy of 11
1372026-09-22 13:00add_to_cart← copy of 11
14NULL2026-09-23 18:00session_start← no user_id — leave alone
15NULL2026-09-23 18:00session_start← no user_id — leave alone
1672026-09-22 13:00session_start← different name — not a duplicate
Output
event_idkept_event_id
1211
1311

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

All questions