SQL INSERT INTO SELECT Statement

In SQL, the INSERT INTO SELECT Statement is used to insert data into the table directly. The data which is inserted is the result we get from the SELECT statement. This statement is generally used when we want to copy some data from one table to another. There is a thing to remember, the data returned by the SELECT statement must be compatible with other table columns on which we try to insert them. Otherwise, it will throw us an error.

Syntax:

INSERT INTO table_01 (column_01, column_02,…………………)

SELECT (column_01, column_02,…………………)

FROM table_02;

SQL INSERT INTO SELECT Statement

In SQL, the INSERT INTO statement is used to add or insert records to the specified table. We can use this statement to add data directly to the table. We use the VALUES keyword along with the INSERT INTO statement. VALUES keyword is accompanied by column names in a specific order in which we want to insert values in them. SELECT statement is used to retrieve data from the table. We can use a SELECT statement along with a where clause too in order to fetch some specific data from the table.

In this article, we are going to learn how to use the INSERT INTO statement along with the SELECT statement. We will be going through its various examples along with their respective explanations. We will also see how it can be used in real-world situations.

Similar Reads

SQL INSERT INTO SELECT Statement

In SQL, the INSERT INTO SELECT Statement is used to insert data into the table directly. The data which is inserted is the result we get from the SELECT statement. This statement is generally used when we want to copy some data from one table to another. There is a thing to remember, the data returned by the SELECT statement must be compatible with other table columns on which we try to insert them. Otherwise, it will throw us an error....

Examples of INSERT INTO SELECT Statement

Before moving to some examples, we need to create at-least two table in our database first....

Example of SQL INSERT INTO SELECT Statement

Example 1: INSERT INTO SELECT Statement Without Using WHERE Clause....

Conclusion

In SQL, INSERT INTO SELECT Statement is generally used to copy data from one table to some other table. We can also filter out the first table’s data before inserting it into another table with the help of WHERE Clause. Thing to keep in mind while copying data from one table to another, is that data of the first table should be compatible with the data types of another table. Otherwise, this will throw us an error. In this article, we have covered all the concepts will clear and concise examples along with their respective explanations....