CODE WORKSPACE

Duplicate snapshot keys

Return order_id and snapshot_count for every order_id that appears more than once in order_snapshots.

Group by the key that should be unique, then keep the groups whose count is above one.

Sample input

order_snapshotsA history table: order_id repeats across updates. 1001 and 1004 each have two snapshots.
order_idstatusupdated_at
1001paid2026-01-12 09:00:00
1001shipped2026-01-13 10:00:00
1002paid2026-01-12 10:30:00
1004paid2026-01-15 14:30:00
1004shipped2026-01-16 12:20:00
1008shipped2026-01-26 10:30:00

Expected output

Expected outputOnly the order_ids that appear more than once, most-duplicated first.
order_idsnapshot_count
10012
10042

Constraints

GROUP BY order_id and keep only groups with COUNT(*) > 1. Order by snapshot_count DESC, then order_id ASC.

Expected skills

Duplicate-key detection with GROUP BY and HAVING.

SQL
Loading...

AI evaluation

Run the SQL query to inspect preview rows.