SQL INSERT INTO SELECT Statement
Author: Al-mamun Sarkar
Date: 2021-04-26 23:03:12
We can insert data to a table by selecting data from another table using SQL INSERT INTO SELECT Statement. In this lesson, I will show you how we can do that.
Syntax:
INSERT INTO first_table
SELECT * FROM another_table
WHERE condition;
Insert all employees to customer table:
INSERT INTO customers
SELECT * FROM employees;
Insert data with a specific condition:
INSERT INTO customers
SELECT * FROM employees
WHERE country = "USA";
Insert only some column:
INSERT INTO first_table (column1, column2, column3)
SELECT column1, column2, column3
FROM another_table
WHERE condition;
Example:
INSERT INTO customers (name, email, gender)
SELECT name, email, gender
FROM employees
WHERE gender = 'male';