Find the bugs: three required event types
Find users who have all three event types: login, view, and purchase. Return user_id in ascending order. The query uses grouping and a subquery to check these requirements. Which lines prevent it from selecting the correct users?
Find the errorsSubqueries, CTEs, EXISTS / IN, UNIONMiddleAnalystData EngineerQA
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 user_id
FROM iv_events
WHERE user_id IS NOT NULL
AND event_type IN ('login', 'view')
AND EXISTS (SELECT 1 FROM iv_events WHERE event_type = 'purchase')
GROUP BY user_id
HAVING COUNT(DISTINCT event_type) = 2
ORDER BY user_id