Inserting Record In to a Table Form an other Table In SQL
SQL Insert Command
Inserting Record In to a Table Form an other Table In SQL
You can also insert the data in to a table from an other table. In other word copy the data from one table to an other table. For this purpose SQL provide a command INSERT INTO SELECT statement. You can copy all column information and also selected column information. You can also use the WHERE Clause to copy the specific records.
Syntax is :
INSERT INTO new-table-name
SELECT * FROM source-table-name ;
If you want to insert some specific column information in to new table then use the following syntax:
Syntax is :
INSERT INTO new-table-name (Column1 , Cooumn2........)
SELECT (Column1 , Cooumn2........)
FROM source-table-name ;
If you want to insert some specific Records in to new table then use the following syntax:
Syntax is :
INSERT INTO new-table-name
SELECT * FROM source-table-name
WHERE condition ;
Now we take Some example to Understand All these syntax.
We have two table
TABLE_A
| Name | Marks | Result | Grade |
| Mr. Muneer | 500 | Pass | A |
| Mr. Naweed | 300 | Pass | B |
| Mr.tauqeer | 450 | Pass | A |
| Mr. Jalil | 200 | Fail | Fail |
And other table name is TABLE_B
Now we insert all these record in to TABLE_B
INSERT INTO TABLE_B
SELECT * FROM TABLE_B ;
IF you want to insert the Some specific column information for example from above table Name and Marks then use the following statement.
INSERT INTO TABLE_B (Name , Marks)
SELECT (Name, Marks)
FROM TABLE_A ;
IF you want to insert the Some specific Record form TABLE_A to TABLE_B for example copy all the Pass student record then use the following statement.
INSERT INTO TABLE_B
SELECT * FROM TABLE_A
WHERE Result='Pass' ;