Your cart is currently empty!
Author: alien
-
Khóa học miễn phí SQL – Insert Into Select nhận dự án làm có lương
SQL – Insert Into… Select Statement
Table of content
The Insert Into… Select Statement
The SQL INSERT INTO… SELECT statement is used to add/insert one or more new rows from an existing table to another table. This statement is a combination of two different statements: INSERT INTO and SELECT.
-
The INSERT INTO statement is one of the most fundamental and frequently used statements in database management and requires only the name of the table and the values to be inserted. However, it is important to ensure that the data being inserted satisfies the constraints if the columns of a table (if any) and its type matches the data types of the table columns.
-
The SELECT statement is used to retrieve data from an existing database table.
When these statements are used together, the SELECT statement first retrieves the data from an existing table and the INSERT INTO statement inserts the retrieved data into another table (if they have same table structures).
Syntax
Following is the syntax of the SQL INSERT INTO… SELECT statement −
INSERT INTO table_new SELECT (column1, column2, ...columnN) FROM table_old;
Before using this query, we have to make sure that −
-
In the database where we are going to insert data, source and target tables already exist.
-
The structure of the source and target tables are same.
Example
Assume we have created a table named CUSTOMERS which contains the personal details of customers including their name, age, address and salary etc.., as shown below −
CREATE TABLE CUSTOMERS ( ID INT NOT NULL, NAME VARCHAR (20) NOT NULL, AGE INT NOT NULL, ADDRESS CHAR (25), SALARY DECIMAL (18, 2), PRIMARY KEY (ID) );
Now, insert values into this table using the INSERT statement as follows −
INSERT INTO CUSTOMERS VALUES (1, ''Ramesh'', 32, ''Ahmedabad'', 2000.00), (2, ''Khilan'', 25, ''Delhi'', 1500.00), (3, ''Kaushik'', 23, ''Kota'', 2000.00), (4, ''Chaitali'', 25, ''Mumbai'', 6500.00), (5, ''Hardik'', 27, ''Bhopal'', 8500.00), (6, ''Komal'', 22, ''Hyderabad'', 4500.00), (7, ''Muffy'', 24, ''Indore'', 10000.00);
The table will be created as −
ID NAME AGE ADDRESS SALARY 1 Ramesh 32 Ahmedabad 2000.00 2 Khilan 25 Delhi 1500.00 3 Kaushik 23 Kota 2000.00 4 Chaitali 25 Mumbai 6500.00 5 Hardik 27 Bhopal 8500.00 6 Komal 22 Hyderabad 4500.00 7 Muffy 24 Indore 10000.00 Create another table named BUYERS with same structure as the CUSTOMERS table.
CREATE TABLE BUYERS ( ID INT NOT NULL, NAME VARCHAR (20) NOT NULL, AGE INT NOT NULL, ADDRESS CHAR (25), SALARY DECIMAL (18, 2), PRIMARY KEY (ID) );
Following query copies all the records from the CUSTOMERS table to BUYERS −
INSERT INTO BUYERS SELECT * FROM CUSTOMERS;
Verification
If you verify the contents of the BUYERS table using the SELECT statement as −
SELECT * FROM BUYERS;
The table will be created as −
ID NAME AGE ADDRESS SALARY 1 Ramesh 32 Ahmedabad 2000.00 2 Khilan 25 Delhi 1500.00 3 Kaushik 23 Kota 2000.00 4 Chaitali 25 Mumbai 6500.00 5 Hardik 27 Bhopal 8500.00 6 Komal 22 Hyderabad 4500.00 7 Muffy 24 Indore 10000.00 SQL – Inserting Specific Records
Sometimes we only need to add a small number of records to another table. This can be accomplished by using a WHERE clause along with the SQL INSERT INTO… SELECT statement.
Example
Let us create a table named NAMESTARTSWITH_K with the same structure as the CUSTOMER table using the CREATE statement as −
CREATE TABLE NAMESTARTSWITH_K ( ID INT NOT NULL, NAME VARCHAR (20) NOT NULL, AGE INT NOT NULL, ADDRESS CHAR (25), SALARY DECIMAL (18, 2), PRIMARY KEY (ID) );
Following query inserts the records of the customers whose name starts with the letter k from the CUSTOMERS table to the BUYERS table −
INSERT INTO NAMESTARTSWITH_K SELECT * FROM CUSTOMERS WHERE NAME LIKE ''k%
Verification
Following is the SELECT statement to verify the contents of the above created table −
SELECT * FROM NAMESTARTSWITH_K;
The table will be created as −
ID NAME AGE ADDRESS SALARY 2 Khilan 25 Delhi 1500.00 3 Kaushik 23 Kota 2000.00 6 Komal 22 Hyderabad 4500.00 SQL – Inserting Top N Rows
The LIMIT clause filters the number of rows from the query. You can use this to filter the top N records that should be added to the target table.
Example
But, before proceeding further, let us truncate all rows in the BUYERS table using the following statement −
TRUNCATE TABLE BUYERS;
Following query inserts the top 3 records from the CUSTOMERS table to the BUYERS table −
INSERT INTO BUYERS SELECT * FROM CUSTOMERS ORDER BY ID ASC LIMIT 3;
Verification
Let us verify the contents of the BUYERS table −
SELECT * FROM BUYERS;
The resultant table will be as follows −
ID NAME AGE ADDRESS SALARY 1 Ramesh 32 Ahmedabad 2000.00 2 Khilan 25 Delhi 1500.00 3 Kaushik 23 Kota 2000.00
Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc
-
Khóa học miễn phí SQL – Select Into nhận dự án làm có lương
SQL – Select Into Statement
The SQL Select Into Statement
The SQL SELECT INTO Statement creates a new table and inserts data from an existing table into the newly created table. The new table is automatically created based on the structure of the columns in the SELECT statement and can be created in the same database or in a different database.
However, it”s important to note that the SELECT INTO statement does not preserve any indexes, constraints, or other properties of the original table, and the new table will not have any primary keys or foreign keys defined by default. Therefore, you may need to add these properties to the new table manually if necessary.
MySQL doesn”t support the SELECT … INTO TABLE Sybase SQL extension i.e. in MySQL you cannot use the SELECT … INTO statement to insert data from one table to another. Instead of this, we can use INSERT INTO … SELECT statement or, CREATE TABLE … SELECT.
Syntax
Following is the basic syntax of the SQL SELECT INTO statement in SQL Server −
SELECT * INTO new_table_name FROM existing_table_name
Example
Let us create the CUSTOMERS table which contains the personal details of customers including their name, age, address and salary etc. as shown below −
CREATE TABLE CUSTOMERS ( ID INT NOT NULL, NAME VARCHAR (20) NOT NULL, AGE INT NOT NULL, ADDRESS CHAR (25), SALARY DECIMAL (18, 2), PRIMARY KEY (ID) );
Now, insert values into this table using the INSERT statement as follows −
INSERT INTO CUSTOMERS VALUES (1, ''Ramesh'', 32, ''Ahmedabad'', 2000.00 ), (2, ''Khilan'', 25, ''Delhi'', 1500.00 ), (3, ''Kaushik'', 23, ''Kota'', 2000.00 ), (4, ''Chaitali'', 25, ''Mumbai'', 6500.00 ), (5, ''Hardik'', 27, ''Bhopal'', 8500.00 ), (6, ''Komal'', 22, ''Hyderabad'', 4500.00 ), (7, ''Muffy'', 24, ''Indore'', 10000.00 );
The CUSTOMERS table will be creates as follows −
ID | NAME | AGE | ADDRESS | SALARY |
---|---|---|---|---|
1 | Ramesh | 32 | Ahmedabad | 2000.00 |
2 | Khilan | 25 | Delhi | 1500.00 |
3 | Kaushik | 23 | Kota | 2000.00 |
4 | Chaitali | 25 | Mumbai | 6500.00 |
5 | Hardik | 27 | Bhopal | 8500.00 |
6 | Komal | 22 | Hyderabad | 4500.00 |
7 | Muffy | 24 | Indore | 10000.00 |
The following SELECT INTO statement creates a new table called CUSTOMER_BACKUP and copies the data from the CUSTOMERS table into it −
SELECT * INTO CUSTOMER_BACKUP FROM CUSTOMERS;
Output
We get the following result. We can observe that 7 rows have been modified.
(7 rows affected)
Verification
We can verify whether the changes are reflected in a table by retrieving its contents using the SELECT statement. Following is the query to display the records in the CUSTOMER_BACKUP table −
SELECT * from CUSTOMER_BACKUP;
The table displayed is as follows −
ID | NAME | AGE | ADDRESS | SALARY |
---|---|---|---|---|
1 | Ramesh | 32 | Ahmedabad | 2000.00 |
2 | Khilan | 25 | Delhi | 1500.00 |
3 | Kaushik | 23 | Kota | 2000.00 |
4 | Chaitali | 25 | Mumbai | 6500.00 |
5 | Hardik | 27 | Bhopal | 8500.00 |
6 | Komal | 22 | Hyderabad | 4500.00 |
7 | Muffy | 24 | Indore | 10000.00 |
Copying Data From Specific Columns
We can also copy data from specific columns from an existing table into the new table using the SQL SELECT INTO statement. To do so, we just need to include the required column names after the select keyword.
Syntax
Following is the syntax −
SELECT column1, column2, ..., columnN INTO new_table_name FROM existing_table_name;
Example
In the following query, we are creating a new table called CUSTOMER_DETAILS with only the NAME, AGE, and ADDRESS columns from the CUSTOMERS table, and populate it with the corresponding data.
SELECT name, age, address INTO CUSTOMER_DETAILS FROM CUSTOMERS;
Output
We get the following result. We can observe that 7 rows have been modified.
(7 rows affected)
Verification
We can verify whether the changes are reflected in a table by retrieving its contents using the SELECT statement. Following is the query to display the records in the CUSTOMER_DETAILS table −
SELECT * from CUSTOMER_DETAILS;
The table displayed is as follows −
NAME | AGE | ADDRESS |
---|---|---|
Ramesh | 32 | Ahmedabad |
Khilan | 25 | Delhi |
Kaushik | 23 | Kota |
Chaitali | 25 | Mumbai |
Hardik | 27 | Bhopal |
Komal | 22 | Hyderabad |
Muffy | 24 | Indore |
Note: The new table will not include any other columns from the original table. Also the original table remains unchanged.
Copying Data From Multiple Tables
Using the SQL SELECT INTO statement we can also copy data from multiple tables to a new table. This is accomplished using the JOIN clause which combines the data from multiple tables (based on a common column).
Syntax
Following is the syntax to copy data from multiple tables using the SELECT INTO statement −
SELECT column1, column2, ..., columnN INTO new_table_name FROM table1 JOIN table2 ON table1.column = table2.column
Example
First of all, let us create another table named ORDERS −
CREATE TABLE ORDERS ( OID INT NOT NULL, DATE VARCHAR (20) NOT NULL, CUSTOMER_ID INT NOT NULL, AMOUNT DECIMAL (18, 2));
Using the INSERT statement, insert values into this table as follows −
INSERT INTO ORDERS VALUES (102, ''2009-10-08 00:00:00'', 3, 3000.00), (100, ''2009-10-08 00:00:00'', 3, 1500.00), (101, ''2009-11-20 00:00:00'', 2, 1560.00), (103, ''2008-05-20 00:00:00'', 4, 2060.00);
The table is created as −
OID | DATE | CUSTOMER_ID | AMOUNT |
---|---|---|---|
102 | 2009-10-08 00:00:00 | 3 | 3000.00 |
100 | 2009-10-08 00:00:00 | 3 | 1500.00 |
101 | 2009-11-20 00:00:00 | 2 | 1560.00 |
103 | 2008-05-20 00:00:00 | 4 | 2060.00 |
Now, we are creating a new table called CUSTOMER_ORDERS that includes the customer name from the CUSTOMERS table and the customer id from the ORDERS table, where the id of customers from the CUSTOMERS table matches with the id of customers from the ORDERS table −
SELECT CUSTOMERS.Name, ORDERS.customer_id INTO CUSTOMER_ORDERS FROM CUSTOMERS LEFT JOIN ORDERS ON CUSTOMERS.ID = ORDERS.customer_id;
Output
We get the following result. We can observe that 8 rows have been modified.
(8 rows affected)
Verification
We can verify whether the changes are reflected in a table by retrieving its contents using the SELECT statement. Following is the query to display the records in the CUSTOMER_ORDERS table −
SELECT * FROM CUSTOMER_ORDERS;
The table displayed is as follows −
NAME | customer_id |
---|---|
Ramesh | NULL |
Khilan | 2 |
Kaushik | 3 |
Kaushik | 3 |
Chailtali | 4 |
Hardik | NULL |
Komal | NULL |
Muffy | NULL |
Copying Specific Records
We can also use the SQL SELECT INTO statement with a WHERE clause to create a new table and copy specific rows from an existing table into it.
Syntax
Following is the syntax for using SELECT INTO statement with a WHERE clause −
SELECT * INTO new_table_name FROM existing_table_name WHERE condition;
Example
Using the following query we are creating a new table called NameStartsWith_K that includes all columns from the CUSTOMERS table, but it only stores the records of the customers whose name starts with “k”.
SELECT * INTO NameStartsWith_K FROM CUSTOMERS WHERE NAME LIKE ''k%
Output
We get the following result. We can observe that 3 rows have been modified.
(3 rows affected)
Verification
We can verify whether the changes are reflected in a table by retrieving its contents using the SELECT statement.
SELECT * from NameStartsWith_K;
The table displayed is as follows −
ID | NAME | AGE | ADDRESS | SALARY |
---|---|---|---|---|
2 | Khilan | 25 | Delhi | 1500.00 |
3 | Kaushik | 23 | Kota | 2000.00 |
6 | Komal | 22 | Hyderabad | 4500.00 |
Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc