What is SQL SELECT INTO Statement?
The SQL SELECT INTO statement in SQL Server creates a new table from the existing table by copying the columns of the existing table. It is used for bulk data transfers.
The constraints such as not null, unique, etc. of the source table are not copied to the destination table. We can modify the SQL SELECT INTO statement to copy only a particular set of rows using the WHERE clause. Similarly, we can also select columns to be copied into the new table.
Syntax
The syntax for the SQL SELECT INTO statement in SQL Server is as follows:
The above query will copy N columns into the new_table from an existing table calledOld_table for all the rows where the condition in the where clause satisfies.
Parameters or Arguments
Let us look at all the parameters used and their meaning in the above SQL SELECT INTO Statement.
- column1, column2,...,columnN - It is the list of columns from the parent table (existing table) to be copied to the new table. We can also use * to copy all the columns of the old table.
- New_Table - It is the new table that is to be created by this query. Its name should be unique. It will contain all the columns specified by the query. This new table will not have constraints like not null, unique, etc. from the Old_table.
- Old_table - It is the parent table from which data is to be copied to the new table. We can also use the JOIN clause to join multiple existing tables.
- WHERE condition - It is an optional clause that can be used to select only particular records from the table. In case we only want rows that satisfy the given condition are only copied into the new table.
Data Types
In SQL Server, the data type of the column such as Varchar(), Integer, Date, etc remains the same in the new table as well.
When a primary key column (which is unique for each record, and can be used to uniquely identify the record) of the existing table is copied to the new table, the column inherits the primary key property as well. In the cases listed below, the primary key column doesn't inherit this property:
- The SELECT statement contains more than one table using join.
- UNION is used to join multiple SELECT statements.
- The identity column is either part of an expression or is listed more than once in the select list.
Even if any one of the above conditions is true the column created is not the primary key instead it is set to NOT NULL.
Limitations and Restrictions
The SQL SELECT INTO statement has a few limitations which are important to be noted as stated below:
- A partitioned table cannot be created using SQL SELECT INTO. It is not useful even when the parent or existing table is partitioned table.
Note: Partitioned table is a large table that is divided into small tables so that it is easier to manage and query of the data.
- Indexes, constraints, and triggers are not transferred to the new table from the old table. We are required to define them manually after the SQL SELECT INTO statement.
- The ORDER BY clause used to sort the data in a particular order does not guarantee the specified order of the records in the new table. In other words, the order by clause is sometimes ineffective when using the SELECT INTO statement.
- We cannot complete a column while inserting it using select into statement. Even if we use an expression it inserts old values only.
Logging Behavior
Database logging is a method of keeping records of the changes made to the database or in a narrower vision, changes made to a particular table. They help in the recovery of the database from any kind of failure.
The amount of logging using the SQL SELECT INTO statement depends on the recovery model in the effect of the database. If the database is using a simple recovery model or bulk-logged recovery model, bulk operations are minimally logged. This makes the SELECT INTO Statement efficient. But the SELECT INTO statements are fully logged operations and thus make it inefficient. To make it minimally logged we can set the derived UserDataAccess property to 0.
Examples
Let us take a few examples to understand the working of the SQL SELECT INTO statement.
Example 1:
Let us create a Table Employee that stores employee details as EmpCode, EmpFullName, Job, Manager, HireDate, and DEPTCODE. Now, let us assume we wish to create another table that has EmpCode, EmpfullName, and DEPTCODE of all the software engineers only. To get this table from the Parent table Employee we will be using the SQL SELECT INTO statement.
Let us first create the EMPLOYEE table.
Output:
EmpCode | EmpFullName | Job | Manager | HireDate | DEPTCODE |
---|---|---|---|---|---|
9369 | TONY STARK | SOFTWARE ENGINEER | 7902 | 1980-12-17 | 20 |
9654 | SAM MILES | SALESMAN | 7698 | 1981-09-28 | 30 |
9782 | KEVIN HILL | MANAGER | 7839 | 1981-06-09 | 10 |
9788 | CONNIE SMITH | ANALYST | 7566 | 1982-12-09 | 20 |
9839 | ALFRED KINSLEY | PRESIDENT | 7566 | 1981-11-17 | 10 |
9698 | BELLA SWAN | MANAGER | 7839 | 1981-05-01 | 30 |
9777 | MADII HIMBURY | ANALYST | 7839 | 1981-05-01 | NULL |
9860 | ATHENA WILSON | ANALYST | 7839 | 1992-06-21 | 50 |
9499 | TIM ADOLF | SALESMAN | 7698 | 1981-02-20 | 30 |
9566 | KIM JARVIS | MANAGER | 7839 | 1981-04-02 | 20 |
9844 | PAUL TIMOTHY | SALESMAN | 7698 | 1981-09-08 | 30 |
9876 | JOHN ASGHAR | SOFTWARE ENGINEER | 7788 | 1983-01-12 | 20 |
9900 | ROSE SUMMERS | TECHNICAL LEAD | 7698 | 1981-12-03 | 20 |
9902 | ANDREW FAULKNER | ANAYLYST | 7566 | 1981-12-03 | 10 |
9934 | KAREN MATTHEWS | SOFTWARE ENGINEER | 7782 | 1982-01-23 | 20 |
9591 | WENDY SHAWN | SALESMAN | 7698 | 1981-02-22 | 30 |
9861 | JENNIFER HUETTE | ANALYST | 7839 | 1996-07-01 | 50 |
Output:
EmpCode | EmpFullName | DEPTCODE |
---|---|---|
9369 | TONY STARK | 20 |
9876 | JOHN ASGHAR | 20 |
9934 | KAREN MATTHEWS | 20 |
The above code copies specified rows and columns from the employee table to the Software_Engineers table.
Example 2
Let us take another example where we use join as well along with select into a statement. The first table will be CUSTOMERS and the other one will be ORDERS. We will combine these tables to get the order details and the address for delivery from the customer's table.
Creating Customers and Orders table
Output:
customer_id | customer_name | address | city | state | zip_code |
---|---|---|---|---|---|
156 | Lyndsey | 759 River Dr. | Buffalo | CA | 61422 |
585 | Pamelia | 472 Chestnut Ave. | NY | NA | 85401 |
487 | Kashka | 284 Pearl Lane | Uniondale | NY | 62697 |
219 | Daryl | 20 Brown St. | Fairport | NY | 18593 |
103 | Genoveva | 18 Vine Street | Campbell | CA | 96247 |
Output:
ORDER_ID | CUSTOMER_ID | ORDER_AMT |
---|---|---|
1001 | 585 | 9283.91 |
1038 | 103 | 5720.56 |
1089 | 487 | 6742.37 |
Output:
customer_id | order_id | address | city | state | zip_code |
---|---|---|---|---|---|
585 | 1001 | 472 Chestnut Ave. | NY | NA | 85401 |
487 | 1089 | 284 Pearl Lane | UnionDale | NY | 62697 |
103 | 1038 | 18 Vine Street | Campbell | CA | 96247 |
In the above example, we are performing a left join on two tables Order and Customer to generate a new table CurrentOrders using the SELECT INTO statement.
INSERT INTO SELECT vs SELECT INTO
-
INSERT INTO AND SELECT INTO both statements can be used to insert data from one table to another. But INSERT INTO can only be used if the new table exists in our database. SELECT INTO statement can be used even if the target table does not exist. It automatically creates a new table if it doesn't exist.
Let us say I have only one table called patients and I want to create another table that has a list of dental patients. Here I can use the SELECT INTO statement but if I use the INSERT INTO statement it will cause an error.
Now, once I have both the tables and I wish to update existing table patients with entries from dental patients I can use the INSERT INTO statement.
-
Another difference between the two statements is inserted inserts data in the predefined table but SELECT INTO defines a table by itself which sometimes may not structure the table as expected.
To learn more about SQL visit SQL OPERATORS next.
Conclusion
- In SQL SELECT INTO statement is used to create a new table from one or more than one table in the database.
- We can modify columns as well as rows to be inserted into the new table from the old table.
- The constraints of the parent table are not inherited by the new table.
- It has a few limitations such as a partitioned table cannot be created simply using the SELECT INTO statement, the Order By clause is not efficient, and the computation of a column is not possible.
- Logging behavior depends on the recovery model, logging can be manually controlled using the UserDataAccess property.
- SELECT INTO AND INSERT INTO statements perform similar operations but the SELECT INTO statement is effective even if the new table doesn't exist.