The SQL SELECT INTO
statement is a powerful tool for copying data from one table to another. This is often used for tasks such as creating backups, duplicating tables, or transferring data between different tables or databases. It allows you to quickly and efficiently copy data while potentially applying transformations, filtering, or conditions to the copied data.
SELECT INTO
Statement?The SELECT INTO
statement is used to create a new table and populate it with data from an existing table or query result. It is typically used when you want to copy data to a new table structure or create a backup.
Unlike a standard SELECT
query, which retrieves data from an existing table, the SELECT INTO
statement performs two actions at once:
The basic syntax is:
SELECT column1, column2, ...
INTO new_table
FROM existing_table
WHERE condition;
new_table
: The name of the new table that will be created.existing_table
: The table from which data will be selected.column1, column2, ...
: The columns you want to copy from the existing table.WHERE condition
: A condition to filter the data that is copied (optional).If you want to copy all the data from one table into a new table, you can use the SELECT INTO
statement without a WHERE
clause. Here's a basic example:
SELECT *
INTO new_employees
FROM employees;
This query creates a new table called new_employees
and copies all the data from the employees
table into it. The new table will have the same structure as the source table but will be an independent table in the database.
You don’t have to copy all columns; you can select specific columns to be copied to the new table. For example:
SELECT name, position, salary
INTO new_employees
FROM employees;
This query creates the new_employees
table and copies only the name
, position
, and salary
columns from the employees
table.
WHERE
Clause to Filter DataYou can also apply a WHERE
clause to filter the data you are copying. For example, to copy only employees in the 'Engineering' department:
SELECT name, position, salary
INTO engineering_employees
FROM employees
WHERE department = 'Engineering';
This query creates a new table engineering_employees
and copies only the records of employees in the 'Engineering' department.
SELECT INTO
StatementIn some cases, you may want to copy data to a new table but also change the column names. While the SELECT INTO
statement doesn’t allow you to rename columns directly, you can do so by aliasing the columns in the SELECT
part of the statement. For example:
SELECT name AS employee_name, position AS job_title, salary AS annual_salary
INTO new_employees
FROM employees;
This query creates a new table called new_employees
and copies the data from the employees
table, but the columns are renamed as employee_name
, job_title
, and annual_salary
.
If the source table has many columns, but you only want to copy a subset of them, you can select only the necessary columns for the new table. This can be useful when you need to restructure your data:
SELECT id, name, position
INTO employees_summary
FROM employees;
This query creates a new table called employees_summary
with only the id
, name
, and position
columns.
SELECT INTO
StatementSELECT INTO
on Large Tables Without FilteringWhen copying large tables, avoid copying all the data without filtering it, as this could lead to performance issues. Always use a WHERE
clause to filter the data if possible.
For example, instead of copying all employees, you could only copy employees from a specific department:
SELECT id, name, position, salary
INTO marketing_employees
FROM employees
WHERE department = 'Marketing';
If the new table already exists, attempting to use SELECT INTO
will result in an error. Always make sure that the table doesn’t exist before running the query, or you can drop the table first:
DROP TABLE IF EXISTS new_employees;
SELECT *
INTO new_employees
FROM employees;
Alternatively, you can use CREATE TABLE
and then perform an INSERT INTO
if you want more control over the table's structure.
The SELECT INTO
statement only copies data and creates a basic table structure (without indexes, constraints, or primary keys). If you need to add indexes or enforce constraints on the new table, you will need to do this manually after the table is created:
-- Create the new table
SELECT * INTO new_employees FROM employees;
-- Add an index on the 'id' column
CREATE INDEX idx_new_employees_id ON new_employees (id);
SELECT INTO
for Backup or ArchivingYou can use the SELECT INTO
statement to create a backup or archive of a table. This is useful for storing historical data or making a snapshot of a table before making changes:
SELECT * INTO employees_backup
FROM employees;
This creates a backup of the employees
table with all data in the employees_backup
table.
SELECT INTO
StatementSELECT INTO
statement. Use DROP TABLE IF EXISTS
to drop the table before running the SELECT INTO
statement.