Premium problem58. Keep the Latest Customer Record

Easy Locked

customer_snapshots(snapshot_id, customer_id, updated_at, email, plan)

Return one row per customer: their most recent snapshot. When two snapshots share the same updated_at, the larger snapshot_id wins.

Return customer_id, email, plan and updated_at, ordered by customer_id.

Grouping by customer and taking MAX(updated_at) gives the right timestamp but lets email and plan come from any row, which can stitch together a record that never existed.

Tables

customer_snapshots

snapshot_id  customer_id  updated_at           email      plan
-----------  -----------  -------------------  ---------  ----
9            1            2026-01-01 10:00:00  old@a.com  free
3            1            2026-05-01 10:00:00  new@a.com  pro
4            2            2026-03-01 10:00:00  b@b.com    free
5            2            2026-03-01 10:00:00  b@b.com    pro
6            3            2026-02-01 10:00:00  c@c.com    team

Expected result

customer_id  email      plan  updated_at
-----------  ---------  ----  -------------------
1            new@a.com  pro   2026-05-01 10:00:00
2            b@b.com    pro   2026-03-01 10:00:00
3            c@c.com    team  2026-02-01 10:00:00

Premium problem

This one's part of Premium. Unlock the full MySQL track plus every other premium problem on the site.

Write one SELECT query