Academic Block

SQL INSERT INTO SELECT Get Certified

Learn how to copy rows from one table (or from a query) into an already existing table using INSERT INTO … SELECT.

What is INSERT INTO SELECT?

The statement

INSERT INTO destination_table (col1, col2, …)
SELECT …
FROM source_table;

takes the result of a SELECT query and inserts those rows into a table that must already exist. This is different from SELECT INTO / CREATE TABLE … AS SELECT, which create a brand-new table.

INSERT INTO … SELECT is fully supported in SQLite and is one of the most useful ways to move, archive or summarise data.

Basic Syntax

INSERT INTO destination_table (column1, column2)
SELECT column1, column2
FROM source_table;

Copy All Columns

When the source and destination tables have the same structure you can copy every column with a simple SELECT *.

What this code does: Copies every row from courses into the already-created course_archive table.

DROP TABLE IF EXISTS courses;
DROP TABLE IF EXISTS course_archive;
CREATE TABLE courses (
  course_id INTEGER,
  course_name TEXT,
  duration INTEGER
);
CREATE TABLE course_archive (
  course_id INTEGER,
  course_name TEXT,
  duration INTEGER
);
INSERT INTO courses VALUES
  (1, 'SQL Basics', 30),
  (2, 'Web Design', 45),
  (3, 'Data Analysis', 60);
INSERT INTO course_archive
SELECT *
FROM courses;
SELECT *
FROM course_archive
ORDER BY course_id;

Copy Selected Columns

You can list only the columns you want to transfer. The number and order of columns in the SELECT list must match the column list of the INSERT.

What this code does: Inserts only product_id and product_name into the narrower product_list table.

DROP TABLE IF EXISTS products;
DROP TABLE IF EXISTS product_list;
CREATE TABLE products (
  product_id INTEGER,
  product_name TEXT,
  category TEXT,
  price INTEGER
);
CREATE TABLE product_list (
  product_id INTEGER,
  product_name TEXT
);
INSERT INTO products VALUES
  (1, 'Mechanical Keyboard', 'Accessories', 2800),
  (2, 'Studio Headphones', 'Audio', 4200),
  (3, 'USB Hub', 'Accessories', 1500);
INSERT INTO product_list (product_id, product_name)
SELECT product_id, product_name
FROM products;
SELECT *
FROM product_list
ORDER BY product_id;

INSERT INTO SELECT with WHERE

A WHERE clause lets you transfer only the rows that satisfy a condition.

What this code does: Copies only the employees whose salary is 80 000 or higher into the senior_employees table.

DROP TABLE IF EXISTS employees;
DROP TABLE IF EXISTS senior_employees;
CREATE TABLE employees (
  employee_id INTEGER,
  employee_name TEXT,
  department TEXT,
  salary INTEGER
);
CREATE TABLE senior_employees (
  employee_id INTEGER,
  employee_name TEXT,
  department TEXT,
  salary INTEGER
);
INSERT INTO employees VALUES
  (1, 'Nisha', 'Design', 58000),
  (2, 'Arjun', 'Engineering', 85000),
  (3, 'Mira', 'Marketing', 67000),
  (4, 'Kabir', 'Engineering', 92000);
INSERT INTO senior_employees
SELECT *
FROM employees
WHERE salary >= 80000;
SELECT *
FROM senior_employees
ORDER BY salary DESC;

INSERT DISTINCT Values

DISTINCT can be used to insert only unique values.

What this code does: Inserts the three unique department names into the departments table.

DROP TABLE IF EXISTS employees;
DROP TABLE IF EXISTS departments;
CREATE TABLE employees (
  employee_id INTEGER,
  employee_name TEXT,
  department TEXT
);
CREATE TABLE departments (
  department TEXT
);
INSERT INTO employees VALUES
  (1, 'Aarav', 'Engineering'),
  (2, 'Meera', 'Design'),
  (3, 'Rohan', 'Engineering'),
  (4, 'Tara', 'Marketing'),
  (5, 'Dev', 'Design');
INSERT INTO departments (department)
SELECT DISTINCT department
FROM employees;
SELECT *
FROM departments
ORDER BY department;

INSERT with ORDER BY

An ORDER BY clause in the SELECT query controls the order in which rows are inserted (useful when the destination table has an auto-increment column or when the order matters for later processing).

What this code does: Inserts books priced 900 or more, ordered from highest to lowest price.

DROP TABLE IF EXISTS books;
DROP TABLE IF EXISTS selected_books;
CREATE TABLE books (
  book_id INTEGER,
  title TEXT,
  price INTEGER
);
CREATE TABLE selected_books (
  book_id INTEGER,
  title TEXT,
  price INTEGER
);
INSERT INTO books VALUES
  (1, 'SQL Essentials', 650),
  (2, 'Database Systems', 1400),
  (3, 'Web Programming', 900),
  (4, 'Data Science', 1800);
INSERT INTO selected_books
SELECT *
FROM books
WHERE price >= 900
ORDER BY price DESC;
SELECT *
FROM selected_books
ORDER BY price DESC;

INSERT Calculated Values

The SELECT list can contain expressions; the calculated results are inserted into the destination columns.

What this code does: Calculates quantity * price for each order and stores the result as total_amount.

