Narayana Tutorial

Online Java Tutorial Blog

SQL INSERT

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:

  1. While inserting data into a table from another table, the table column data types should be same.
  2. 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%';

 

Narayanaswamy

Hello! I am Narayanaswamy founder and admin of narayanatutorial.com. I have been working in IT industry more than 7 years. NarayanaTutorial is my web technologies blog. My specialties are Java / J2EE, Spring, Hibernate, Struts, Webservices, PHP, Oracle, MySQL, SQLServer, Web Hosting and Website Development.

I am a self learner and passionate about training and writing. I am always trying my best to share my knowledge through my blog.

Leave a Reply

Narayana Tutorial © 2018 Frontier Theme
Show Button
Hide Button