What is SQL SELECT INTO Statement?

Learn via video course
FREE
View all courses
DBMS Course - Master the Fundamentals and Advanced Concepts
DBMS Course - Master the Fundamentals and Advanced Concepts
by Srikanth Varma
1000
5
Start Learning
DBMS Course - Master the Fundamentals and Advanced Concepts
DBMS Course - Master the Fundamentals and Advanced Concepts
by Srikanth Varma
1000
5
Start Learning
Topics Covered

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:

EmpCodeEmpFullNameJobManagerHireDateDEPTCODE
9369TONY STARKSOFTWARE ENGINEER79021980-12-1720
9654SAM MILESSALESMAN76981981-09-2830
9782KEVIN HILLMANAGER78391981-06-0910
9788CONNIE SMITHANALYST75661982-12-0920
9839ALFRED KINSLEYPRESIDENT75661981-11-1710
9698BELLA SWANMANAGER78391981-05-0130
9777MADII HIMBURYANALYST78391981-05-01NULL
9860ATHENA WILSONANALYST78391992-06-2150
9499TIM ADOLFSALESMAN76981981-02-2030
9566KIM JARVISMANAGER78391981-04-0220
9844PAUL TIMOTHYSALESMAN76981981-09-0830
9876JOHN ASGHARSOFTWARE ENGINEER77881983-01-1220
9900ROSE SUMMERSTECHNICAL LEAD76981981-12-0320
9902ANDREW FAULKNERANAYLYST75661981-12-0310
9934KAREN MATTHEWSSOFTWARE ENGINEER77821982-01-2320
9591WENDY SHAWNSALESMAN76981981-02-2230
9861JENNIFER HUETTEANALYST78391996-07-0150

Output:

EmpCodeEmpFullNameDEPTCODE
9369TONY STARK20
9876JOHN ASGHAR20
9934KAREN MATTHEWS20

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_idcustomer_nameaddresscitystatezip_code
156Lyndsey759 River Dr.BuffaloCA61422
585Pamelia472 Chestnut Ave.NYNA85401
487Kashka284 Pearl LaneUniondaleNY62697
219Daryl20 Brown St.FairportNY18593
103Genoveva18 Vine StreetCampbellCA96247

Output:

ORDER_IDCUSTOMER_IDORDER_AMT
10015859283.91
10381035720.56
10894876742.37

Output:

customer_idorder_idaddresscitystatezip_code
5851001472 Chestnut Ave.NYNA85401
4871089284 Pearl LaneUnionDaleNY62697
103103818 Vine StreetCampbellCA96247

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.