TExcelTAD
Home › SQL › SQL INSERT INTO SELECT

SQL INSERT INTO SELECT

INSERT INTO … SELECT copies rows from a query into an existing table. Unlike CREATE TABLE … AS SELECT, the target table keeps its own definition — constraints, defaults and all.

sql
INSERT INTO target_table (column1, column2, ...)
SELECT column1, column2, ...
FROM source_table
WHERE condition;

The SELECT must return the same number of columns, in the same order, as the column list.

Archiving old orders

Create an archive table with a proper primary key, then copy the delivered orders from the first quarter into it:

sql · try it yourself
CREATE TABLE orders_archive (
  order_id    INTEGER PRIMARY KEY,
  customer_id INTEGER,
  order_date  TEXT NOT NULL,
  status      TEXT NOT NULL
);

INSERT INTO orders_archive (order_id, customer_id, order_date, status)
SELECT order_id, customer_id, order_date, status
FROM orders
WHERE status = 'delivered' AND order_date < '2025-04-01';

SELECT * FROM orders_archive ORDER BY order_id;
6 rows affected.

order_id | customer_id | order_date | status
---------+-------------+------------+----------
2        | 18          | 2025-01-02 | delivered
4        | 18          | 2025-01-08 | delivered
6        | 17          | 2025-02-10 | delivered
7        | 16          | 2025-02-20 | delivered
8        | 5           | 2025-02-17 | delivered
12       | 12          | 2025-03-01 | delivered
(6 rows)

Copying between tables with different columns

The column names don't have to match — only the positions do. Turn every supplier into a customer contact (perhaps for a newsletter):

sql · try it yourself
INSERT INTO customers (name, contact, city, country, joined)
SELECT name, contact, city, country, '2025-10-15'
FROM suppliers
WHERE country IN ('Japan', 'India');

SELECT customer_id, name, contact, city, joined
FROM customers
WHERE customer_id > 25
ORDER BY customer_id;
2 rows affected.

customer_id | name         | contact    | city      | joined
------------+--------------+------------+-----------+-----------
26          | Volt Gadgets | Kenji Mori | Osaka     | 2025-10-15
27          | Byte Press   | Anil Mehta | Bangalore | 2025-10-15
(2 rows)

Constants such as '2025-10-15' can be mixed with columns in the SELECT.

Filling a summary table

sql · try it yourself
CREATE TABLE monthly_orders (
  month  TEXT PRIMARY KEY,
  orders INTEGER NOT NULL
);

INSERT INTO monthly_orders (month, orders)
SELECT substr(order_date, 1, 7), COUNT(*)
FROM orders
WHERE order_date < '2025-05-01'
GROUP BY substr(order_date, 1, 7);

SELECT * FROM monthly_orders ORDER BY month;
4 rows affected.

month   | orders
--------+-------
2025-01 | 4
2025-02 | 4
2025-03 | 4
2025-04 | 4
(4 rows)

Avoiding duplicates

Run the archive copy twice and the primary key will complain. Insert only the rows that are not there yet with NOT EXISTS — or, in SQLite, INSERT OR IGNORE:

sql · try it yourself
CREATE TABLE vip (customer_id INTEGER PRIMARY KEY, name TEXT);
INSERT INTO vip SELECT customer_id, name FROM customers WHERE customer_id IN (2, 17);

-- second batch overlaps with the first (17)
INSERT INTO vip (customer_id, name)
SELECT c.customer_id, c.name
FROM customers AS c
WHERE c.customer_id IN (17, 18, 22)
  AND NOT EXISTS (SELECT 1 FROM vip AS v WHERE v.customer_id = c.customer_id);

SELECT * FROM vip ORDER BY customer_id;
2 rows affected.

2 rows affected.

customer_id | name
------------+-------------
2           | Bright Bytes
17          | Amara Costa
18          | Felix Reyes
22          | Kai Moreau
(4 rows)
Database Skip rows that would violate a unique key
SQLite INSERT OR IGNORE INTO … or … ON CONFLICT DO NOTHING
PostgreSQL … ON CONFLICT DO NOTHING
MySQL INSERT IGNORE INTO …
SQL Server WHERE NOT EXISTS (…) or MERGE

Test yourself

What must match between the INSERT column list and the SELECT?

  1. The column names
  2. The number and order of columns
  3. The table names
  4. Nothing

What is the main difference from CREATE TABLE … AS SELECT?

  1. INSERT INTO SELECT creates a new table
  2. INSERT INTO SELECT fills an existing table and keeps its constraints
  3. There is none
  4. INSERT INTO SELECT can only copy one row