In relational databases such as Microsoft SQL Server, what is a temporary table temp table, and when is it typically used?

Difficulty: Easy

Correct Answer: It is a table created in a temporary database, often with a name starting with # or ##, used to store intermediate results for the duration of a session or transaction

Explanation:


Introduction / Context:
Temporary tables are a common feature in relational database systems and are particularly visible in Microsoft SQL Server, where they are stored in the tempdb system database. Developers and database administrators use temporary tables to store intermediate results during complex queries, reporting, or batch processing. Understanding what temporary tables are and when to use them is important for writing efficient and maintainable Structured Query Language code. This question asks you to define a temporary table and describe its typical use case.


Given Data / Assumptions:

  • We are considering Microsoft SQL Server style temporary tables, which use naming conventions with # and ##.
  • Temporary tables are created and managed with Structured Query Language statements such as CREATE TABLE or SELECT INTO.
  • They persist only for a limited scope, such as a session or connection, or in the case of global temporary tables, until all referencing sessions end.
  • They are useful for storing intermediate or staging data, not long term application data.


Concept / Approach:
In Microsoft SQL Server, a local temporary table has a name beginning with a single number sign and is visible only to the current session. It is stored in the tempdb database and is automatically dropped when the session ends or when the table is explicitly dropped. A global temporary table uses two number signs, is visible to multiple sessions, and is dropped after the last session referencing it disconnects. Developers often create temporary tables to hold the results of complex joins, aggregations, or transformations so that subsequent queries can reuse these results without recomputing them. This improves clarity and, in some cases, performance.


Step-by-Step Solution:
Step 1: Identify that a temp table is defined for short term use and is stored in a temporary database such as tempdb. Step 2: Recall that in SQL Server, temp tables typically have names starting with # for local and ## for global temporary tables. Step 3: Recognise that temp tables are automatically dropped when their scope ends, for example when a session or stored procedure completes, unless explicitly dropped earlier. Step 4: Evaluate option a, which describes temp tables as tables created in a temporary database with # or ## names, used to store intermediate results during a session or transaction. Step 5: Reject options b, c, and d, which mischaracterise temp tables as permanent, self encrypting, or unqueryable memory structures.


Verification / Alternative check:
A simple example in SQL Server would be: CREATE TABLE #TempResults Id INT, Total DECIMAL; followed by an INSERT INTO #TempResults statement that stores intermediate totals. Later, a SELECT query reads from #TempResults to produce the final report. When the session ends, SQL Server automatically removes #TempResults from tempdb. This behaviour matches the description in option a.


Why Other Options Are Wrong:
Option b claims that a temp table is a permanent table that can never be dropped or modified, which contradicts the temporary nature and automatic drop behaviour. Option c suggests automatic encryption for all data, which is not a defining property of temp tables; encryption is handled separately. Option d implies that temp tables cannot be queried and disappear at server restart only, which is also inaccurate; temp tables are fully queryable and their scope is tied to sessions and connections, not just server restarts.


Common Pitfalls:
A common pitfall is overusing temp tables when a single well written query would suffice, leading to unnecessary complexity. Another issue is forgetting that temp tables live in tempdb and can contribute to tempdb contention under heavy load. However, when used appropriately, temporary tables make complex logic clearer and can improve performance by breaking down tasks. For exam questions, focus on the definition: a temp table is created in a temporary database, often named with # or ##, and used to store intermediate results during a limited scope, as in option a.


Final Answer:
A temporary table is a table created in a temporary database, often with a name starting with # or ##, used to store intermediate results for the duration of a session or transaction.

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion