When writing complex SQL, developers often ask:
Should I use a CTE, a Temp Table, or a Derived Table?

All three can solve similar problems, but they behave very differently in terms of:
- Performance
- Scope & lifetime
- Reusability
- Debugging
- Real-world scalability
This article breaks it down clearly, practically, and with real SQL examples, so you always choose the right tool.
1. Quick Definitions (Plain English)
CTE (Common Table Expression)
A named temporary result set that exists only during query execution.
Think of a CTE as a logical step in your query.
Temp Table
A physically created table stored in tempdb.
Think of a temp table as a scratchpad database table.
Derived Table
A subquery in the FROM clause without a name outside that query.
Think of a derived table as a one-time inline dataset.
2. Basic Syntax Comparison
CTE
WITH SalesCTE AS (
SELECT CustomerID, SUM(Amount) AS TotalSales
FROM Sales
GROUP BY CustomerID
)
SELECT *
FROM SalesCTE
WHERE TotalSales > 100000;
Temp Table
CREATE TABLE #SalesTemp (
CustomerID INT,
TotalSales MONEY
);
INSERT INTO #SalesTemp
SELECT CustomerID, SUM(Amount)
FROM Sales
GROUP BY CustomerID;
SELECT *
FROM #SalesTemp
WHERE TotalSales > 100000;
Derived Table
SELECT *
FROM (
SELECT CustomerID, SUM(Amount) AS TotalSales
FROM Sales
GROUP BY CustomerID
) AS SalesDerived
WHERE TotalSales > 100000;
3. Real-World Analogy (Easy to Remember)
CTE = Whiteboard Notes
- Written for one discussion
- Easy to read
- Erased after use
Temp Table = Notebook
- Stores data physically
- Can be reused
- Can be indexed & analyzed
Derived Table = Sticky Note
- Quick
- Disposable
- Not reusable
4. Scope & Lifetime (Critical Difference)
| Feature | CTE | Temp Table | Derived Table |
|---|---|---|---|
| Exists beyond query | No | Yes (session) | No |
| Stored physically | No | Yes | No |
| Can be indexed | No | Yes | No |
| Can be reused | Limited | Yes | No |
| Stored in tempdb | NO | Yes | NO |
5. Performance Reality (Very Important)
Common Myth
“CTEs are always faster.”
Truth
- SQL Server usually inlines CTEs
- Derived tables behave almost identical to CTEs
- Temp tables:
- Have statistics
- Can have indexes
- Often outperform others on large datasets
6. When Temp Tables Outperform CTEs
Scenario: Large Data + Multiple Reuse
Bad (CTE re-executed):
WITH BigData AS (
SELECT * FROM Orders WHERE OrderDate >= '2024-01-01'
)
SELECT COUNT(*) FROM BigData;
SELECT SUM(Amount) FROM BigData;
Better (Temp Table):
SELECT *
INTO #BigData
FROM Orders
WHERE OrderDate >= '2024-01-01';
SELECT COUNT(*) FROM #BigData;
SELECT SUM(Amount) FROM #BigData;
Temp tables avoid recomputing large result sets.
7. Debugging & Maintainability
CTE / Derived Table
- Cannot
SELECT *separately - Harder to debug step-by-step
Temp Table
SELECT * FROM #BigData;
Easy inspection
Stepwise debugging
Production-friendly
8. Recursive Queries (CTE Superpower)
Only CTEs can do this cleanly.
WITH EmployeeTree AS (
SELECT EmployeeID, ManagerID, EmployeeName, 0 AS Level
FROM Employees
WHERE ManagerID IS NULL
UNION ALL
SELECT e.EmployeeID, e.ManagerID, e.EmployeeName, t.Level + 1
FROM Employees e
JOIN EmployeeTree t
ON e.ManagerID = t.EmployeeID
)
SELECT * FROM EmployeeTree;
Temp tables → complicated
Derived tables → impractical
9. Derived Tables: When Are They Useful?
Derived tables are best when:
- Logic is very small
- Used only once
- Query readability is still acceptable
Example:
SELECT o.*
FROM Orders o
JOIN (
SELECT CustomerID
FROM Customers
WHERE Country = 'India'
) c
ON o.CustomerID = c.CustomerID;
Simple
Inline
Not reusable
10. Comparison Summary Table
| Use Case | Best Choice |
|---|---|
| Simple one-time logic | Derived Table |
| Readable multi-step logic | CTE |
| Large datasets | Temp Table |
| Reuse data multiple times | Temp Table |
| Recursive hierarchy | CTE |
| Debugging & troubleshooting | Temp Table |
| Performance-critical queries | Temp Table |
11. Decision Flow (Easy Rule)
Ask yourself:
- Need recursion? → CTE
- Large data or reuse? → Temp Table
- Small inline logic? → Derived Table
Final Recommendation
CTEs improve readability
Temp Tables improve performance & debugging
Derived Tables keep queries compact
Golden Rule (Production SQL)
If the query:
- Runs once → CTE / Derived Table
- Runs heavy logic → Temp Table
- Needs maintenance → CTE or Temp Table
Discover more from Technology with Vivek Johari
Subscribe to get the latest posts sent to your email.



