Hello everyone, I have faced an issue while working with CTE and Temp Tables in SQL. Could you please provide a simple explanation with an example?"
Would you like an explanation or an example for CTE and Temp Tables?
Hello everyone, I have faced an issue while working with CTE and Temp Tables in SQL. Could you please provide a simple explanation with an example?"
Would you like an explanation or an example for CTE and Temp Tables?
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Jignesh KumarRank #91 · 22.9K reputationPosted Jan 7, 2025, 12:57 PM EST
Hello Sourabh,
Let's take an example of each CTE (Common Table Expression) and Temp table.
CTE : Common table expression
SELECT,INSERT,UPDATE, orDELETEstatement.WITH CTE_Example AS ( SELECT EmployeeID, FirstName FROM Employees ) SELECT * FROM CTE_Example; This will work As expectedBut if you try to execute CTE in another statement like below,
WITH CTE_Example AS ( SELECT EmployeeID, FirstName FROM Employees ) SELECT * FROM CTE_Example; SELECT * FROM CTE_Example; -- This will fail because the CTE no longer exists.Temp Table
A Temporary Table is a table stored in the
tempdbdatabase, and it exists only for the duration of the user session or the batch in which it was created.-- Creating and populating a temporary table CREATE TABLE #TempEmployees ( EmployeeID INT, FirstName NVARCHAR(50), LastName NVARCHAR(50), Salary DECIMAL(10, 2) ); INSERT INTO #TempEmployees (EmployeeID, FirstName, LastName, Salary) SELECT EmployeeID, FirstName, LastName, Salary FROM Employees WHERE Salary > 50000; -- Using the temporary table in a query SELECT * FROM #TempEmployees WHERE EmployeeID < 10; -- Using the temporary table in a query again for second time with different condition SELECT * FROM #TempEmployees WHERE EmployeeID < 5; -- Temporary table is automatically dropped at the end of the session or can be dropped explicitly DROP TABLE #TempEmployees;You can see the difference here: a temporary table can be used multiple times, whereas a CTE cannot be reused, as demonstrated in the example above.
Difference you can find as below,
tempdb)Lokendra SinghRank #279 · 8.8K reputationPosted Jan 8, 2025, 5:15 AM EST
A CTE is like creating a temporary named result set that you can reference within a SELECT, INSERT, UPDATE, DELETE, or MERGE statement. It exists only for the duration of that query.
-- Create a simple Employee table CREATE TABLE Employees ( ID INT, Name VARCHAR(50), Salary INT ); -- Insert sample data INSERT INTO Employees VALUES (1, 'Loki', 50000), (2, 'Sunil', 60000), (3, 'Rakesh', 55000); -- Using CTE to find employees with above-average salary WITH high_paid_cte AS ( SELECT AVG(Salary) as AvgSalary FROM Employees ) SELECT e.Name, e.Salary FROM Employees e, high_paid_cte h WHERE e.Salary > h.AvgSalary;A few things to note:
Shubham SidnaleRank #968 · 2.1K reputationPosted Jan 7, 2025, 1:02 PM EST
CTE (Common Table Expression):
- Definition: A temporary result set defined within a SQL query.
- Scope: Exists only for the duration of the query in which it is defined.
- Declaration: Declared using the
- Storage: Does not store data physically in the database; works in-memory.
- Indexes: Cannot have indexes.
- Reusability: Cannot be reused across multiple queries; valid only within the query.
- Use Case: Ideal for simplifying complex queries, recursive queries, and improving code readability.
Temporary Table:WITHkeyword.CREATE TABLE #TableNameorSELECT INTO #TableName.tempdbdatabase.