Select from One table and Insert into Another Table in SQL


SQL INSERT INTO SELECT  

Insert into select statement gets data from one table and inserts in another table. To do so data type in From(source table) and To(target table) must match, It does not affect the record in source table.

Without Condition

All Columns
To copy all records from one table to another table following syntax is used.

INSERT INTO target_table 
SELECT * FROM source_table

Specified Columns

To insert data from specific columns, Columns names have to be specified in both (target and source) tables.

INSERT INTO target_table(Name,Mobile,......)
SELECT Name,Mobile.......
FROM source_table


With Condition


All Columns
To copy specific record set  records from one table to another table following syntax is used.
INSERT INTO target_table 
SELECT * FROM source_table


where ID between 1 and 100

The above statement will insert all records from source table to target table where ID is between and 100.

Specified Columns

To insert data from specific columns, Columns names have to be specified in both (target and source) tables.

INSERT INTO target_table(Name,Mobile,......)
SELECT Name,Mobile.......
FROM source_table
where ID between 1 and 100

The above statement will insert records from 1 to 100 from source to target for columns specified where ID is between and 100.

Note: Records from auto generated column(s) are not copied from source table.

SQL Between Operator

For further question please mail: brainstormiert@gmail.com