Sometimes you want to keep the result of a query: a list of scholarship winners, a snapshot of this month’s sales, or a cleaned-up copy of messy data to work on. SQL lets you save a query result as a new table, add rows to a table straight from a query, and create temporary tables that disappear by themselves when you are done.
In this lesson you will learn
- How
CREATE TABLE … AS SELECTsaves a result - How
INSERT INTO … SELECTcopies rows - What a copied table loses: keys and constraints
- How temporary tables work and when to use them
- How these compare with views and CTEs
You should know CREATE TABLE and views.
Saving a Result as a Table
-- Save the CSE high achievers as a new table
CREATE TABLE toppers AS
SELECT id, name, cgpa
FROM students
WHERE dept = 'CSE' AND cgpa >= 3.6;
SELECT * FROM toppers ORDER BY cgpa DESC;
-- Add the BBA high achievers later from another query
INSERT INTO toppers (id, name, cgpa)
SELECT id, name, cgpa FROM students WHERE dept = 'BBA' AND cgpa >= 3.5;
SELECT * FROM toppers ORDER BY cgpa DESC;
Output
+----+--------+------+
| id | name | cgpa |
+----+--------+------+
| 3 | Tania | 3.95 |
| 1 | Nusrat | 3.82 |
| 5 | Mitu | 3.67 |
+----+--------+------+
(3 rows)
+----+--------+------+
| id | name | cgpa |
+----+--------+------+
| 3 | Tania | 3.95 |
| 1 | Nusrat | 3.82 |
| 5 | Mitu | 3.67 |
| 7 | Sabbir | 3.55 |
+----+--------+------+
(4 rows)
CREATE TABLE toppers AS SELECT …makes a new table with the query’s columns and fills it with the matching rows. This is often called CTAS.INSERT INTO toppers (…) SELECT …adds rows from another query. The column lists must line up in number and order.- The new table is a snapshot. If a student’s CGPA changes later,
toppersdoes not change.
What a Copy Does Not Keep
-- CREATE TABLE AS copies columns, not keys or constraints
CREATE TABLE students_copy AS SELECT * FROM students;
SELECT name, type, pk FROM pragma_table_info('students') WHERE name IN ('id', 'name');
SELECT name, type, pk FROM pragma_table_info('students_copy') WHERE name IN ('id', 'name');
-- A temporary table vanishes when the connection closes
CREATE TEMP TABLE monthly AS
SELECT substr(order_date, 1, 7) AS month, SUM(amount) AS revenue
FROM orders
WHERE status = 'delivered'
GROUP BY month;
SELECT * FROM monthly ORDER BY month;
SELECT name FROM sqlite_temp_master WHERE type = 'table';
Output
+------+---------+----+
| name | type | pk |
+------+---------+----+
| id | INTEGER | 1 |
| name | TEXT | 0 |
+------+---------+----+
(2 rows)
+------+------+----+
| name | type | pk |
+------+------+----+
| id | INT | 0 |
| name | TEXT | 0 |
+------+------+----+
(2 rows)
+---------+---------+
| month | revenue |
+---------+---------+
| 2026-01 | 1650 |
| 2026-02 | 2300 |
| 2026-03 | 1800 |
+---------+---------+
(3 rows)
+---------+
| name |
+---------+
| monthly |
+---------+
(1 row)
The first two results compare the original table with its copy. In the copy, id is no longer a primary key (pk is 0), and SQLite even recorded its type as INT. A CTAS copy keeps only column names and values. Primary keys, NOT NULL, CHECK, defaults, foreign keys and indexes are all lost. If the copy will be used for real work, create the table properly first and then fill it with INSERT INTO … SELECT.
Temporary Tables
A table made with CREATE TEMP TABLE (or CREATE TEMPORARY TABLE) is private to your connection and is deleted automatically when the connection closes. In the example, monthly holds monthly revenue while you work, and it is listed in sqlite_temp_master, not in the main database.
Temporary tables are useful when:
- A long report runs several queries on the same intermediate result.
- You want to clean or test data without touching the real tables.
- A slow subquery is reused many times; saving its result once can speed things up.
| Tool | Stores rows? | Lives for | Best for |
|---|---|---|---|
CTE (WITH) | No | One query | Breaking one query into steps |
| View | No, re-runs the query | Until dropped | A saved query that always shows current data |
| Temporary table | Yes | Your connection | Reusing a result in several queries |
| CTAS table | Yes | Until dropped | Snapshots, archives, backups before risky changes |
The syntax is similar elsewhere. MySQL and PostgreSQL support CREATE TABLE … AS SELECT and CREATE TEMPORARY TABLE. SQL Server uses SELECT … INTO new_table FROM …, and its temporary tables have names starting with #.
Common Mistakes
- Expecting a CTAS copy to stay up to date. It is a snapshot; use a view for live data.
- Expecting keys and constraints to be copied. Recreate them, or define the table first.
- Mismatched column order in
INSERT INTO … SELECT. Columns are matched by position, not by name. - Relying on a temp table after reconnecting. It is gone when the connection ends.
Practice Exercises
Exercise 1. Before deleting cancelled orders, make a backup table of them.
Show answer
CREATE TABLE cancelled_orders_backup AS
SELECT * FROM orders WHERE status = 'cancelled';
Exercise 2. Create a temporary table of average marks per course, then list courses above 80.
Show answer
CREATE TEMP TABLE course_avg AS
SELECT course_id, AVG(marks) AS avg_marks
FROM enrollments
GROUP BY course_id;
SELECT * FROM course_avg WHERE avg_marks > 80;
The averages are 91.5 for course 101, 86 for 102, 72.5 for 201 and 78.5 for 301, so only 101 and 102 are listed.
Frequently Asked Questions
What does CREATE TABLE AS SELECT do?
It creates a new table using the columns of a SELECT query and fills it with the rows that query returns. The new table is a snapshot and does not copy keys, constraints or indexes from the source.
When should I use a temporary table instead of a CTE?
Use a CTE to organise a single query. Use a temporary table when you need the same intermediate result in several separate queries during one session, or when saving it once makes repeated work faster.
Summary
CREATE TABLE … AS SELECTsaves a query result as a new table.INSERT INTO … SELECTadds query results to an existing table.- Copies lose keys, constraints and indexes, and do not update themselves.
- Temporary tables are private and disappear when the connection closes.
