Find the bugs: categorizing events by user
Select events with id values from 4 through 9. For each event, return id, user_id, and a category. Label events with no specified user as no user. If an event has a user and its type is view, assign the label view. Label all other events other. The result should contain all six events, sorted by id. Which lines of the query prevent it from producing this result?
Find the errorsNULLMiddleAnalystData 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 e.id,
e.user_id,
CASE
WHEN e.event_type = 'view' THEN 'view'
WHEN e.user_id IS NULL THEN 'no user'
ELSE 'other'
END AS category
FROM iv_events AS e
WHERE e.id BETWEEN 4 AND 9
ORDER BY e.id