SQL SELECT INTO
Here is a clear, practical, and complete guide to the SQL SELECT INTO statement, including examples for SQL Server, PostgreSQL, and MySQL.
β
What SELECT INTO Does
SELECT INTO creates a new table and inserts the results of a SELECT query into it.
β‘οΈ Commonly used for:
-
Creating backup tables
-
Copying part of a table
-
Creating tables from query results
Supported in: SQL Server, PostgreSQL (variant), Oracle
Not supported in: MySQL (use CREATE TABLE AS SELECT)
πΉ Basic Syntax (SQL Server)
π Example: Copy an entire table
β Creates a new table customers_backup
β Copies all data from customers
π Example: Copy only filtered rows
β New table contains only active customers
π Example: Create a table from a JOIN
β Creates a reporting table based on a JOIN result
π₯ SQL Server: Create an empty table with structure only
β Table created
β Zero rows inserted
π§© PostgreSQL Equivalent
PostgreSQL does not support SELECT INTO for table creation in modern versions.
Use CREATE TABLE AS:
Equivalent to SQL Serverβs SELECT INTO.
π§© MySQL Equivalent
MySQL also uses CREATE TABLE AS SELECT:
π Performance Notes
β SELECT INTO is fast for creating backup/migration tables
β New table inherits data types, but not:
-
Indexes
-
Constraints
-
Primary keys
-
Foreign keys
If you need those, create them separately after the table is created.
