Temporary Table, Table Variable, and Common Table Expression (CTE) These are techniques in SQL to store intermediate results for further processing. Each has distinct characteristics, advantages, and limitations.
Before starting each we explain simple word - Session and Batch .
A session represents the connection between a client application and the SQL Server. It begins when the client connects to the server and ends when the client disconnects
Session ID
(SPID in SQL Server)Example
Example
-- Batch 1
DECLARE @Variable1 INT = 10;
SELECT @Variable1;
GO -- Marks the end of Batch 1
-- Batch 2
-- This batch cannot access @Variable1 from Batch 1
SELECT @Variable1; -- This will result in an error
A temporary table is a physical table stored in the tempdb database. It is useful for storing intermediate results and can be indexed.
Syntax
CREATE TABLE #TempTable (EmployeeID INT, Salary INT);
INSERT INTO #TempTable VALUES (1, 50000), (2, 60000);
SELECT * FROM #TempTable;
Real-World Example
Suppose you are generating a sales report and need to process large transactional data.
CREATE TABLE #SalesSummary (Region VARCHAR(50), TotalSales DECIMAL(10, 2));
INSERT INTO #SalesSummary
SELECT Region, SUM(SalesAmount)
FROM Sales
GROUP BY Region;
SELECT * FROM #SalesSummary
WHERE TotalSales > 100000;
A table variable is an in-memory data structure. It is declared using the DECLARE statement and behaves like a variable.
Syntax
DECLARE @TableVariable TABLE (EmployeeID INT, Salary INT);
INSERT INTO @TableVariable VALUES (1, 50000), (2, 60000);
SELECT * FROM @TableVariable;
Real-World Example
For a stored procedure that needs a temporary dataset for simple calculations.
DECLARE @SalesSummary TABLE (Region VARCHAR(50), TotalSales DECIMAL(10, 2));
INSERT INTO @SalesSummary
SELECT Region, SUM(SalesAmount)
FROM Sales
GROUP BY Region;
SELECT * FROM @SalesSummary WHERE TotalSales > 100000;
A CTE is a temporary, named result set that is defined within the execution scope of a single SELECT, INSERT, UPDATE, or DELETE statement.
Syntax
WITH CTE_SalesSummary AS (
SELECT Region, SUM(SalesAmount) AS TotalSales
FROM Sales
GROUP BY Region
)
SELECT * FROM CTE_SalesSummary WHERE TotalSales > 100000;
Real-World Example
Finding hierarchical data, such as an employee reporting structure.
WITH EmployeeHierarchy AS (
SELECT EmployeeID, ManagerID, 1 AS Level
FROM Employees
WHERE ManagerID IS NULL
UNION ALL
SELECT e.EmployeeID, e.ManagerID, eh.Level + 1
FROM Employees e
INNER JOIN EmployeeHierarchy eh ON e.ManagerID = eh.EmployeeID
)
SELECT * FROM EmployeeHierarchy;
Feature | Temporary Table | Table Variable | CTE |
---|---|---|---|
Storage | Stored in tempdb . |
Stored in memory. | No storage; in-memory query. |
Scope | Session or batch. | Batch or procedure. | Single statement. |
Indexing | Can have indexes. | Limited indexing. | No indexing. |
Constraints | Supports constraints. | Limited constraint support. | No constraints. |
Performance | Suitable for large datasets. | Suitable for small datasets. | Suitable for query simplification. |
Recursion | Not supported. | Not supported. | Supported. |