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 Table
Step 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 Table
Step 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

output
Step 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
.png)
Output
Insert 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.
Similar Reads
SQL SELECT IN Statement
The IN operator in SQL is used to compare a column's value against a set of values. It returns TRUE if the column's value matches any of the values in the specified list, and FALSE if there is no match. In this article, we will learn how IN operator works and provide practical examples to help you b
4 min read
SQL INSERT INTO SELECT Statement
In SQL, the INSERT INTO statement is used to add or insert records into the specified table. We use this statement to insert data directly into a table by specifying column names in a specific order. The SELECT statement is used to retrieve data from the table, and it can be used in conjunction with
6 min read
SQL Server SELECT INTO Statement
SQL Server is a relational database management system. SQL Server offers robust security features to protect data integrity and confidentiality. It includes authentication, authorization, encryption, and various mechanisms to secure the database environment. It is designed to scale from small applic
6 min read
PL/SQL INSERT Statement
The PL/SQL INSERT statement is vital for adding new records to a database table. By specifying the table's name and providing values for its columns, users can populate their database with essential information. This functionality enables efficient data entry and ensures the completeness of datasets
3 min read
Select Statement in MS SQL Server
The SELECT statement in SQL Server is a foundational SQL command used for querying and retrieving data from one or more tables within a database. This command allows users to specify which columns and rows to retrieve and apply filters to focus on specific data and perform various operations to mani
4 min read
SQL INSERT INTO Statement
The SQL INSERT INTO statement is one of the most commonly used commands for adding new data into a table in a database. Whether you're working with customer data, products, or user details, mastering this command is crucial for efficient database management. Letâs break down how this command works,
6 min read
PL/SQL DELETE Statement
In PL/SQL(Procedural Language/Structured Query Language), the DELETE statement is the powerful command used to remove one or more records from the database table. It is an essential part of database management and enables the users to efficiently manage and maintain the data integrity by selectively
7 min read
SQL CASE Statement
The CASE statement in SQL is a versatile conditional expression that enables us to incorporate conditional logic directly within our queries. It allows you to return specific results based on certain conditions, enabling dynamic query outputs. Whether you need to create new columns, modify existing
4 min read
MySQL INSERT INTO Statement
In MySQL, the INSERT INTO statement is essential for adding new data rows to a table in a database. This is important for setting up initial data in tables and for adding new records as needed when working with the database. Understanding how to use the INSERT INTO statement is key for managing and
6 min read
What is Nested Select Statement in PL/SQL?
PL/SQL is a Procedural Language/Structured Query Language and it enables users to write procedural logic directly within the database, including procedures, functions, triggers, and packages. In this article, we will understand Nested select statements in PL/SQL along with the examples and so on. Un
4 min read