SQL SELECT INTO
SELECT INTO
lets you create a new table and insert data from an existing query — all in one step.
It’s handy for quick backups, snapshots, or creating temporary tables.
🔹 Basic Syntax
SELECT column1, column2, ...
INTO new_table
FROM existing_table
WHERE conditions;
🔹 How It Works
- Creates
new_table
with the same columns and data types as selected columns. - Copies the data returned by the query into this new table.
- Only works if
new_table
does not already exist.
🔹 Example
Create a table high_salary_employees
with employees earning more than 70000:
SELECT employee_id, employee_name, salary
INTO high_salary_employees
FROM employees
WHERE salary > 70000;
🔹 Notes
- Supported by SQL Server and some other DBMS (not standard in MySQL or Oracle).
- In MySQL, similar effect achieved with:
CREATE TABLE new_table AS
SELECT column1, column2
FROM existing_table
WHERE conditions;
- In Oracle, use
CREATE TABLE AS SELECT
(CTAS).
🧠 Quick Recap
Key Point | Explanation |
---|---|
Purpose | Create new table and fill it with query results |
Table must not exist | new_table should not exist before execution |
DBMS differences | SELECT INTO (SQL Server), CREATE TABLE AS (MySQL, Oracle) |
💡 Use SELECT INTO
to quickly clone or filter data into a new table without writing separate CREATE and INSERT commands.