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%';

 

Leave a Reply