SQL Insert Into Select
On this page
SQL INSERT INTO SELECT
The INSERT INTO SELECT statement copies data from one table and inserts it into an existing table.
Basic Syntax
INSERT INTO target_table (column1, column2, ...) SELECT column1, column2, ... FROM source_table WHERE condition;
Example: Copy Active Customers
Insert active customers into an archive table:
INSERT INTO archived_customers (id, name, email) SELECT id, name, email FROM customers WHERE status = 'active';
Copy All Columns
If both tables have the same structure and column order:
INSERT INTO archived_customers SELECT * FROM customers WHERE status = 'inactive';
Using JOIN with INSERT INTO SELECT
You can insert results from a JOIN query:
INSERT INTO customer_orders_summary (customer_name, order_date) SELECT c.name, o.order_date FROM customers AS c JOIN orders AS o ON c.id = o.customer_id;
Important Rules
- The number of columns in INSERT must match the SELECT
- Data types must be compatible
- Target table must already exist
Duplicate Data Risk
If you run the same INSERT INTO SELECT multiple times, you may insert duplicate rows unless prevented by constraints.
Performance Note
On large datasets, this operation may lock tables or consume significant resources. Proper indexing and batching may be necessary.
Common Mistakes
- Mismatched column counts
- Wrong column order
- Forgetting WHERE when only partial data is intended
Next Step
Continue with SQL CASE to learn conditional logic inside queries.