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
SELECT column1, column2, ... INTO new_table FROM existing_table WHERE condition;
SQL SELECT INTO All Columns Example
SELECT * INTO PatientsBackup 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
.
CREATE TABLE PatientsBackup AS SELECT * FROM Patients;

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

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