Creating temporary tables from SELECT statements in SQL Server can be accomplished using SELECT INTO or CREATE TABLE followed by INSERT INTO syntax, with temporary tables starting with # for local scope or ## for global scope.
SQL Server Create Temp Table From Select
Understanding SQL Server Temporary Tables
What Are Temporary Tables in SQL Server?
Temporary tables are essential tools for intermediate data storage, complex query optimization, and batch processing operations that support critical business functions.
Temporary Table Fundamentals for Database Development:
| Temporary Table Type | Naming Convention | Scope and Visibility | Storage Location | Use Case Scenario |
|---|---|---|---|---|
| Local Temp Table | #TempTableName | Current session only | tempdb database | User-specific processing |
| Global Temp Table | ##TempTableName | All sessions | tempdb database | Cross-session data sharing |
| Table Variable | @TableVariable | Current batch | Memory/tempdb | Small dataset operations |
| Common Table Expression (CTE) | WITH CTEName | Single query | Virtual result set | Query readability enhancement |
Creating Temp Tables Using SELECT INTO
Basic SELECT INTO Syntax
Simplest Method for American Development Teams:
SELECT INTO represents the most straightforward approach for creating temporary tables from existing data:
SELECT INTO Pattern:
Case-1: Basic SELECT INTO temporary table pattern for applications
SELECT
CustomerID,
CustomerName,
City,
State,
ZipCode
INTO #USACustomers
FROM Customers
WHERE Country = 'USA';Case-2: SELECT INTO with join operations
SELECT
o.OrderID,
c.CustomerName,
o.OrderDate,
o.TotalAmount
INTO #USAOrderSummary
FROM Orders o
INNER JOIN Customers c ON o.CustomerID = c.CustomerID
WHERE c.Country = 'USA'
AND o.OrderDate >= '2025-01-01';Case-3: SELECT INTO with aggregations
SELECT
State,
City,
COUNT(*) AS CustomerCount,
SUM(TotalPurchases) AS TotalRevenue,
AVG(CustomerLifetimeValue) AS AverageLTV
INTO #StateCustomerMetrics
FROM CustomerAnalytics
WHERE Country = 'USA'
GROUP BY State, City;SELECT INTO Advanced Techniques
| SELECT INTO Technique | Business Application | Performance Benefit | Code Complexity |
|---|---|---|---|
| Filtered SELECT INTO | regional data segmentation | Reduced dataset size | Low complexity |
| Computed Columns | business calculation logic | Pre-calculated values | Medium complexity |
| Multiple Table Joins | denormalized reporting | Query performance improvement | Medium complexity |
| UNION Operations | data consolidation | Combined dataset creation | Medium complexity |
| Subquery Integration | complex filtering logic | Optimized data retrieval | High complexity |
Advanced SELECT INTO Examples:
-- SELECT INTO with UNION
SELECT
'East Coast' AS Region,
CustomerID,
OrderTotal
INTO #RegionalSales
FROM EastCoastOrders
WHERE OrderDate >= '2025-01-01'
UNION ALL
SELECT
'West Coast' AS Region,
CustomerID,
OrderTotal
FROM WestCoastOrders
WHERE OrderDate >= '2025-01-01';
-- SELECT INTO with window functions
SELECT
CustomerID,
OrderDate,
OrderAmount,
ROW_NUMBER() OVER (PARTITION BY CustomerID ORDER BY OrderDate) AS OrderSequence,
SUM(OrderAmount) OVER (PARTITION BY CustomerID ORDER BY OrderDate) AS RunningTotal
INTO #CustomerOrderHistory
FROM Orders
WHERE CustomerState IN ('CA', 'NY', 'TX', 'FL');
-- SELECT INTO with CASE statements
SELECT
CustomerID,
TotalPurchases,
CASE
WHEN TotalPurchases >= 10000 THEN 'Premium'
WHEN TotalPurchases >= 5000 THEN 'Gold'
WHEN TotalPurchases >= 1000 THEN 'Silver'
ELSE 'Bronze'
END AS CustomerTier
INTO #CustomerSegmentation
FROM CustomerMetrics
WHERE Country = 'USA';Conclusion:
Mastering SQL Server temporary table creation from SELECT statements extends far beyond understanding basic syntax—it requires comprehensive knowledge of performance optimization techniques, resource management strategies, and architectural best practices that define successful database development.
- Multiple Creation Approaches: Understanding both SELECT INTO and CREATE TABLE methodologies enables developers to choose the optimal approach based on specific requirements, with SELECT INTO providing rapid development for straightforward scenarios and explicit CREATE TABLE offering granular control for complex enterprise applications.
You may also like the following articles:
- SQL Server Temp Table Scope
- How to Retrieve Data from Temp Table in SQL Server
- SQL Server Import Excel File Into Temp Table
- How to Create Index on Temp Table in SQL Server
After working for more than 15 years in the Software field, especially in Microsoft technologies, I have decided to share my expert knowledge of SQL Server. Check out all the SQL Server and related database tutorials I have shared here. Most of the readers are from countries like the United States of America, the United Kingdom, New Zealand, Australia, Canada, etc. I am also a Microsoft MVP. Check out more here.