CTAS & Temporary Tables
Save a query result as a real table with CTAS / SELECT INTO, and use session-only #temp tables for multi-step work.
A view re-runs its query every time. If that query takes 2 minutes and 50 people open the dashboard, that is 100 minutes of work. Sometimes you want to save the result once as a real table. That is what CTAS and temporary tables do.
Table types — where does the data live and for how long?
| Type | Stores data? | Lives for | Created with |
|---|---|---|---|
| Permanent table | Yes, on disk | Until you DROP it | CREATE TABLE … |
| Temporary table | Yes (in tempdb in SQL Server) | Only your session, then vanishes | #temp / CREATE TEMPORARY TABLE |
| View | No — only the query | Until you DROP it | CREATE VIEW … |
Definition — CTAS
CTAS (Create Table As Select) creates a new physical table and fills it with the rows returned by a
SELECT— in one statement. The new table's columns and data types are copied from the query's result.
- New physical table — real rows on disk, just like any other table.
- One statement — no separate
CREATE TABLEcolumn list andINSERT. - A snapshot — it stores a copy of the data at that moment. It does not change when the source table changes later. To refresh it, drop and re-create it.
Syntax
sql
-- Standard / MySQL / PostgreSQL / SQLite
CREATE TABLE new_table AS
SELECT column_list
FROM source_table
WHERE condition;
-- SQL Server (T-SQL) — SELECT … INTO
SELECT column_list
INTO new_table
FROM source_table
WHERE condition;| Part | Meaning |
|---|---|
CREATE TABLE new_table AS | standard form: the table is created from the query |
INTO new_table | SQL Server form: written between SELECT list and FROM |
new_table | must not already exist (both forms fail if it does) |
Example — top batters snapshot (SQL Server)
sql
SELECT id, name, runs
INTO top_batters
FROM players
WHERE runs >= 700;
SELECT * FROM top_batters;| id | name | runs |
|---|---|---|
| 1 | Virat Kohli | 750 |
| 3 | Jos Buttler | 850 |
| 5 | David Warner | 700 |
The playground does not support SELECT … INTO, but it supports the standard CREATE TABLE top_batters AS SELECT …, which does the same job — try it below.
Example — snapshot vs view
sql
CREATE VIEW v_top AS SELECT id, name, runs FROM players WHERE runs >= 700;
CREATE TABLE top_copy AS SELECT id, name, runs FROM players WHERE runs >= 700;
UPDATE players SET runs = 750 WHERE name = 'MS Dhoni';
SELECT 'view' AS source, COUNT(*) AS row_count FROM v_top
UNION ALL
SELECT 'ctas table', COUNT(*) FROM top_copy;| source | row_count |
|---|---|
| view | 4 |
| ctas table | 3 |
The view sees Dhoni's new 750 runs; the CTAS table is a frozen copy and still has 3 rows.
CTAS vs CREATE TABLE + INSERT
| CREATE TABLE + INSERT | CTAS | |
|---|---|---|
| Steps | 1) define columns 2) insert rows | ONE statement |
| Column types | You choose them | Copied from the query |
| Constraints | You can add keys, defaults, checks… | Usually none — add them afterwards |
| Best for | Production tables with rules | Fast copies, snapshots, data marts |
Note: SQL Server's
SELECT … INTOcopies column names, data types, NULL-ability and theIDENTITYproperty, but not primary keys, indexes, foreign keys, defaults or check constraints.
CTAS use cases
| Use case | Example |
|---|---|
| Optimise performance | Pre-calculate a slow report once, then dashboards query the small table |
| Create a snapshot / backup | players_backup — preserve today's data before a risky UPDATE / DELETE |
| Physical data marts | mart_rcb — store a ready-made slice for one team or department |
Definition — temporary table
A temporary table is a real table that is visible only to the session (connection) that created it and is dropped automatically when that session ends. In SQL Server its name starts with
#and it is stored in the system databasetempdb.
Syntax — temporary tables
sql
-- SQL Server: SELECT … INTO a #temp table
SELECT id, name, runs
INTO #temp_big
FROM players
WHERE runs >= 500;
-- SQL Server: or create it first, then insert
CREATE TABLE #temp_big (id INT, name VARCHAR(50), runs INT);
INSERT INTO #temp_big SELECT id, name, runs FROM players WHERE runs >= 500;
-- MySQL / PostgreSQL
CREATE TEMPORARY TABLE temp_big AS
SELECT id, name, runs FROM players WHERE runs >= 500;| id | name | runs |
|---|---|---|
| 1 | Virat Kohli | 750 |
| 2 | Rohit Sharma | 500 |
| 3 | Jos Buttler | 850 |
| 5 | David Warner | 700 |
| Prefix (SQL Server) | Name | Visible to | Dropped when |
|---|---|---|---|
#temp | Local temp table | Your session only | Your session disconnects (or you DROP it) |
##temp | Global temp table | All sessions | The creating session ends and nobody is using it |
Temp tables are great for breaking a long job into steps: step 1 saves a filtered set into #step1, step 2 reads #step1 and saves into #step2, and so on. Unlike a CTE, each step's result is actually stored, can be indexed, and can be queried many times in the same session.
Common mistakes
- ❌ Running
SELECT … INTO top_batterstwice → error "There is already an object named 'top_batters'". ✅DROP TABLE IF EXISTS top_batters;first (SQL Server 2016+). - ❌ Expecting a CTAS table to update itself. ✅ it is a snapshot — re-create it, or use a view for live data.
- ❌ Expecting another user to see your
#temptable. ✅ local temp tables are private to your session. - ❌ Writing
CREATE TABLE … AS SELECTin SQL Server → syntax error. ✅ SQL Server usesSELECT … INTO.
SQL Server vs MySQL / PostgreSQL
| Task | SQL Server | MySQL / PostgreSQL |
|---|---|---|
| CTAS | SELECT … INTO new_table FROM … | CREATE TABLE new_table AS SELECT … |
| Temp table | #name (prefix) | CREATE TEMPORARY TABLE name |
Interview tip
Q: Temp table vs CTE? A CTE is just a named query for one statement — nothing is stored. A temp table physically stores rows for your whole session, can be indexed and reused by many statements. Use a temp table when an intermediate result is large or used several times.
Key points to remember
- CTAS creates a new physical table and fills it from a SELECT in one statement.
- SQL Server writes CTAS as SELECT … INTO new_table FROM …; MySQL / PostgreSQL use CREATE TABLE … AS SELECT.
- A CTAS table is a snapshot — it does not change when the source table changes; drop and re-create to refresh.
- CTAS copies column names and types but not primary keys, indexes, defaults or constraints.
- CTAS use cases: pre-calculated reports (performance), backups / snapshots, physical data marts.
- A SQL Server temp table (#name) lives in tempdb, is private to your session and is dropped automatically when you disconnect.
- Temp tables store rows (unlike CTEs), so they are ideal for breaking a long job into steps.
Try it yourself
CTAS and temp table in SQL Server (SELECT … INTO)
-- CTAS: create AND fill a permanent table in one step
DROP TABLE IF EXISTS top_batters;
SELECT id, name, runs
INTO top_batters
FROM players
WHERE runs >= 700;
-- Temporary table: only your session can see it
SELECT id, name, runs
INTO #temp_big
FROM players
WHERE runs >= 500;
SELECT * FROM #temp_big; -- Kohli 750, Rohit 500, Buttler 850, Warner 700
DROP TABLE #temp_big; -- optional: dropped automatically when you disconnectSQL Server-specific — run this in SQL Server (SSMS / Azure Data Studio).
Try it: same idea in the playground (CREATE TABLE … AS SELECT)
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Snapshot vs view after an UPDATE
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Practice: backup before a risky DELETE
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Revision Flashcards
Tap a card to flip it and see the answer.
