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.
| id | amount | status |
|---|---|---|
| 1 | 100.00 | paid |
| 2 | 200.00 | paid |
| 3 | 150.00 | paid |
Sign in to see submission history
Sign inSign in to use AI Mentor
Sign in