- SQL - Discussion
- SQL - Useful Resources
- SQL - Useful Functions
- SQL - Quick Guide
- SQL - Questions and Answers
- SQL - Datatype Functions
- SQL - Conversion Functions
- SQL - JSON Functions
- SQL - Cursor Functions
- SQL - Logical Functions
- SQL - Statistical Functions
- SQL - Text & Image Functions
- SQL - Numeric Functions
- SQL - Aggregate Functions
- SQL - String Functions
- SQL - Date Functions
- SQL - Database Tuning
- SQL - IN vs EXISTS
- SQL - Group By vs Order By
- SQL - Common Table Expression
- SQL - Cursors
- SQL - Date & Time
- SQL - Auto Increment
- SQL - Using Sequences
- SQL - Handling Duplicates
- SQL - Sub Queries
- SQL - Transactions
- SQL - NULL Values
- SQL - Stored Procedures
- SQL - Default Constraint
- SQL - Check Constraint
- SQL - Null Functions
- SQL - Min & Max
- SQL - Hosting
- SQL - Injection
- SQL - Comments
- SQL - Wildcards
- SQL - Non-Clustered Index
- SQL - Clustered Index
- SQL - Unique Index
- SQL - Primary Key
- - 工会诉Join
- SQL - Inner Join
- SQL - Using Joins
- SQL - Aliases
- SQL - EXCEPT Operator
- SQL - INTERSECT Operator
- SQL - UNION vs UNION ALL
- SQL - UNION Operator
- SQL - BETWEEN Operator
- SQL - NOT NULL
- SQL - IS NOT NULL
- SQL - IS NULL
- SQL - NOT EQUAL
- SQL - NOT Operator
- SQL - CASE
- SQL - EXISTS Operator
- SQL - ANY, ALL Operators
- SQL - IN Operator
- SQL - LIKE Operator
- SQL - BOOLEAN (BIT) Operator
- SQL - AND & OR
- SQL - Having Clause
- SQL - Group By Clause
- SQL - Order By Clause
- SQL - Distinct Clause
- SQL - Top Clause
- SQL - Where Clause
- SQL - Rename Views
- SQL - Drop Views
- SQL - Update Views
- SQL - Create Views
- SQL - Sorting Results
- SQL - Delete Query
- SQL - Update Query
- SQL - Insert Into Select
- SQL - Select Into
- SQL - Select Query
- SQL - Insert Query
- SQL - Constraints
- SQL - Delete Table
- SQL - Drop Table
- SQL - Alter Tables
- SQL - Temporary Tables
- SQL - Clone Tables
- SQL - Truncate Table
- SQL - Rename Table
- SQL - Show Tables
- SQL - Create Table
- SQL - Backup Database
- SQL - Show Database
- SQL - Rename Database
- SQL - Select Database
- SQL - Drop Database
- SQL - Create Database
- SQL - Expressions
- SQL - Operators
- SQL - Data Types
- SQL - Syntax
- SQL - Databases
- SQL - RDBMS Concepts
- SQL - Overview
- SQL - Home
5. 图瓦卢
- 页: 1
- 页: 1
- 结构-创建指数
- 页: 1
- 页: 1
- 页: 1
- SQL - Foreign Key
- 文 件
- ∗ E/CN.6/2009/1。
- 页: 1
- 页: 1
- 文 件
- 页: 1
- 页: 1
- 文 件
- 页: 1
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
SQL - Insert Into... Select
In SQL, the INSERT INTO... SELECT statement is used to add/insert one or more new rows (records) 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 commands 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 matches the structure and 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 for using insert into select statement −
INSERT INTO table2 SELECT * FROM table1
Before we are going to use the above query, we have to consider the following points −
In the database where we are going to insert data, a table must already exist.
The table structure of the source and target tables must match.
Example
Let’s consider the following table we are going to create for further explanation of "INSERT INTO SELECT."
CREATE TABLE Agent ( id INT NOT NULL, name VARCHAR(50) NOT NULL, gender VARCHAR(20) NOT NULL, age INT NOT NULL, PRIMARY KEY(ID) );
Now, we are going to populate the table −
INSERT INTO Agent VALUES (1, Msd , Male , 21), (2, Virat , Male , 23), (3, Perry , Female , 24), (4, Smiti , Female , 18), (5, Rose , Female , 23), (6, Jack , Male , 22);
We can use the following query to check whether the table has been created or not −
SELECT * FROM Agent;
On running it will display a table as −
+----+-------+--------+-----+ | id | name | gender | age | +----+-------+--------+-----+ | 1 | Msd | Male | 21 | | 2 | Virat | Male | 23 | | 3 | Perry | Female | 24 | | 4 | Smiti | Female | 18 | | 5 | Rose | Female | 23 | | 6 | Jack | Male | 22 | +----+-------+--------+-----+
Now we are going to create a table and use it as a demonstration for our examples −
CREATE TABLE Agentdemo ( id INT NOT NULL, name VARCHAR(50) NOT NULL, gender VARCHAR(20) NOT NULL, age INT NOT NULL, PRIMARY KEY(ID) );
Inserting required data from one table to another table
Sometimes we only need to add a small number of records to another table. This can be accomppshed by using a WHERE clause to select the number of rows that the query returned.
Execute the following query, which fetches the gender ="male" from table Agent and inserts it in the table Agentdemo.
INSERT INTO Agentdemo(id,name, gender, age) SELECT id,name, gender, age FROM Agent WHERE gender = male ;
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 Agentdemo table −
SELECT * FROM Agentdemo;
On running the above query, it will generate the following output, as shown below −
+----+-------+--------+-----+ | id | name | gender | age | +----+-------+--------+-----+ | 1 | Msd | Male | 21 | | 2 | Virat | Male | 23 | | 6 | Jack | Male | 22 | +----+-------+--------+-----+
Inserting the top N rows required
The TOP clause details the number of rows from the query that should be added to the target table. This may be accomppshed by first truncating all rows in the Agentdemo table using the following statement −
TRUNCATE TABLE Agentdemo;
Now, we are going to insert the top 3 agents sorted by their age by using the following query, shown below −
INSERT TOP (3) INTO Agentdemo(id,name, gender, age) SELECT id,name, gender, age FROM Agent ORDER BY age;
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 Agentdemo table −
SELECT * FROM Agentdemo;
It will display the output as shown below after running the above query.
+----+-------+--------+-----+ | id | name | gender | age | +----+-------+--------+-----+ | 1 | Msd | Male | 21 | | 2 | Virat | Male | 23 | | 3 | Perry | Female | 24 | +----+-------+--------+-----+
Inserting all data from one table to another table
Imagine that we want to add every piece of information from the Agent table to the Agentdemo table. To do this, first truncate all rows in the Agentdemo table by using the statement −
TRUNCATE TABLE Agentdemo;
Execute the following query to add all information from the Agent table to the Agentdemo table −
INSERT INTO Agentdemo(id,name, gender, age) SELECT id,name, gender, age FROM Agent;
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 Agentdemo table −
SELECT * FROM Agentdemo;
On executing the above query, it will generate an output as shown below −
+----+-------+--------+-----+ | id | name | gender | age | +----+-------+--------+-----+ | 1 | Msd | Male | 21 | | 2 | Virat | Male | 23 | | 3 | Perry | Female | 24 | | 4 | Smiti | Female | 18 | | 5 | Rose | Female | 23 | | 6 | Jack | Male | 22 | +----+-------+--------+-----+Advertisements