Pragmatic Works Nerd News

SQL Temp Tables

Written by Austin Libal | Aug 12, 2026

In this Pragmatic Works video, trainer Austin Libal dives into the use of temporary tables in SQL, explaining how these tools can drastically improve query performance. Temporary tables, also known as temp tables, are designed to store intermediate results during complex queries, allowing for better data management and optimization. Austin outlines how to create, use, and drop temporary tables in SQL, and the best practices for ensuring their effective application.

 

What are Temporary Tables?

A temporary table is a table created temporarily within a SQL database, used only during a specific session or transaction. Once the session or transaction ends, the temporary table is automatically dropped. These tables are particularly useful for performing CRUD operations (Create, Read, Update, Delete) and handling complex queries that require intermediate steps or the storage of specific results.

Temporary tables offer several benefits:

  • Storage for intermediate results during long or complex queries.
  • Data-specific to sessions or users, like user preferences or search parameters.
  • Improved query performance, as temp tables allow only relevant data to be processed.

Types of Temporary Tables

  • Local Temp Tables: Available only within the session that created them, denoted by a single `#` symbol.
  • Global Temp Tables: Accessible across all sessions, and dropped when the session that created them ends. They are denoted by `##`.

Best Practices for Using Temporary Tables

Austin provides a list of best practices to ensure that temporary tables are used effectively:

  1. Choose appropriate column data types and sizes: Avoid excessive memory usage by selecting the correct data types for each column.
  2. Proper indexing: Indexing temp tables can significantly boost query performance by reducing search time.
  3. Drop temp tables when no longer needed: It’s crucial to remove temporary tables to free up memory and prevent unnecessary load on the database.

These best practices are essential to ensure that temporary tables enhance rather than hinder SQL query performance.

Temporary Tables vs Subqueries and CTEs

Temporary tables are often compared to other techniques in SQL, such as subqueries and Common Table Expressions (CTEs). Austin explains that while all three can store intermediate results, they differ in their scope and usage:

  • Subqueries: These are similar to temp tables but are only available within the query they are part of. They are not stored as physical objects in the database.
  • CTEs: Like subqueries, CTEs store intermediate results but are only available within a single query and don’t persist in the database.

Temporary tables, on the other hand, are physical objects within the database, providing more flexibility and being available for longer durations.

Creating and Using Temporary Tables in SQL

To demonstrate the basics, Austin walks through a series of SQL examples using SQL Server Management Studio (SSMS). He starts with creating a basic local temp table, inserting data into it, and querying the data.

Example 1: Creating a Local Temp Table

                CREATE TABLE #Names (
                    BusinessEntityID INT,
                    FirstName VARCHAR(50),
                    LastName VARCHAR(50)
                );
            

Example 2: Inserting Data into the Temp Table

                INSERT INTO #Names (BusinessEntityID, FirstName, LastName)
                VALUES (1, 'John', 'Smith');
            

Example 3: Dropping the Temp Table

                DROP TABLE #Names;
            

After inserting some hard-coded data, Austin also demonstrates how temp tables can be populated using data from other queries. For example, you can select specific data from another table and store it in a temporary table to reduce processing load.

Advanced Use: Populating Temp Tables from Queries

Austin shows how to create a temp table from a query’s results. He uses a common sales query and selects products that have sold more than five units. The query results are then stored into a temporary table for further analysis.

Example:

                SELECT SalesOrderID, ProductID, OrderQty
                INTO #OrdersOverFive
                FROM Sales.SalesOrderDetail
                WHERE OrderQty > 5;
            

This technique helps in reducing the number of records processed during the query, as the data is filtered and stored in the temporary table before joining with other data sources.

Joining Temp Tables with Other Tables

Austin further illustrates how to join a temporary table with other database tables to extract more targeted data. In his example, he joins a temporary table (#OrdersOverFive) with a product table to retrieve products with an order quantity greater than five. This reduces the workload on the server and helps to gather more specific results quickly.

                SELECT P.ProductID, P.ProductName, O.OrderQty
                FROM Production.Product P
                LEFT JOIN #OrdersOverFive O ON P.ProductID = O.ProductID;
            

Conclusion

Temporary tables are a powerful tool in SQL that can drastically improve query performance, particularly when dealing with large datasets or complex queries. By following the best practices outlined in the video, SQL developers can harness the full potential of temp tables to optimize their workflows. Austin’s demonstrations in SQL Server Management Studio offer a practical approach for implementing temporary tables, helping developers make the most of this powerful SQL feature.

Don't forget to check out the Pragmatic Works' on-demand learning platform for more insightful content and training sessions on SQL and other Microsoft applications. Be sure to subscribe to the Pragmatic Works YouTube channel to stay up-to-date on the latest tips and tricks.