How to Create a Temporary Table in SQL

What you’ll build or solve

You’ll create a table that holds intermediate data without affecting permanent tables.

When this approach works best

Creating a temporary table works best when you:

  • Break a complex query into smaller, easier steps
  • Store intermediate results during reporting
  • Isolate filtered data before final processing

For example, you might store active users for a report, cache aggregated sales results, or stage transformed data before loading it elsewhere.

This is a bad idea if you need the data long term. Use a regular table if the data must persist across sessions.

Prerequisites

  • Access to the database
  • Permission to create tables
  • Basic SQL knowledge

Step-by-step instructions

Step 1: Create the temporary table

Use the correct syntax for your database system.

MySQL or PostgreSQL

Create a temporary table with defined columns:

CREATETEMPORARYTABLE temp_users (
  idINT,
  nameVARCHAR(100),
  emailVARCHAR(100)
);

The table exists only in your current session.

You can also create it from a query result:

CREATETEMPORARYTABLE active_usersAS
SELECT id, name, email
FROM users
WHERE active=1;

This creates the table and fills it with query results in one step.

SQL Server

Create a local temporary table:

CREATETABLE #temp_users (
  idINT,
  name NVARCHAR(100),
  email NVARCHAR(100)
);

Create a global temporary table:

CREATETABLE ##temp_users (
  idINT,
  name NVARCHAR(100),
  email NVARCHAR(100)
);
  • # creates a table visible only in your session.
  • ## creates a table visible to all sessions until it is dropped.

What to look for

  • Temporary tables are session-scoped in most systems
  • They can be created with column definitions or from a SELECT query
  • They require the same permissions as regular table creation
  • They disappear automatically when the session ends in most databases
  • You can drop them manually with DROP TABLE temp_users; or DROP TABLE #temp_users;
  • Avoid using the same name as a permanent table to prevent confusion

Examples you can copy

Example 1: Create a simple session table

You want to store user IDs during a calculation:

CREATETEMPORARYTABLE temp_ids (
  idINT
);

This table exists only for your current connection.

Example 2: Create from filtered results

You need only active users for further processing:

CREATETEMPORARYTABLE active_usersAS
SELECT id, name
FROM users
WHERE active=1;

This creates and fills the temporary table in one command.

Example 3: Use a global temporary table in SQL Server

You need data accessible across multiple sessions:

CREATETABLE ##shared_data (
  idINT,
value NVARCHAR(100)
);

The table remains available until all sessions release it.

Common mistakes and how to fix them

Mistake 1: Using the wrong syntax for your database

What you might do:

CREATE TEMPTABLE temp_users (...);

Why it fails: Some databases require the full TEMPORARY keyword or use # prefixes instead.

Correct approach: Use the syntax for your system, such as:

CREATETEMPORARYTABLE temp_users (...);

Or in SQL Server:

CREATETABLE #temp_users (...);

Mistake 2: Expecting the table to persist

What you might do: Create a temporary table and try to access it in a new session.

Why it fails: Temporary tables are usually removed when the session ends.

Correct approach: Use a regular table if you need long-term storage.

Mistake 3: Creating naming conflicts

What you might do: Create a temporary table with the same name as a permanent table.

Why it causes problems: Queries may reference the temporary table instead of the permanent one during the session.

Correct approach: Use clear prefixes such as temp_ or # to distinguish them.

Troubleshooting

  • If you see “permission denied,” confirm you have CREATE TABLE privileges.
  • If SQL Server shows “invalid object name,” check that you used # or ## correctly.
  • If the table disappears unexpectedly, verify whether your session ended.
  • If your query references the wrong table, check for a temporary table with the same name.

Quick recap

  • Use CREATE TEMPORARY TABLE in MySQL and PostgreSQL
  • Use # or ## in SQL Server
  • Create from column definitions or from a SELECT query
  • Temporary tables usually disappear after the session ends
  • Avoid naming conflicts with permanent tables