Back to list
sql

PostgreSQL INSERT INTO ... SELECT

This statement lets you insert rows into a table by pulling data from another table (or the same table) using a SELECT query, rather than specifying values manually.

-- Assume both tables have the same structure
INSERT INTO orders_archive (order_id, customer_id, total, order_date)
SELECT order_id, customer_id, total, order_date
FROM orders
WHERE status = 'completed' AND order_date < '2025-01-01';
postgresql
lubosmato