SQL INSERT DATA INTO A TABLE FROM ANOTHER TABLE
We can insert data into table in two different ways. They are
1) Using INSERT INTO without using SELECT statement.
Syntax
INSERT INTO <TableName>(<ColumnName1>,<ColumnName2>) VALUES (<ColumnValue1>,<ColumnValue2>);
Example
INSERT INTO EMPLOYEE (FIRSTNAME, LASTNAME) VALUES ('Narayana','Ragi');
2) Using INSERT INTO with SELECT statement Syntax
INSERT INTO <TableName> SELECT <ColumnName1>, <ColumnName2>......<ColumnNameN> FROM <TableName>
Example
INSERT INTO EMPLOYEE_DETAILS SELECT FIRSTNAME, LASTNAME FROM EMPLOYEE
Note:
- While inserting data into a table from another table, the table column data types should be same.
- target table columns length should be greater than or equal to source table data columns length
Insertion of a data set into a table from another table
Syntax
INSERT INTO <TableName> SELECT <ColumnName 1>, <ColumnName 2> FROM <TableName> WHERE <Condition>;
Example
INSERT INTO EMPLOYEE_HISTORY SELECT FIRSTNAME,LASTNAME FROM EMPLOYEE WHERE ID LIKE '123%';
Hello! I am Narayanaswamy founder and admin of narayanatutorial.com. I have been working in the IT industry for more than 12 years. NarayanaTutorial is my web technologies blog. My specialties are Java / J2EE, Spring, Hibernate, Struts, Webservices, PHP, Oracle, MySQL, SQLServer, Web Hosting, Website Development, and IAM(ForgeRock) Specialist
I am a self-learner and passionate about training and writing. I am always trying my best to share my knowledge through my blog.