DROP TABLE IF EXISTS orders;
DROP TABLE IF EXISTS order_summary;
CREATE TABLE orders (
  order_id INTEGER,
  product TEXT,
  quantity INTEGER,
  price INTEGER
);
CREATE TABLE order_summary (
  order_id INTEGER,
  product TEXT,
  total_amount INTEGER
);
INSERT INTO orders VALUES
  (101, 'Keyboard', 2, 1800),
  (102, 'Mouse', 3, 900),
  (103, 'Monitor', 1, 12500);
INSERT INTO order_summary (order_id, product, total_amount)
SELECT
  order_id,
  product,
  quantity * price
FROM orders;
SELECT *
FROM order_summary
ORDER BY order_id;

INSERT Aggregated Results

Aggregate queries can be materialised into another table – ideal for summary or reporting tables.

What this code does: Inserts one summary row per category containing the total sales amount and the number of orders.

DROP TABLE IF EXISTS sales;
DROP TABLE IF EXISTS category_summary;
CREATE TABLE sales (
  sale_id INTEGER,
  category TEXT,
  amount INTEGER
);
CREATE TABLE category_summary (
  category TEXT,
  total_sales INTEGER,
  order_count INTEGER
);
INSERT INTO sales VALUES
  (1, 'Books', 800),
  (2, 'Books', 1200),
  (3, 'Games', 1500),
  (4, 'Games', 700),
  (5, 'Accessories', 950);
INSERT INTO category_summary (category, total_sales, order_count)
SELECT
  category,
  SUM(amount),
  COUNT(*)
FROM sales
GROUP BY category;
SELECT *
FROM category_summary
ORDER BY total_sales DESC;

INSERT Data Combined from Multiple Tables (JOIN)

The SELECT can contain JOINs, so you can combine columns from several tables and insert the combined result in one step.

What this code does: Joins customers and purchases, then inserts the customer name together with each purchase into customer_purchases.

DROP TABLE IF EXISTS customers;
DROP TABLE IF EXISTS purchases;
DROP TABLE IF EXISTS customer_purchases;
CREATE TABLE customers (
  customer_id INTEGER,
  customer_name TEXT
);
CREATE TABLE purchases (
  purchase_id INTEGER,
  customer_id INTEGER,
  amount INTEGER
);
CREATE TABLE customer_purchases (
  customer_name TEXT,
  purchase_id INTEGER,
  amount INTEGER
);
INSERT INTO customers VALUES
  (1, 'Anaya'),
  (2, 'Rohan'),
  (3, 'Mira');
INSERT INTO purchases VALUES
  (501, 1, 2200),
  (502, 2, 1450),
  (503, 3, 3200);
INSERT INTO customer_purchases (customer_name, purchase_id, amount)
SELECT
  c.customer_name,
  p.purchase_id,
  p.amount
FROM customers AS c
INNER JOIN purchases AS p
  ON c.customer_id = p.customer_id;
SELECT *
FROM customer_purchases
ORDER BY purchase_id;

INSERT Filtered Data (Low-Stock Example)

A common real-world use is to move only the rows that meet a business rule into a monitoring or alert table.

What this code does: Copies every inventory item whose stock is less than 10 into the low_stock table.

DROP TABLE IF EXISTS inventory;
DROP TABLE IF EXISTS low_stock;
CREATE TABLE inventory (
  item_id INTEGER,
  item_name TEXT,
  stock INTEGER
);
CREATE TABLE low_stock (
  item_id INTEGER,
  item_name TEXT,
  stock INTEGER
);
INSERT INTO inventory VALUES
  (1, 'USB Cable', 35),
  (2, 'HDMI Cable', 8),
  (3, 'Laptop Stand', 18),
  (4, 'Mouse Pad', 5),
  (5, 'Desk Lamp', 25);
INSERT INTO low_stock
SELECT *
FROM inventory
WHERE stock < 10;
SELECT *
FROM low_stock
ORDER BY stock;

INSERT INTO SELECT vs SELECT INTO

Feature
INSERT INTO … SELECT
SELECT INTO / CREATE TABLE AS SELECT
Destination table
Must already exist
Creates a new table
Main purpose
Copy rows into an existing table
Create a table from a query result
SQLite support
Yes
CREATE TABLE AS SELECT only

Advantages

  • Works with tables that already exist (perfect for archives, staging tables, summary tables).
  • Can copy selected columns, filtered rows, calculated values and aggregated results.
  • Can combine data from multiple tables with JOINs in a single statement.
  • Fully supported in SQLite and virtually every other relational database.

Best Practices

  • Always list the destination columns when the mapping is not obvious.
  • Ensure the number and data types of the SELECT list match the destination columns.
  • Use WHERE (or HAVING) to avoid inserting unwanted rows.
  • Check for potential duplicates before a bulk insert if the destination has unique constraints.
  • For large transfers consider wrapping the statement in a transaction.

INSERT INTO … SELECT is the standard way to move query results into an existing table. It can copy whole tables, selected columns, filtered records, calculated values and even the results of JOINs and aggregates. Click Test My Code on any example to run it live, or Code Explanation to walk through the query block by block.

Ctrl + Enter to run code in the editor Esc to close the editor

🧪 Test Your SQL Code

Edit the SQL code on the left and click "Run Code" to see the result on the right.

📝 SQL Code
👁️ Preview (query result)

Click "Run Code" to see the result here.