Find the bugs: Count a user's earlier events

For each event with a known user_id, return its id, user_id, event_type, and the number of earlier events from the same user. If two events have the same timestamp, the event with the smaller id counts as earlier. A user's first event should have a count of 0. Review the query and select the lines that produce incorrect counts.

Find the errorsWindow functionsMiddleAnalystData EngineerBackend
Input data · iv_events
iduser_idevent_typecreated_at
11login2026-01-31 23:59:00
21login2026-01-31 23:59:00
31view2026-02-01 00:00:00
42login2026-02-01 08:00:00
52view2026-02-01 08:05:00
62view2026-02-01 08:05:00
73login2026-02-02 10:00:00
8NULLview2026-02-03 11:00:00
91purchase2026-02-03 12:00:00
102logout2026-02-28 23:59:59
SELECT id,
       user_id,
       event_type,
       COUNT(*) OVER (
         PARTITION BY user_id
         ORDER BY created_at, id
         ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
       ) AS earlier_events
FROM iv_events
WHERE user_id IS NOT NULL
ORDER BY user_id, id