SQL SELECT INTO
Sometimes you want to save the result of a query as a new table — a backup before a risky update, a snapshot for a report, or a smaller working copy. Standard SQL and most databases do this with CREATE TABLE … AS SELECT; SQL Server and MS Access use SELECT … INTO.
| Database | Copy a query result into a new table |
|---|---|
| SQLite, PostgreSQL, MySQL, Oracle | CREATE TABLE new_table AS SELECT … |
| SQL Server, MS Access | SELECT … INTO new_table FROM … |
| PostgreSQL | both forms (SELECT INTO is a legacy form) |
-- SQL Server
SELECT * INTO customers_backup FROM customers;
-- SQLite, PostgreSQL, MySQL
CREATE TABLE customers_backup AS SELECT * FROM customers;A full backup
CREATE TABLE products_backup AS
SELECT * FROM products;
UPDATE products SET price = price * 2;
SELECT b.name, b.price AS before, p.price AS after
FROM products_backup AS b
JOIN products AS p ON p.product_id = b.product_id
WHERE b.product_id <= 3
ORDER BY b.product_id;30 rows affected.
name | before | after
---------------+--------+------
Espresso Beans | 24.5 | 49
Green Tea | 8.75 | 17.5
Orange Juice | 3.2 | 6.4
(3 rows)
The backup still holds the old prices after the update.
Copying selected columns and rows
The new table gets exactly the columns of the SELECT, named after the column aliases:
CREATE TABLE eu_customers AS
SELECT customer_id, name, city, country
FROM customers
WHERE country IN ('Germany', 'France', 'Spain', 'Italy', 'Portugal', 'Austria', 'Ireland', 'Sweden');
SELECT COUNT(*) AS rows FROM eu_customers;
SELECT * FROM eu_customers ORDER BY country, name LIMIT 4;rows
----
10
(1 row)
customer_id | name | city | country
------------+--------------+--------+--------
19 | Nora Fischer | Vienna | Austria
2 | Bright Bytes | Paris | France
22 | Kai Moreau | Paris | France
1 | Corner Cafe | Berlin | Germany
(4 rows)
Saving a summary
The SELECT can be any query — joins, grouping, calculations:
CREATE TABLE sales_by_product AS
SELECT p.product_id, p.name, SUM(oi.quantity) AS units,
ROUND(SUM(oi.quantity * p.price), 2) AS revenue
FROM order_items AS oi
JOIN products AS p ON p.product_id = oi.product_id
GROUP BY p.product_id;
SELECT name, units, revenue FROM sales_by_product ORDER BY revenue DESC LIMIT 3;name | units | revenue
----------------------------+-------+--------
Mechanical Keyboard | 9 | 801
Cast Iron Pan | 19 | 722
Noise-Cancelling Headphones | 4 | 596
(3 rows)
Copying only the structure
A condition that is never true copies the columns but no rows — an empty table with the same shape:
CREATE TABLE orders_archive AS SELECT * FROM orders WHERE 0;
SELECT COUNT(*) AS rows FROM orders_archive;
SELECT name, type FROM pragma_table_info('orders_archive');rows
----
0
(1 row)
name | type
-------------+-----
order_id | INT
customer_id | INT
employee_id | INT
order_date | TEXT
shipped_date | TEXT
status | TEXT
(6 rows)
Note:
CREATE TABLE … AS SELECTcopies column names and (roughly) types, but not constraints: no primary key,NOT NULL,DEFAULT, foreign keys or indexes. For a proper copy, write theCREATE TABLEyourself and fill it withINSERT INTO … SELECT(next lesson).
Test yourself
Which statement copies a query result into a new table in SQLite?
- SELECT * INTO t FROM products
- CREATE TABLE t AS SELECT * FROM products
- COPY products TO t
- INSERT TABLE t SELECT * FROM products
What does CREATE TABLE t AS SELECT * FROM orders WHERE 0 create?
- A table with all orders
- An empty table with the same columns
- An error
- A view