In This Article, You Will Know About SQL SELECT INTO Statement.
SQL SELECT INTO Statement – Before moving ahead, let’s know a bit about the SQL EXISTS Operator.
Table of Contents
SQL SELECT INTO Statement
The SQL SELECT INTO statement is used to copy data from one table to a new table.
Syntax
Copy all columns into a new table.
SELECT *
INTO newtable [IN externaldb]
FROM oldtable
WHERE condition;
Copy only some columns into a new table.
SELECT column1, column2, column3, ...
INTO newtable [IN externaldb]
FROM oldtable
WHERE condition;
SELECT INTO Examples
Example: Create a duplicate of table Data_1.
SELECT * INTO newtable
FROM Data_1;
Example: Use IN clause to create a table in a new database.
SELECT * INTO newtable IN 'newdatabase'
FROM Data_1;
Example: Use SQL statement to copy specific columns into a new table.
SELECT Name, Class INTO newtable
FROM Data_1;
Example: Use SQL statement to copy a specific column into a new table.
SELECT Name INTO newtable
FROM Data_1
WHERE Class = 6;
Example: Use SQL statement to copy data from more than one table into a new table.
SELECT Name.Data_1, Section_ID.Data_2
INTO newtable
FROM Data_1
LEFT JOIN Data_2 ON Data_1.S.No = Data_2.S.No;
Example: Use SQL SELECT INTO statement to create a new empty table.
SELECT * INTO mytable
FROM old_table
WHERE 1 = 0;
If you find anything incorrect in the above-discussed topic and have further questions, please comment below.