SQL SELECT INTO Statement
Last Updated :
16 Dec, 2024
The SELECT INTO
statement in SQL is a powerful and efficient command that allow users to create a new table and populate it with data from an existing table or query result in a single step. This feature is especially useful for creating backups, extracting specific subsets of data, or preparing new tables for analysis.
By automatically creating the target table with the same schema and data types as the source table, SELECT INTO
simplifies workflows and eliminates the need for manual table creation. This guide provides a step-by-step breakdown of the SELECT INTO
statement, complete with examples and comparisons to similar SQL commands.
What is SQL SELECT INTO?
The SELECT INTO
statement is used to create a new table and populate it with data copied from an existing table. Unlike INSERT INTO SELECT
, the SELECT INTO
statement creates the target table automatically if it does not already exist. This makes it particularly useful for scenarios where the schema and data types of the source table need to be replicated quickly.
Key Features of SELECT INTO
- Creates a new table with the same schema and data types as the source table.
- Copies selected rows and columns based on conditions (optional).
- Eliminates the need to manually create the target table before inserting data
Syntax
SELECT column1, column2...
INTO new_table
FROM source_table
WHERE condition;
To copy the entire table:
SELECT * INTO new_table
FROM source_table
WHERE condition;
Key Terms
- column1, column2: Specifies the columns to be copied from the source table.
- new_table: The name of the new table to be created.
- source_table: The table from which data will be copied.
- condition: (Optional) A condition to filter the rows that will be copied. If omitted, all rows are copied.
Step-by-Step Guide to Using SELECT INTO
The SELECT INTO
statement in SQL allows us to create a new table and populate it with data from an existing table in a single operation. This is particularly useful for creating backups or temporary tables for analysis. Let us look at some examples of the SELECT INTO statement in SQL, and understand how to use it.
Step 1: Create the Source Table
Before using the SELECT INTO
statement, we need a source table. Here’s how to create a sample Customer
table and insert some data:
Query:
CREATE TABLE Customer(
CustomerID INT PRIMARY KEY,
CustomerName VARCHAR(50),
LastName VARCHAR(50),
Country VARCHAR(50),
Age int(2),
Phone int(10)
);
-- Insert some sample data into the Customers table
INSERT INTO Customer (CustomerID, CustomerName, LastName, Country, Age, Phone)
VALUES (1, 'Shubham', 'Thakur', 'India','23','xxxxxxxxxx'),
(2, 'Aman ', 'Chopra', 'Australia','21','xxxxxxxxxx'),
(3, 'Naveen', 'Tulasi', 'Sri lanka','24','xxxxxxxxxx'),
(4, 'Aditya', 'Arpan', 'Austria','21','xxxxxxxxxx'),
(5, 'Nishant. Salchichas S.A.', 'Jain', 'Spain','22','xxxxxxxxxx');
Customer Table
Customer TableStep 2: Copy the Entire Table Using SELECT INTO
To create a backup of the Customer
table, we use the SELECT INTO
statement. This creates a new table called backUpCustomer
and copies all data into it.
Query:
SELECT *
INTO backUpCustomer
FROM Customer;
Output
backUp Customer TableStep 3: Copy Specific Rows Using WHERE Clause
The WHERE
clause allows us to filter rows when copying data. For example, to copy only customers from India: Use the 'where' clause to copy only some rows from Customer into the backUpCustomer table.
Query:
SELECT *
INTO IndianCustomers
FROM Customer
WHERE Country = 'India';
Output
outputStep 4: Copy Specific Columns
To copy only some columns from Customer into the backUpCustomer table specify them in the query.
Query:
SELECT CustomerName, LastName, Age
INTO CustomerSummary
FROM Customer;
Output
OutputInsert INTO SELECT vs SELECT INTO
Both statements could be used to copy data from one table to another. But INSERT INTO SELECT could be used only if the target table exists whereas SELECT INTO statement could be used even if the target table doesn't exist as it creates the target table if it doesn't exist.
INSERT INTO backUpCustomer select * from Customer;
HERE table tempTable should be present or created beforehand else throw an error. 
SELECT * INTO backUpCustomer from Customer;
Here it's not necessary to exist before as SELECT INTO creates a table if the table doesn't exist and then copies the data.
Differences Between SELECT INTO and INSERT INTO SELECT
Feature | SELECT INTO | INSERT INTO SELECT |
---|
Table Creation | Creates the table if it doesn’t exist | Requires the table to exist beforehand |
Performance | Faster for large datasets | Slower due to table existence checks |
Flexibility | Less flexible; creates table schema | More flexible; works with existing tables |
Syntax Example | SELECT * INTO NewTable FROM OldTable; | INSERT INTO ExistingTable SELECT * FROM OldTable; |
Conclusion
The SELECT INTO
statement is an efficient way to create and populate a new table from an existing one. Whether we want to create backups, filter rows, or copy specific columns, this flexible command simplifies our SQL workflows. However, remember that its compatibility may vary across SQL platforms, and using an offline SQL editor is recommended for execution.
Mastering SELECT INTO
along with its comparison to INSERT INTO SELECT
can significantly enhance our SQL skills, enabling us to handle diverse data manipulation scenarios effectively.