Skip to content
RED SAGSkills
medium10 pts · first answer countssqlwindow-functionsdeduplication

Keeping the latest row per key

events has (user_id, status, updated_at): (1, new, 01-01), (1, paid, 01-03), (2, new, 01-02), (1, shipped, 01-02), (2, cancelled, 01-04), (3, new, 01-05). What does the query return?

WITH r AS (
  SELECT *, ROW_NUMBER() OVER (
    PARTITION BY user_id ORDER BY updated_at DESC) AS rn
  FROM events
)
SELECT user_id, status FROM r WHERE rn = 1 ORDER BY user_id;
Choose one answer