sql_select_into
SQL SELECT INTO and CREATE TABLE AS SELECT
Section titled “SQL SELECT INTO and CREATE TABLE AS SELECT”SQL provides mechanisms to copy data from one or more tables into a new table. The specific syntax can vary between database systems.
The SELECT INTO statement is primarily used in SQL Server and MS Access, while the CREATE TABLE AS SELECT (CTAS) statement is the standard SQL way and is used in systems like PostgreSQL, MySQL, and Oracle.
The SQL SELECT INTO Statement (SQL Server / MS Access)
Section titled “The SQL SELECT INTO Statement (SQL Server / MS Access)”In SQL Server and MS Access, the SELECT INTO statement copies data from one table and inserts it into a new table. The new table is created automatically by the statement.
SQL SELECT INTO Syntax:
Section titled “SQL SELECT INTO Syntax:”To copy all columns into the new table:
SELECT *INTO NewTableFROM OldTableWHERE condition;To copy specific columns into the new table:
SELECT column1, column2, column_name(s)INTO NewTableFROM OldTableWHERE condition;The new table will be created with the column names and data types as defined in the SELECT statement. You can assign new names to columns using the AS clause.
Note: The IN externaldb clause, to copy into a table in another database, is specific to some systems like MS Access or SQL Server (for linked servers). Its usage and syntax can vary.
The SQL CREATE TABLE AS SELECT (CTAS) Statement (Standard SQL, PostgreSQL, MySQL, Oracle)
Section titled “The SQL CREATE TABLE AS SELECT (CTAS) Statement (Standard SQL, PostgreSQL, MySQL, Oracle)”The more standard and widely supported way to create a table from a query result is the CREATE TABLE AS SELECT (CTAS) statement.
SQL CTAS Syntax:
Section titled “SQL CTAS Syntax:”To copy all columns into the new table:
CREATE TABLE NewTable ASSELECT *FROM OldTableWHERE condition;To copy specific columns into the new table:
CREATE TABLE NewTable ASSELECT column1, column2, column_name(s)FROM OldTableWHERE condition;Similar to SELECT INTO, the new table’s structure is derived from the result set of the SELECT query.
SQL SELECT INTO / CTAS Examples
Section titled “SQL SELECT INTO / CTAS Examples”Assume we have a Customers table. We want to create backups or derived tables.
- Create a backup copy of
Customers:
SQL Server / MS Access:
SELECT *INTO CustomersBackup2024FROM Customers;Standard SQL (PostgreSQL, MySQL, Oracle):
CREATE TABLE CustomersBackup2024 ASSELECT *FROM Customers;- Copy only a few columns into the new table:
SQL Server / MS Access:
SELECT CustomerName, ContactNameINTO CustomerContactsBackupFROM Customers;Standard SQL (PostgreSQL, MySQL, Oracle):
CREATE TABLE CustomerContactsBackup ASSELECT CustomerName, ContactNameFROM Customers;- Copy only customers from a specific country (e.g., ‘Germany’) into the new table:
SQL Server / MS Access:
SELECT *INTO GermanCustomersFROM CustomersWHERE Country = 'Germany';Standard SQL (PostgreSQL, MySQL, Oracle):
CREATE TABLE GermanCustomers ASSELECT *FROM CustomersWHERE Country = 'Germany';- Copy data from more than one table into the new table (e.g.,
CustomersandOrders):
SQL Server / MS Access:
SELECT c.CustomerName, o.OrderID, o.OrderDateINTO CustomerOrderSummaryFROM Customers cLEFT JOIN Orders o ON c.CustomerID = o.CustomerID;Standard SQL (PostgreSQL, MySQL, Oracle):
CREATE TABLE CustomerOrderSummary ASSELECT c.CustomerName, o.OrderID, o.OrderDateFROM Customers cLEFT JOIN Orders o ON c.CustomerID = o.CustomerID;Tip: Creating an empty table with the schema of another.
Section titled “Tip: Creating an empty table with the schema of another.”You can create a new, empty table with the same structure as an existing table by adding a WHERE clause that returns no data (e.g., WHERE 1=0).
SQL Server / MS Access:
SELECT *INTO NewEmptyCustomersTableFROM CustomersWHERE 1=0; -- This condition is always falseStandard SQL (PostgreSQL, MySQL, Oracle):
CREATE TABLE NewEmptyCustomersTable ASSELECT *FROM CustomersWHERE 1=0; -- This condition is always falseSome systems also provide a CREATE TABLE ... LIKE ... syntax (e.g., MySQL, PostgreSQL) for copying table structure without data, which can be more direct: CREATE TABLE NewEmptyCustomersTable LIKE Customers;