#789

Move old rows to archive in one go — race-free

Archiving old orders in two statements (insert into the archive, then delete from the main table) is race-prone: someone can insert a new matching row between the steps, or a status can flip. A single CTE-based query handles this atomically. Move all paid orders (status = 'paid') created before January 1, 2024 from orders into orders_archive in a single statement, no intermediate state. The archive should receive the columns id, status, and amount.

Expected output sample

This is what a correct answer looks like — its row count is its own, it doesn't have to match the schema tables.

idamountstatus
1100.00paid
2200.00paid
3150.00paid

Your query result will appear here