SQL INSERT INTO SELECT Keyword
The INSERT INTO SELECT statement copies data from one table and inserts it into another. This is useful when transferring data between tables with similar structures.
SQL INSERT INTO SELECT Syntax
SQL
x
1
INSERT INTO target_table (column1, column2, ...)
2
SELECT column1, column2, ...
3
FROM source_table
4
WHERE condition;
SQL INSERT INTO SELECT Copy Names from Students to Doctors Example
This query copies names from the Students table into the Doctors table, filling only the first and last name columns. Other fields will be NULL.
SQL
1
1
INSERT INTO Doctors (first_name, last_name)
2
SELECT name, name
3
FROM Students
4
WHERE id < 5;

SQL INSERT INTO SELECT Copy Doctors into Patients Table Example
This query inserts rows from the Doctors table into the Patients table, providing fixed values for columns that require non-null data (like dob and gender):
SQL
1
1
INSERT INTO Patients (first_name, last_name, dob, gender)
2
SELECT first_name, last_name, '1990-01-01', 'F'
3
FROM Doctors
4
WHERE specialty IS NOT NULL;
