Learn how to use SQL SELECT INTO statement with the explained examples of this tutorial. Copy all data to the new table using SQL SELECT INTO statement.
You can copy all the columns and data from another table or you can copy only the selected columns from the other table. Specify the column names and the table name to which you want to copy.
In addition to all these, you can also use the other useful clauses to copy the table or table data.
How to Use SQL SELECT INTO Statement to Copy Another Table
To copy another table to the new table, you have to specify the old table name from where you want to copy. Also, specify the new table name to which you want to copy the table. You can also specify only the selected column names which you want to copy to the new table.
Copy all The Columns Using SQL SELECT INTO Statement
You can copy all the columns and its data from another using the syntax given below. Use the wildcard (*) after the SELECT statement with the where condition which you can apply to copy required data only.
Syntax
1 2 |
SELECT * INTO new_tablename FROM old_tablename WHERE condition; |
Copy Only The Selected Columns Using SQL SELECT INTO Statement
In addition to above syntax, you can also copy only the selected column from another table. Specify the column name you want to copy. Also, give the condition to get only the required data from another table.
Syntax
1 2 3 |
SELECT column1, column2, column3...columnN INTO new_tablename FROM old_tablename WHERE condition; |
Parameter Descriptions
Sr.No. | Parameter Name | Description |
---|---|---|
1 | new_tablename | Specify the new table name to which you copy data. |
2 | old_tablename | Enter the name of the table from where you want to copy the data and the columns. |
3 | column1, column2…columnN | Specify only the selected column name which you want to copy from another table. |
Examples to Use SQL SELECT INTO Statement
Create a table for the employee of a company.
1 2 |
SELECT * INTO Company FROM Employee where salary>25000; ); |
The above example copies all the columns from the table Employee. I have also specified the condition her, it will copy the record for the given condition.
1 2 |
SELECT firstname, lastname, address, email INTO Company FROM Employee where salary>25000; ); |
The example given above copy only the selected columns you have specified and the records you have given in the where condition.
You must also learn.
Reference