What is a Temporary Table and its need in SQL?

SQL provides a good feature to store data or table temporarily in memory. Temporary Table means to store the complete or sub-part of a table in different table or database. This table store this information temporarily or a certain time of period.

► Play

Every DB Developer uses temporary tables. Temporary tables are very useful when you have a large number of rows in a table and you need to retrieve some records from the table, again and again, you need a hell of a time for that and also write a query again and again. It is very headache for a developer. You simply create a temp table for that data and just fetch that table rather than a big table. Temporary tables improve your DB performance and maintainability.

How to Create a Temporary Table in SQL

SQL provide a query for creating a temporary table in database, the syntax is given below       

► Play

                SELECT name of columns

                INTO #temporary table name

                FROM name of table

                Where conditions [ optional ]

SELECT clause: In the select statement you can write the name of columns those you want to display in-store in a temporary table.

INTO clause: In into clause write the temporary table name.

FROM Clause: In into clause write the table name that data you want to store in a temporary table.

WHERE Clause: In where clause writes the condition it’s optional.

Example of a Temporary Table Query

How to create a Temp table, let’s spouse a products table exists in my database, then for temp table query is given below

► Play

SELECT product_name, list_price

INTO #trek_products — temporary table name

FROM products

WHERE brand_id = 9 s;

How to create Temp Table in SQL Server

One way to create a temp table in SQL is, using create a table just like a simple table. Example of temp table is given below using create table clause.

► Play

CREATE TABLE #temp Table Name (

Attribute1 dataType,

Attribute2 dataType,

Attribute3  dataType,

) ;

How to create Temp Table in SQL Developer

How can we make the temporary table data is transient? Firstly, the information is handiest visible within the session which inserts it; any other session will see an empty table. Secondly, the statistics can persist for either a transaction or the session, depending on the ON COMMIT clause; the default is ON COMMIT DELETE ROWS.

► Play

                CREATE GLOBAL TEMPORARY TABLE temp_Table AS

                SELECT * FROM real_Table ;

How to create Temp Table in SQL Procedure

Before creating a temp table we can declare a temporary table like this

► Play

The temporary table will be declared as below:

Declare @temp table(

                staffid varchar(10),

                attstatus char(1)


How to create Temp Table in SQL with Example

Below is the complete example of a Temporary table, with parameters or attributes name and data types,

► Play

CREATE table # Color (

Colour varchar (10) PRIMARY key





Send us a feedback0/500

Do you like this article?