SQL Insert Into Select

The SQL Insert Into Select statement allows you to copy data from one table into another. This command is useful when you want to transfer data between tables without manually inserting each row.

SQL Insert Into Select Syntax

INSERT INTO target_table (column1, column2, ...)
SELECT column1, column2, ...
FROM source_table
WHERE condition;



SQL Insert Into Select Basic Example

This query creates a new table called NewPatients and inserts all patient data from the Patients table into it.

CREATE TABLE NewPatients (
    patient_id INTEGER,
    first_name TEXT,
    last_name TEXT
);

INSERT INTO NewPatients (patient_id, first_name, last_name)
SELECT patient_id, first_name, last_name
FROM Patients;




SQL Insert Into Select with Condition Example

This query copies only the patients whose first name starts with ‘A’ from the Patients table to the NewPatients table.

INSERT INTO NewPatients (patient_id, first_name, last_name)
SELECT patient_id, first_name, last_name
FROM Patients
WHERE first_name LIKE 'A%';




SQL Insert Into Select Visual Diagram



SQL Insert Into Select Labs

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top