Search RU
Tasks / SQL / Archiving old orders
Statement medium

Archiving old orders

Move orders created before `2026-01-01` from `orders` to `orders_archive`:
first insert them into the archive, then delete them from the main table.

The boundary is strict: an order with `created_at = DATE '2026-01-01'` stays in
`orders`. All columns are copied unchanged.

In the end `orders` must contain only new orders, and `orders_archive` must
contain the old ones plus whatever was already archived. No duplicates.

SQL · PostgreSQL
sqlinsert-selectdeletearchivedml
Schema
CREATE TABLE orders (
    id integer PRIMARY KEY,
    customer_id integer NOT NULL,
    total integer NOT NULL,
    status text NOT NULL,
    created_at date NOT NULL
);

CREATE TABLE orders_archive (
    id integer PRIMARY KEY,
    customer_id integer NOT NULL,
    total integer NOT NULL,
    status text NOT NULL,
    created_at date NOT NULL
);
Data
INSERT INTO orders (id, customer_id, total, status, created_at) VALUES
    (1, 1, 1500, 'paid', '2025-11-10'),
    (2, 1, 2500, 'new', '2025-12-15'),
    (3, 2, 800, 'cancelled', '2025-12-31'),
    (4, 2, 3200, 'paid', '2026-01-01'),
    (5, 3, 1200, 'new', '2026-01-05'),
    (6, 3, 900, 'paid', '2026-01-10');
query.sql PostgreSQL 17
AI mentor

The AI mentor is available after you log in: it sees your attempt history and does not spend resources on anonymous requests.

Log in and ask
Hints 1 of 3 unlocked · attempts: 0
Hint 1

You need two operations with the same condition: copy the rows into the
archive, then delete them from the source table.

Hint 2 — unlocks after 1 attempt
Hint 3 — unlocks after 3 attempts