Request a demo

BigQuery · DDL statement

BigQuery CREATE TEMP TABLE

CREATE TEMP TABLE stores intermediate results in a table that exists only for the current multi-statement query or session, without a dataset. Use it to compute an expensive step once and reuse it, instead of a CTE that BigQuery re-runs at every reference.

GoogleSQL for BigQuery · Checked against the official reference in November 2026

CREATE TEMP TABLE name (...) or CREATE TEMP TABLE name AS SELECT ... has the same syntax as CREATE TABLE, except that the name has no project or dataset: BigQuery places the table in a hidden _script... dataset (or a session dataset) for you. It is only allowed in a multi-statement query, a stored procedure or a session. As a single statement it fails with "Use of CREATE TEMPORARY TABLE requires a script or session".

Inside a script you refer to the table by its bare name, or explicitly as _SESSION.name. An unqualified name means the temp table if one exists, otherwise a table in the default dataset; _SESSION.name always means the temp table and fails if it does not exist. You can insert into it, update it, join it and drop it with DROP TABLE name before the script ends. Temp tables cannot be shared, referenced from views or persistent functions, or created with a project or dataset prefix.

Lifetime depends on where you create it. In a multi-statement query the table lives until the script ends and is kept for up to 24 hours afterwards (you can see it from the job's destination table in the console). In a session it lives until the session ends; sessions end after 24 hours of inactivity or 7 days after creation. Storage of temp tables is billed like any other table, and the bytes show up in INFORMATION_SCHEMA.TABLE_STORAGE under the generated _script dataset names.

The point for cost is materialization. A non-recursive CTE is re-executed at each reference, so a CTE over a large table used three times is scanned three times. Writing it once to a temp table bills that scan once, and later statements read only the small temp table.

Syntax

CREATE [OR REPLACE] TEMP[ORARY] TABLE [IF NOT EXISTS] table_name
[(column_definitions)]
[PARTITION BY ...] [CLUSTER BY ...]
[OPTIONS (...)]
[AS query_statement];

SELECT ... FROM table_name;          -- or _SESSION.table_name

DROP TABLE table_name;

Arguments and return value

ArgumentDescription
table_nameBare table name without project or dataset. _SESSION.table_name is also accepted.
AS query_statementFills the table from a query, billed like any query on demand.
column definitions, PARTITION BY, CLUSTER BY, OPTIONSSame as in CREATE TABLE; clustering a large temp table that later statements filter on can make them cheaper.

Returns: A temporary table visible to the rest of the multi-statement query, procedure or session.

Examples

Compute an expensive step once and reuse it

The scan of events happens once. Both following statements read only the small temp table.

CREATE TEMP TABLE active_users AS
SELECT user_id, MIN(event_ts) AS first_seen, COUNT(*) AS events
FROM `project.dataset.events`
WHERE DATE(event_ts) BETWEEN '2026-11-01' AND '2026-11-22'
GROUP BY user_id;

SELECT COUNT(*) AS active_users FROM active_users;

SELECT DATE(first_seen) AS day, COUNT(*) AS new_users
FROM active_users
GROUP BY day
ORDER BY day;

Stage, fix and merge in one script

A temp table holds the deduplicated batch; the MERGE reads it instead of re-running the deduplication.

CREATE TEMP TABLE batch AS
SELECT * FROM `project.dataset.customers_staging`
WHERE load_date = '2026-11-22'
QUALIFY ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY updated_at DESC) = 1;

UPDATE batch SET email = LOWER(email) WHERE TRUE;

MERGE `project.dataset.customers` AS t
USING batch AS s
ON t.customer_id = s.customer_id
WHEN MATCHED THEN UPDATE SET email = s.email, updated_at = s.updated_at
WHEN NOT MATCHED THEN INSERT ROW;

DROP TABLE batch;

Disambiguate with _SESSION

With a default dataset that also has a table called daily, _SESSION.daily always means the temp table.

CREATE TEMP TABLE daily AS
SELECT DATE(created_at) AS day, SUM(amount) AS revenue
FROM `project.dataset.orders`
WHERE DATE(created_at) >= '2026-11-01'
GROUP BY day;

SELECT * FROM _SESSION.daily ORDER BY day;

Common mistakes

  • Running CREATE TEMP TABLE as a single statement. It fails with "Use of CREATE TEMPORARY TABLE requires a script or session"; add the statements that use it to the same script.
  • Prefixing the name with a dataset (CREATE TEMP TABLE mydataset.t). Temp tables take a bare name; _SESSION is the only qualifier allowed.
  • Referencing a temp table from a view or persistent UDF. Only the script or session that created it can see it.
  • Assuming temp tables are free. Their storage is billed until they are deleted, up to 24 hours after a script ends; drop large ones at the end of the script.

Cost and performance

  • Materializing a step that later statements reuse bills its scan once; the same logic as a CTE referenced three times is billed three times on demand.
  • Dry runs stop at a CREATE TEMP TABLE statement, so the estimate of a script with temp tables is incomplete. Check the bytes of each child job in INFORMATION_SCHEMA.JOBS after a first run.

FAQ

How long does a temp table last in BigQuery?

For the rest of the multi-statement query (kept up to 24 hours after it ends), or until the session ends when created in a session. Drop it earlier with DROP TABLE name.

What is the difference between a CTE and a temp table in BigQuery?

A CTE is part of one statement and is re-run at every reference. A temp table stores the result once for the whole script or session, which saves bytes when the result is reused.

Why does CREATE TEMP TABLE fail with 'Use of CREATE TEMPORARY TABLE requires a script or session'?

It was run as a lone statement. Add the queries that use the table to the same request, or create a session first.

More DDL statements

All BigQuery DDL statements · All statements and clauses · BigQuery error messages

Observability for every BigQuery table, in about 15 minutes

Masthead detects freshness, volume and schema anomalies, monitors pipeline errors and maps column-level lineage across all your projects, from logs and metadata only.