Introduction
This article will discuss 3 different methods in SQL to insert records either from input data or bulk copied from another table.
The content of the article:
- Introduction
- Definition
- SQL INSERT INTO Statement
- SQL INSERT INTO SELECT Statement
- SQL SELECT INTO Statement
- Demos
- Test Data
- Demo 1: Copy data from one table into a new table.
- Demo 2: Copy data from one table into an existing table.
- Demo 3: Insert new records in a (existing) table.
Definition
There are 3 Inserting Methods in SQL:
- SQL INSERT INTO Statement
- SQL INSERT INTO SELECT Statement
- SQL SELECT INTO Statement
SQL INSERT INTO Statement
- The
INSERT INTOstatement is used to insert new records in a (existing) table. - It is possible in two ways:
1. Specify both the column names and the values to be inserted:
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
2. If you are adding values for all the columns of the table, you do not need to specify the column names in the SQL query. However, make sure the order of the values is in the same order as the columns in the table. Here, the INSERT INTO syntax would be as follows:
INSERT INTO table_name
VALUES (value1, value2, value3, ...);
SQL INSERT INTO SELECT Statement:
- The
INSERT INTO SELECTstatement copies data from one table and inserts it into another (existing) table. - The
INSERT INTO SELECTthe Statement requires that the data types in the source and target tables match. - The existing records in the target table are unaffected.
- It is possible in two ways:
1. Copy all columns from one table to another table:
INSERT INTO table2
SELECT * FROM table1
WHERE condition;
2. Copy only some columns from one table into another table:
INSERT INTO table2 (column1, column2, column3, ...)
SELECT column1, column2, column3, ...
FROM table1
WHERE condition;
SQL SELECT INTO Statement:
- The
SELECT INTOstatement copies data from one table into a new table. - It is possible in two ways:
1. Copy all columns into a new table:
SELECT *
INTO newtable [IN externaldb]
FROM oldtable
WHERE condition;
2. Copy only some columns into a new table:
SELECT column1, column2, column3, ...
INTO newtable [IN externaldb]
FROM oldtable
WHERE condition;
The new table will be created with the column-names and types as defined in the old table. You can create new column names using the AS clause.












Join the conversation! Your thoughts help the community grow.