SQL Select Into
In SQL, the SELECT INTO statement is used to create a new table and populate it with data from an existing table. The new table is created based on the column names and data types derived from the columns selected in the query. Here’s the basic syntax:
SELECT column1, column2, ... INTO new_table FROM existing_table WHERE condition;
Let’s break down the syntax:
SELECT column1, column2, ...: Specify the columns you want to select from the existing table.
INTO new_table: Specify the name of the new table you want to create.
FROM existing_table: Specify the name of the existing table from which you want to retrieve data.
WHERE condition: (Optional) Add a condition to filter the rows from the existing table.
Here’s an example to illustrate the usage:
SELECT first_name, last_name, email INTO new_customers FROM customers WHERE registered_year = 2023;
In this example, a new table named
new_customers will be created, and it will contain the columns
customers table, but only for the customers who registered in the year 2023.
It’s important to note that the SELECT INTO statement may differ slightly depending on the specific SQL database system you are using, as there might be variations in the syntax and supported features.