TechnoLearnAcademy

TECHNOLEARN

SQL SELECT INTO Statement

The SQL SELECT INTO statement is used in SQL to copy data from one table and insert it into a new table.
It’s commonly used for creating backup and temporary tables or extracting a subset of data from one table and storing it in another.
SYNTAX:
                       SELECT column1, column2, …
                                    INTO new_table
                                              FROM source_table
                                                 WHERE condition;
Let’s understand the select into statement with an example. For demonstration purposes, we will be using the table emp
         Table EMP
Example:
   Objective: In this example, we will create a new table using the old table’s column names and data types.
                      SELECT * INTO Tbl_Emp_Copy                                                                                                                                                                         FROM Tbl_Emp                    
    Query:
                      SELECT * FROM Tbl_Emp_Copy;
    Output:

Example:

   Objective: In this example, we will be copying a few columns into a new table. 
                      SELECT Ecode , FirstName , LastName , Emailaddress FROM Tbl_Emp                                                                                                                                                               INTO Tbl_Emp_Copy 
    Query:
                      SELECT * FROM Tbl_Emp_Copy;

Example:

   Objective: IIn this example, we will create a new table based on certain conditions. 
                      SELECT Ecode , FirstName , LastName , Emailaddress FROM Tbl_Emp                                                                                                                                                              INTO Tbl_Emp_Copy                                                                                                                                                                                    WHERE Department = ‘IT’
    Query:
                      SELECT * FROM Tbl_Emp_Copy;

Example:

   Objective: In this example, We will create a new table with the same structure as the original table, but without any data                        copied. 
                      SELECT Ecode , FirstName , LastName , Emailaddress FROM Tbl_Emp                                                                                                                                                              INTO Tbl_Emp_Copy                                                                                                                                                                                    WHERE 1 = 0
    Query:
                      SELECT * FROM Tbl_Emp_Copy;
Scroll to Top