SQL SELECT INTO Keyword
The SELECT INTO command is used to copy data from one table into a new table. This is useful for creating backup copies or for transferring data to another table.
SQL SELECT INTO Syntax
SQL
x
1
SELECT column1, column2, ...
2
INTO new_table
3
FROM existing_table
4
WHERE condition;
SQL SELECT INTO All Columns Example
SQL
1
1
SELECT *
2
INTO PatientsBackup
3
FROM Patients;
Note: This code does not work in TechKubo playground (SQLite). In SQLite, use
CREATE TABLE AS SELECT
instead.
SQL CREATE TABLE AS SELECT Equivalent Example
This query creates a backup of all records from Patients
into a new table PatientsBackup
.
SQL
1
1
CREATE TABLE PatientsBackup AS
2
SELECT *
3
FROM Patients;

SQL SELECT INTO with Condition Example
This query creates a backup table of patients who are female.
SQL
1
1
CREATE TABLE FemalePatientsBackup AS
2
SELECT *
3
FROM Patients
4
WHERE gender = 'Female';

SQL SELECT INTO from Multiple Tables Syntax
SQL
1
1
SELECT table1.column1, table2.column2, ...
2
INTO new_table
3
FROM table1
4
JOIN table2 ON table1.column = table2.column
5
WHERE condition;
SQL SELECT INTO from Multiple Tables Example
SQL
1
1
CREATE TABLE AppointmentsDoctorsBackup AS
2
SELECT Appointments.appointment_id, Doctors.specialty
3
FROM Appointments
4
LEFT JOIN Doctors ON Appointments.doctor_id = Doctors.doctor_id;
