Copying a Table
CREATE TABLE new_table AS SELECT ... makes a new table and fills it with a query's result, in one statement. It's the quickest way to take a snapshot before a risky change, copy a subset to experiment on, or save a report's rows. SQL Server and Access spell it SELECT ... INTO new_table. This lesson covers both, and the detail that matters most: the copy gets the columns and the data, but not the primary key, foreign keys, NOT NULL rules or defaults.
In One Minute
CREATE TABLE t AS SELECT ...createstwith the query's columns and rows.SELECT ... INTO t FROM ...does the same in SQL Server and Access (PostgreSQL accepts it too).WITH NO DATAcopies the column structure only, with no rows.- The copy has no constraints: no primary key, foreign keys,
NOT NULL,CHECKor defaults. CREATE TABLE t (LIKE source INCLUDING ALL)copies the full design, rules included, but no rows.
CREATE TABLE AS
CREATE TABLE new_table AS
SELECT columns FROM source_table [WHERE ...];A copy of the card payments to experiment on:
CREATE TABLE card_payments AS
SELECT payment_id, student_id, amount_usd, paid_on
FROM payments
WHERE method = 'card';
SELECT * FROM card_payments ORDER BY payment_id;SELECT 4
payment_id | student_id | amount_usd | paid_on
------------+------------+------------+------------
1 | 101 | 450.00 | 2025-08-28
2 | 101 | 250.00 | 2025-10-30
4 | 101 | 550.00 | 2025-12-29
6 | 103 | 225.00 | 2025-11-14
(4 rows)The new table's columns take their names and types from the SELECT, and its rows are a snapshot: payments added later don't appear in the copy. Use aliases to name calculated columns, as in paid_on - due_on AS days_late, or the column gets a generic name like ?column?.
SELECT INTO
SELECT ... INTO new_table FROM ... is the same idea in SQL Server and Access syntax. PostgreSQL accepts it as well:
SELECT payment_id, amount_usd
INTO check_payments
FROM payments
WHERE method = 'check';
SELECT * FROM check_payments ORDER BY payment_id;SELECT 2
payment_id | amount_usd
------------+------------
3 | 250.00
7 | 225.00
(2 rows)In PostgreSQL, prefer CREATE TABLE AS: it reads more clearly, and inside PL/pgSQL functions SELECT INTO means something different (storing a value in a variable). MySQL has no SELECT INTO for tables; use CREATE TABLE ... AS SELECT (or CREATE TABLE ... SELECT). Tabs showing each database's version are coming to this course.
What Is and Isn't Copied
The copy gets columns, types and rows, and nothing else. Compare the original payments table with a full copy:
CREATE TABLE payments_copy AS
SELECT * FROM payments;
SELECT count(*) AS constraints_on_copy
FROM pg_constraint
WHERE conrelid = 'payments_copy'::regclass;
SELECT column_name, is_nullable, column_default
FROM information_schema.columns
WHERE table_name = 'payments_copy'
ORDER BY ordinal_position;SELECT 7
constraints_on_copy
---------------------
0
(1 row)
column_name | is_nullable | column_default
-------------+-------------+----------------
payment_id | YES | NULL
student_id | YES | NULL
course_id | YES | NULL
amount_usd | YES | NULL
due_on | YES | NULL
paid_on | YES | NULL
method | YES | NULL
(7 rows)Zero constraints, every column nullable, no defaults. The original has a primary key, three foreign keys, two checks and NOT NULL on six columns. The copy would accept a duplicate payment_id, a negative amount or a student who doesn't exist, and its payment_id no longer numbers itself.
Copied by CREATE TABLE AS | Not copied |
|---|---|
| Column names and types | Primary key and UNIQUE |
| The rows | Foreign keys and CHECK |
NOT NULL and defaults | |
| Identity numbering and indexes |
Copying the Design: LIKE and WITH NO DATA
CREATE TABLE ... (LIKE source INCLUDING ALL) copies the full design, rules included, with no rows:
CREATE TABLE payments_next_year (LIKE payments INCLUDING ALL);
SELECT count(*) AS constraints_on_copy
FROM pg_constraint
WHERE conrelid = 'payments_next_year'::regclass;CREATE TABLE
constraints_on_copy
---------------------
9
(1 row)Nine constraints: the primary key, the checks and the NOT NULL rules (foreign keys are the one thing LIKE never copies). Then fill it with INSERT ... SELECT as needed. CREATE TABLE t AS SELECT ... WITH NO DATA is the in-between option: the columns, no rows, and still no rules. LIKE is PostgreSQL syntax; MySQL has CREATE TABLE t LIKE source.
Common Mistakes
Expecting the copy to have keys and rules. It doesn't. Add them, or use LIKE ... INCLUDING ALL.
Unnamed calculated columns. paid_on - due_on becomes ?column?. Add an alias.
Thinking the copy stays in sync. It's a snapshot. Use a view for live data.
SELECT INTO inside PostgreSQL functions. There it fills variables, not tables. Use CREATE TABLE AS.
Interview Corner
How do you create a new table from a query?
CREATE TABLE new_table AS SELECT ... in PostgreSQL, MySQL and Oracle; SELECT ... INTO new_table FROM ... in SQL Server and Access.
Does CREATE TABLE AS copy constraints and indexes?
No. It copies only column names, types and data. Primary keys, foreign keys, NOT NULL, checks, defaults and indexes must be added separately.
How do you copy only a table's structure?
CREATE TABLE t (LIKE source INCLUDING ALL) in PostgreSQL copies the design with its rules and no rows. CREATE TABLE t AS SELECT ... WITH NO DATA copies just the columns.
Practice
Challenge: Create a table student_totals holding each paying student's student_id, number of payments (payments) and total paid (total_usd), using one statement. Then show it sorted by student_id.
Show solution
CREATE TABLE student_totals AS
SELECT student_id, count(*) AS payments, sum(amount_usd) AS total_usd
FROM payments
GROUP BY student_id;
SELECT * FROM student_totals ORDER BY student_id;SELECT 2
student_id | payments | total_usd
------------+----------+-----------
101 | 5 | 1700.00
103 | 2 | 450.00
(2 rows)The aliases became the column names. Advitha has no payments, so she has no row: the table holds whatever the query returned, at the moment it ran.
Key Takeaways
| You want to | Write |
|---|---|
| Copy a query into a new table | CREATE TABLE card_payments AS SELECT ... WHERE method = 'card' |
| SQL Server style | SELECT ... INTO card_payments FROM payments WHERE ... |
| Copy the design only | CREATE TABLE t (LIKE payments INCLUDING ALL) |
| A safety snapshot | CREATE TABLE payments_backup AS SELECT * FROM payments |
Was this tutorial helpful?