The SELECT INTO statement in SQL is used to select data from one table and insert it into a new table. It allows you to create a new table and populate it with the result set of a query.
Syntax:
SELECT column1, column2, ...
INTO new_table
FROM existing_table
WHERE condition;
Example:
- Create a new table and insert data from an existing table:
SELECT first_name, last_name
INTO new_employees
FROM employees
WHERE department = 'Sales';
This creates a new table new_employees and copies the first_name and last_name columns from the employees table where the department is 'Sales'.
- Create a new table with all rows from the existing table:
SELECT *
INTO new_table
FROM old_table;
This copies all rows and columns from old_table to new_table.
Notes:
- The
SELECT INTOstatement creates a new table with the structure defined by the columns selected in the query. - The new table is populated with the data from the selected query result.
- In some databases (like SQL Server),
SELECT INTOmay also create a new table in the specified schema.
If you need to select data into an already existing table, you should use the INSERT INTO statement instead.
No comments:
Post a Comment