SQL Select INTO

      No Comments on SQL Select INTO

The SELECT INTO statement selects data from one table and inserts it into another table.

SQL SELECT INTO Syntax

We use SELECT INTO statement to copy whole tables or selected columns.

Copying all columns

Copying Selected Columns

The new table will be created with the column names and types as defined in the SELECT statement. You can apply new names using the AS clause.

Sample Data

For this example we are going to use Suppliers table. The DDL statement to create the table as well as insert statements are available here in case you haven’t created the table yet.

Example 1 – Create a New Copy of Table Suppliers

Open up a new query window and run the following query:

Now if you click on the Tables and then refresh, you will see the new Suppliers_Copy table. It will have the same column names and same data types for each column as the Supplier table. As shown in figure below:

Example 2 – Copy Only Selected Columns INTO New Table

In this example we will use SELECT INTO clause to copy selected columns from Suppliers table into another table. Following statement will create a new table Suppliers_Columns with two columns from the Suppliers table:

Now if you refresh Tables, you will see a new table named Suppliers_Columns. However this time we only have two columns in this table as the comparison between the original table and new table shows below:

Let’s query Suppliers_Columns table to see the data in that table by running query below:

Results:

Leave a Reply

Your email address will not be published. Required fields are marked *