
- SQL - Home
- SQL - Roadmap
- SQL - Overview
- SQL - RDBMS Concepts
- SQL - Databases
- SQL - Syntax
- SQL - Data Types
- SQL - Operators
- SQL - Expressions
- SQL - Comments
- SQL Database
- SQL - Create Database
- SQL - Drop Database
- SQL - Select Database
- SQL - Rename Database
- SQL - Show Databases
- SQL - Backup Database
- SQL Table
- SQL - Create Table
- SQL - Show Tables
- SQL - Rename Table
- SQL - Truncate Table
- SQL - Clone Tables
- SQL - Temporary Tables
- SQL - Alter Tables
- SQL - Drop Table
- SQL - Delete Table
- SQL - Constraints
- SQL Queries
- SQL - Insert Query
- SQL - Select Query
- SQL - Select Into
- SQL - Insert Into Select
- SQL - Update Query
- SQL - Delete Query
- SQL - Sorting Results
- SQL Views
- SQL - Create Views
- SQL - Update Views
- SQL - Drop Views
- SQL - Rename Views
- SQL Operators and Clauses
- SQL - Where Clause
- SQL - Top Clause
- SQL - Distinct Clause
- SQL - Order By Clause
- SQL - Group By Clause
- SQL - Having Clause
- SQL - AND & OR
- SQL - BOOLEAN (BIT) Operator
- SQL - LIKE Operator
- SQL - IN Operator
- SQL - ANY, ALL Operators
- SQL - EXISTS Operator
- SQL - CASE
- SQL - NOT Operator
- SQL - NOT EQUAL
- SQL - IS NULL
- SQL - IS NOT NULL
- SQL - NOT NULL
- SQL - BETWEEN Operator
- SQL - UNION Operator
- SQL - UNION vs UNION ALL
- SQL - INTERSECT Operator
- SQL - EXCEPT Operator
- SQL - Aliases
- SQL Joins
- SQL - Using Joins
- SQL - Inner Join
- SQL - Left Join
- SQL - Right Join
- SQL - Cross Join
- SQL - Full Join
- SQL - Self Join
- SQL - Delete Join
- SQL - Update Join
- SQL - Left Join vs Right Join
- SQL - Union vs Join
- SQL Keys
- SQL - Unique Key
- SQL - Primary Key
- SQL - Foreign Key
- SQL - Composite Key
- SQL - Alternate Key
- SQL Indexes
- SQL - Indexes
- SQL - Create Index
- SQL - Drop Index
- SQL - Show Indexes
- SQL - Unique Index
- SQL - Clustered Index
- SQL - Non-Clustered Index
- Advanced SQL
- SQL - Wildcards
- SQL - Injection
- SQL - Hosting
- SQL - Min & Max
- SQL - Null Functions
- SQL - Check Constraint
- SQL - Default Constraint
- SQL - Stored Procedures
- SQL - NULL Values
- SQL - Transactions
- SQL - Sub Queries
- SQL - Handling Duplicates
- SQL - Using Sequences
- SQL - Auto Increment
- SQL - Date & Time
- SQL - Cursors
- SQL - Common Table Expression
- SQL - Group By vs Order By
- SQL - IN vs EXISTS
- SQL - Database Tuning
- SQL Function Reference
- SQL - Date Functions
- SQL - String Functions
- SQL - Aggregate Functions
- SQL - Numeric Functions
- SQL - Text & Image Functions
- SQL - Statistical Functions
- SQL - Logical Functions
- SQL - Cursor Functions
- SQL - JSON Functions
- SQL - Conversion Functions
- SQL - Datatype Functions
- SQL Useful Resources
- SQL - Questions and Answers
- SQL - Cheatsheet
- SQL - Quick Guide
- SQL - Useful Functions
- SQL - Useful Resources
- SQL - Discussion
SQL SELECT INTO Statement
SQL SELECT INTO Statement
The SELECT INTO statement in SQL is used to copy data from one table into a new table. It creates the new table automatically and inserts the selected data into it, all in a single step.
This statement is useful when you want to create a backup of a table, copy only specific data, or generate temporary tables for analysis.
The SELECT INTO statement is commonly supported by SQL Server, PostgreSQL, and some other databases. In MySQL, you can use the CREATE TABLE ... SELECT ... statement instead, as it does not support the SELECT INTO statement.
Syntax
Following is the basic syntax to copy all columns from one table to a new table in SQL Server:
SELECT * INTO new_table FROM existing_table;
Example: Copying All Columns
Let us create the CUSTOMERS table which contains the personal details of customers including their name, age, address and salary etc. as shown below:
CREATE TABLE CUSTOMERS ( ID INT NOT NULL, NAME VARCHAR (20) NOT NULL, AGE INT NOT NULL, ADDRESS CHAR (25), SALARY DECIMAL (18, 2), PRIMARY KEY (ID) );
Now, let us insert few records into this table using the INSERT statement as follows:
INSERT INTO CUSTOMERS VALUES (1, 'Ramesh', 32, 'Ahmedabad', 2000.00 ), (2, 'Khilan', 25, 'Delhi', 1500.00 ), (3, 'Kaushik', 23, 'Kota', 2000.00 ), (4, 'Chaitali', 25, 'Mumbai', 6500.00 ), (5, 'Hardik', 27, 'Bhopal', 8500.00 ), (6, 'Komal', 22, 'Hyderabad', 4500.00 ), (7, 'Muffy', 24, 'Indore', 10000.00 );
Now, you can create a copy of this table using the SELECT INTO statement in SQL server as follows:
SELECT * INTO CUSTOMERS_BACKUP FROM CUSTOMERS;
Following is the output obtained:
(7 rows affected) Completion time: 2025-08-05T16:21:57.0492462+05:30
This will create a new table named CUSTOMERS_BACKUP with the same columns and data as the CUSTOMERS table.
Verification
You can now run a SELECT query to view the contents of the new table as shown below:
SELECT * FROM CUSTOMERS_BACKUP;
The table displayed is as follows:
ID | NAME | AGE | ADDRESS | SALARY |
---|---|---|---|---|
1 | Ramesh | 32 | Ahmedabad | 2000.00 |
2 | Khilan | 25 | Delhi | 1500.00 |
3 | Kaushik | 23 | Kota | 2000.00 |
4 | Chaitali | 25 | NULL | NULL |
5 | Hardik | 27 | Bhopal | 1800.00 |
6 | Komal | 22 | Hyderabad | 1600.00 |
7 | Muffy | 24 | Pune | 1900.00 |
Copying Specific Columns Only
You can also copy only selected columns into the new table if you don't need the entire data. This allows you to include just the relevant information.
Syntax
Following is the syntax to copy specific columns from one table to another in SQL Server:
SELECT column1, column2, ... INTO new_table FROM existing_table;
Example
The following statement creates a new table named CUSTOMER_NAMES with only the ID and NAME columns:
SELECT ID, NAME INTO CUSTOMER_NAMES FROM CUSTOMERS;
We get the output as shown below:
(7 rows affected) Completion time: 2025-08-05T16:25:55.9850586+05:30
Verification
To confirm that the new table CUSTOMER_NAMES has been created and contains the expected data, you can use the following SELECT statement:
SELECT * FROM CUSTOMER_NAMES;
This will display all the rows and columns from the newly created CUSTOMER_NAMES table as shown below:
ID | NAME |
---|---|
1 | Ramesh |
2 | Khilan |
3 | Kaushik |
4 | Chaitali |
5 | Hardik |
6 | Komal |
7 | Muffy |
Using WHERE Clause with SELECT INTO
You can use a WHERE clause with SELECT INTO statement to insert only the rows that match a specific condition into the new table.
Syntax
Following is the basic syntax to use WHERE clause with the SELECT INTO statement in SQL Server:
SELECT column1, column2, ... INTO new_table FROM existing_table WHERE condition;
Example
The following query creates a new table with customers whose AGE is greater than 25:
SELECT * INTO CUSTOMERS_OVER25 FROM CUSTOMERS WHERE AGE > 25;
You get the output as shown below:
(2 rows affected) Completion time: 2025-08-05T16:35:02.2841771+05:30
Verification
To check if the new table CUSTOMERS_OVER25 has been created correctly and includes only the desired records, use the following query:
SELECT * FROM CUSTOMERS_OVER25;
This will display all the rows from the CUSTOMERS_OVER25 table, showing only customers whose age is greater than 25:
ID | NAME | AGE | ADDRESS | SALARY |
---|---|---|---|---|
1 | Ramesh | 32 | Ahmedabad | 2000.00 |
5 | Hardik | 27 | Bhopal | 8500.00 |
Copying from Joins Using SELECT INTO
The SELECT INTO statement can also be used with joins to retrieve data from multiple tables. This allows you to create a new table containing combined results based on related data from those tables.
Syntax
Following is the basic syntax to copy data from joins using the SELECT INTO statement in SQL Server:
SELECT t1.column1, t2.column2, ... INTO new_table FROM table1 t1 JOIN table2 t2 ON t1.common_column = t2.common_column WHERE condition;
Example
Assume you have another table named ORDERS having the following structure:
ORDER_ID | CUSTOMER_ID | AMOUNT | ORDER_DATE |
---|---|---|---|
101 | 1 | 250 | 2025-07-01 |
102 | 2 | 300 | 2025-07-03 |
103 | 1 | 150 | 2025-07-05 |
Now, the following query joins the CUSTOMERS and ORDERS tables using the customer ID, and copies the matching data into a new table named CUSTOMER_ORDERS. The new table will include customer ID, name, order ID, and amount for each order:
SELECT C.ID, C.NAME, O.ORDER_ID, O.AMOUNT INTO CUSTOMER_ORDERS FROM CUSTOMERS C JOIN ORDERS O ON C.ID = O.CUSTOMER_ID;
We get the output as shown below:
(3 rows affected) Completion time: 2025-08-05T17:02:18.9907316+05:30
Verification
To verify that the data has been copied correctly into the new table, run the following query:
SELECT * FROM CUSTOMER_ORDERS;
The table displayed is as shown below:
ID | NAME | ORDER_ID | AMOUNT |
---|---|---|---|
1 | Ramesh | 101 | 250.00 |
2 | Khilan | 102 | 300.00 |
1 | Ramesh | 103 | 150.00 |
INSERT INTO SELECT vs SELECT INTO
Both INSERT INTO SELECT and SELECT INTO statements are used to copy data from one table to another, but they work differently based on whether the target table already exists:
Feature | SELECT INTO | INSERT INTO ... SELECT |
---|---|---|
Purpose | Creates a new table and copies data into it. | Inserts data into an existing table from another table. |
Target Table | Must not exist before the query runs. | Must already exist before the query runs. |
Use Cases | Used for creating backups, temporary or reporting tables. | Used for inserting data from one table into another. |
MySQL Support | Not supported in MySQL. Use CREATE TABLE ... SELECT instead. | Fully supported in MySQL and other databases. |
Example |
SELECT * INTO NEW_TABLE FROM EXISTING_TABLE WHERE condition; |
INSERT INTO EXISTING_TABLE (col1, col2) SELECT col1, col2 FROM SOURCE_TABLE WHERE condition; |
Important Points About SELECT INTO Statement
Following are some of the important points to remember about the SELECT INTO statement in SQL:
- The SELECT INTO statement automatically creates a new table. If the table already exists, it will result in an error.
- It is useful for creating backups, generating reports, or setting up temporary tables for analysis.
- You can combine it with clauses like WHERE, JOIN, ORDER BY, and others to control the data being copied.
- It is not supported in MySQL. Instead, use CREATE TABLE ... SELECT ... as an alternative.