TRENDING TOPICS

×

SQL SELECT INTO Statement

SQL SELECT INTO Stateement is used to copies thw whole data OR SELECTED column data from one table [SOURCE TABLE] into a new table.

SELECT INTO Syntax

SELECT * INTO new_table [IN external DB]
FROM source_table
WHERE [condition];

SELECT INTO Syntax [SELECTED COLUMNS]

SELECT column_1, column_2, .... 
INTO new_table
FROM source_table
WHERE [condition];

Important Points of SELECT INTO

  • The WHERE condition is optional to use along with SELECT INTO statement.
  • The number of columns and datatype of columns must be same.
  • The SELECT INTO statement can be also used to create a new empty table using the schema of another.
  • SQL SELECT INTO used to create a table structure with oppropriate data types & INSERT data into in empty table.

EXAMPLE: SQL SELECT INTO

Employee Table

A sample of EMPLOYEE records table & the name of table Employee. The column(s) of the Employee table [ID, EMPLOYEE_NAME, EMPLOYEE_EMAIL, ADDRESS, CITY, and COUNTRY]:

ID EMPLOYEE_NAME EMPLOYEE_EMAIL ADDRESS CITY COUNTRY
1 Mark J. mark@gmail.com House 53, Sq. London U.K.
2 Lack Josh lackjosh@gmail.com 2745 road mark Delhi India
3 Kim Juhu kimjuhu@gmail.com Street 587327, 34 House Seoul South Korea
4 Jack Jenifer jenifer@gmail.com 464673 Mataderos Mexico D.F. Mexico
5 McKinley mckinley@gmail.com 54, Road Washington Washington U.S.
6 Alena aln@gmail.com House No. 6563 Tokyo Japan Japan
7 Mack Marina mmk@gmail.com 43 Road, NH San Francisco U.S.
8 Dipak Jha jha@gmail.com Mahanager, 26 Road Lucknow India

SELECT INTO Example

SELECT * 
INTO backUp_Employee
FROM Employee;

The command will create a new table [backUp_Employee] similar to "Employee" table, And the name of table is backUp_Employee

Result Look Like

ID EMPLOYEE_NAME EMPLOYEE_EMAIL ADDRESS CITY COUNTRY
1 Mark J. mark@gmail.com House 53, Sq. London U.K.
2 Lack Josh lackjosh@gmail.com 2745 road mark Delhi India
3 Kim Juhu kimjuhu@gmail.com Street 587327, 34 House Seoul South Korea
4 Jack Jenifer jenifer@gmail.com 464673 Mataderos Mexico D.F. Mexico
5 McKinley mckinley@gmail.com 54, Road Washington Washington U.S.
6 Alena aln@gmail.com House No. 6563 Tokyo Japan Japan
7 Mack Marina mmk@gmail.com 43 Road, NH San Francisco U.S.
8 Dipak Jha jha@gmail.com Mahanager, 26 Road Lucknow India

SELECT INTO Example [colums]

SELECT EMPLOYEE_NAME, CITY, COUNTRY
INTO backUp_Employee
FROM Employee;

Result Look Like

EMPLOYEE_NAME CITY COUNTRY
Mark J. London U.K.
Lack Josh Delhi India
Kim Juhu Seoul South Korea
Jack Jenifer Mexico D.F. Mexico
McKinley Washington U.S.
Alena Tokyo Japan Japan
Mack Marina San Francisco U.S.
Dipak Jha Lucknow India

© 2021 GDATAMART.COM (All Rights Reserved)