Skip to main content

Temporary Tables

Temporary tables are session-scoped tables for staging, transformation, and repeat-query workflows.

Temporary Table Characteristics​

CharacteristicBehavior
ScopeVisible only in current session
LifetimeDropped automatically at session end
Use caseMulti-step data processing

Core Workflow​

CREATE TEMPORARY TABLE tmp_paid_orders AS
SELECT order_id, customer_id, total_amount
FROM orders
WHERE status = 'paid';

SELECT customer_id, SUM(total_amount) AS total_spend
FROM tmp_paid_orders
GROUP BY customer_id;

DROP TEMPORARY TABLE IF EXISTS tmp_paid_orders;

When to Choose Temporary Tables​

ScenarioCTETemporary table
One-pass transformationBetterAcceptable
Multi-step reuse in sessionLimitedBetter
Debugging intermediate statesLimitedBetter

Common Pitfalls​

PitfallConsequencePrevention
Forgetting cleanup in long sessionsSession memory growthDrop temp tables explicitly when done
Naming collisions in scriptsScript failuresUse prefixed names like tmp_report_*
Assuming visibility across sessionsUnexpected missing table errorsRecreate per connection/session

Quick Reference​

CREATE TEMPORARY TABLE tmp_x AS SELECT ...;
INSERT INTO tmp_x SELECT ...;
SELECT * FROM tmp_x;
DROP TEMPORARY TABLE IF EXISTS tmp_x;

What's Next​