← Back to SQL, page 10

sql-0191 · Easy fresher

Write a statement that copies shipped orders from 2026 into an archive table.

Use INSERT INTO archive (cols) SELECT cols FROM orders WHERE ...; it inserts every row the SELECT returns in one statement. List the target columns explicitly so a later column change does not shift values. Combine it with a DELETE in one transaction to move rows instead of copying them.

Full answer
sql
CREATE TABLE customers (
  id      INTEGER PRIMARY KEY,
  name    TEXT NOT NULL,
  country TEXT
);
CREATE TABLE orders (
  id          INTEGER PRIMARY KEY,
  customer_id INTEGER REFERENCES customers(id),
  order_date  TEXT NOT NULL,
  status      TEXT NOT NULL
);
CREATE TABLE order_items (
  order_id   INTEGER REFERENCES orders(id),
  product    TEXT NOT NULL,
  qty        INTEGER NOT NULL,
  unit_price INTEGER NOT NULL
);
INSERT INTO customers VALUES (1,'Ana','Spain'),(2,'Bilal','UAE'),(3,'Chloe','France'),(4,'Dev','India');
INSERT INTO orders VALUES
  (101,1,'2026-01-05','shipped'),(102,1,'2026-02-11','shipped'),
  (103,2,'2026-02-14','cancelled'),(104,3,'2026-03-01','shipped'),
  (105,2,'2026-03-09','pending'),(106,NULL,'2026-03-12','shipped');
INSERT INTO order_items VALUES
  (101,'Notebook',2,4),(101,'Pen',5,1),(102,'Bag',1,30),(103,'Pen',10,1),
  (104,'Notebook',3,4),(104,'Bag',1,30),(105,'Lamp',1,25),(106,'Pen',2,1);
sql
CREATE TABLE orders_archive (id INTEGER PRIMARY KEY, customer_id INTEGER, order_date TEXT, status TEXT);
INSERT INTO orders_archive (id, customer_id, order_date, status)
SELECT id, customer_id, order_date, status FROM orders
WHERE status = 'shipped' AND order_date >= '2026-01-01';
SELECT id, status FROM orders_archive ORDER BY id;
idstatus
101shipped
102shipped
104shipped
106shipped

Four shipped orders were copied. No VALUES list is needed; the SELECT supplies the rows.

To move rather than copy, delete with the same condition inside a transaction, so a failure cannot leave rows in both places or in neither:

sql
CREATE TABLE arch2 (id INTEGER PRIMARY KEY, status TEXT);
BEGIN;
INSERT INTO arch2 (id, status) SELECT id, status FROM orders WHERE status = 'shipped';
DELETE FROM orders WHERE status = 'shipped';
COMMIT;
SELECT (SELECT COUNT(*) FROM orders) AS left_in_orders, (SELECT COUNT(*) FROM arch2) AS archived;
left_in_ordersarchived
24

CREATE TABLE archive AS SELECT ... creates and fills a table in one step, but it copies no constraints or indexes, so it suits one-off snapshots more than permanent tables.

sql
CREATE TABLE customers (
  id      INTEGER PRIMARY KEY,
  name    TEXT NOT NULL,
  country TEXT
);
CREATE TABLE orders (
  id          INTEGER PRIMARY KEY,
  customer_id INTEGER REFERENCES customers(id),
  order_date  TEXT NOT NULL,
  status      TEXT NOT NULL
);
CREATE TABLE order_items (
  order_id   INTEGER REFERENCES orders(id),
  product    TEXT NOT NULL,
  qty        INTEGER NOT NULL,
  unit_price INTEGER NOT NULL
);
INSERT INTO customers VALUES (1,'Ana','Spain'),(2,'Bilal','UAE'),(3,'Chloe','France'),(4,'Dev','India');
INSERT INTO orders VALUES
  (101,1,'2026-01-05','shipped'),(102,1,'2026-02-11','shipped'),
  (103,2,'2026-02-14','cancelled'),(104,3,'2026-03-01','shipped'),
  (105,2,'2026-03-09','pending'),(106,NULL,'2026-03-12','shipped');
INSERT INTO order_items VALUES
  (101,'Notebook',2,4),(101,'Pen',5,1),(102,'Bag',1,30),(103,'Pen',10,1),
  (104,'Notebook',3,4),(104,'Bag',1,30),(105,'Lamp',1,25),(106,'Pen',2,1);

CREATE TABLE orders_archive (id INTEGER PRIMARY KEY, customer_id INTEGER, order_date TEXT, status TEXT);
INSERT INTO orders_archive (id, customer_id, order_date, status)
SELECT id, customer_id, order_date, status FROM orders
WHERE status = 'shipped' AND order_date >= '2026-01-01';
SELECT id, status FROM orders_archive ORDER BY id;

CREATE TABLE arch2 (id INTEGER PRIMARY KEY, status TEXT);
BEGIN;
INSERT INTO arch2 (id, status) SELECT id, status FROM orders WHERE status = 'shipped';
DELETE FROM orders WHERE status = 'shipped';
COMMIT;
SELECT (SELECT COUNT(*) FROM orders) AS left_in_orders, (SELECT COUNT(*) FROM arch2) AS archived;

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • INSERT ... SELECT inserts the query's rows
  • List target columns explicitly
  • Wrap copy plus delete in one transaction to move rows
  • CREATE TABLE AS copies data but not constraints

Common mistakes

  • Relying on column order without a column list
  • Moving rows with two statements outside a transaction

Follow-ups

  • How would you archive in batches of 1000?
  • How would PostgreSQL do the move in one statement?

Tips

  • Mention the transaction for moves