📜  SELECT INTO - SQL (1)

📅  最后修改于: 2023-12-03 15:34:54.305000             🧑  作者: Mango

SELECT INTO in SQL

SELECT INTO is a SQL statement used to create a new table from an existing table. It is often used to make a copy of a table or to select specific columns and data from one table and insert it into a new table.

The basic syntax of the SELECT INTO statement is as follows:

SELECT column1, column2, ...
INTO new_table
FROM existing_table
WHERE conditions;

In this syntax, column1, column2, ... are the names of the columns to be selected from the existing_table. The new_table is the name of the new table that will be created. The WHERE clause is used to filter the rows selected from the existing_table.

Example:

Suppose we have an employees table with the following columns:

| EmployeeID | FirstName | LastName | Department | Salary | |------------|-----------|----------|------------|--------| | 1 | John | Smith | HR | 50000 | | 2 | Jane | Doe | IT | 60000 | | 3 | Bob | Jones | Sales | 55000 |

If we want to create a new table called hr_employees that only includes employees from the HR department, we can use the following SELECT INTO statement:

SELECT EmployeeID, FirstName, LastName, Salary
INTO hr_employees
FROM employees
WHERE Department = 'HR';

This will create a new table called hr_employees with the same columns as the original table, but only containing the data of HR department employees.

Conclusion:

SELECT INTO is a powerful SQL statement that allows you to easily create new tables from existing tables. It is especially useful when you need to extract a subset of data from a large table or make a copy of a table for backup or review purposes.