Choosing the Right Technique
Compare subqueries, CTEs, views, temp tables, CTAS, procedures and triggers side by side, plus a one-page cheat sheet.
You now have seven tools that can all "hide complexity". Choosing the wrong one leads to slow dashboards, stale data or code nobody can find. This topic puts them side by side.
Definition — the key questions
Choosing an advanced SQL technique comes down to three questions: Does it need to store data? How long must it live? Who needs to reuse it?
The big picture
| Technique | Stores data? | Lives for | Reusable? | Best for |
|---|---|---|---|---|
| Subquery | No | One query | No | Quick nested logic |
| CTE | No | One query | No (only inside that query) | Readable step-by-step queries, recursion |
| View | No (query only) | Until dropped | Yes — any user / query | Shared logic, security |
| Temp table | Yes | Your session | In your session | Multi-step processing |
| CTAS table | Yes | Until dropped | Yes | Snapshots, speed, data marts |
| Stored procedure | No (stores code) | Until dropped | Yes — EXEC | Repeated jobs with parameters and logic |
| Trigger | No (stores code) | Until dropped | Runs automatically | Audit logs, automatic rules |
Example — same question, five ways
"Which teams scored 700+ runs?"
sql
-- 1. Subquery
SELECT * FROM (SELECT team, SUM(runs) AS total FROM players GROUP BY team) AS t
WHERE total >= 700;
-- 2. CTE
WITH t AS (SELECT team, SUM(runs) AS total FROM players GROUP BY team)
SELECT * FROM t WHERE total >= 700;
-- 3. View (saved for everyone)
CREATE VIEW v_team_totals AS
SELECT team, SUM(runs) AS total FROM players GROUP BY team;
SELECT * FROM v_team_totals WHERE total >= 700;
-- 4. CTAS (SQL Server: SELECT … INTO) — a stored snapshot
SELECT team, SUM(runs) AS total INTO team_totals_snapshot FROM players GROUP BY team;
SELECT * FROM team_totals_snapshot WHERE total >= 700;
-- 5. Temp table — private to your session
SELECT team, SUM(runs) AS total INTO #team_totals FROM players GROUP BY team;
SELECT * FROM #team_totals WHERE total >= 700;All five return:
| team | total |
|---|---|
| DC | 700 |
| RCB | 750 |
| RR | 850 |
The difference is not the answer — it is lifetime, storage, freshness and who can reuse it.
Decision guide
| Situation | Use |
|---|---|
| A quick filter against an average / list | Subquery |
| Several logical steps in one report, or a hierarchy | CTE (recursive CTE for hierarchies) |
| Many people need the same logic, or must not see some columns | View |
| A heavy intermediate result used several times in one script | Temp table |
| A slow report everyone reads, or a backup before a risky change | CTAS |
| A repeatable job with parameters, IF/ELSE and error handling | Stored procedure |
| Something must happen every time data changes | Trigger |
Freshness vs speed
| Always fresh | Fast to read | |
|---|---|---|
| Subquery / CTE / View | ✅ (runs live) | ❌ re-computes each time |
| CTAS / Temp table | ❌ (snapshot) | ✅ pre-computed |
Advanced SQL cheat sheet
| Syntax | What it is |
|---|---|
(SELECT …) in FROM / SELECT / WHERE | Subquery: scalar, list or table |
WITH name AS (…) SELECT … | CTE — one query; add more with commas |
WITH … UNION ALL … (self-reference) | Recursive CTE: anchor + recursive member + stop |
CREATE VIEW v AS SELECT … | Saved query — no data stored |
CREATE TABLE t AS SELECT … | CTAS (SQL Server: SELECT … INTO t) |
#temp / CREATE TEMPORARY TABLE | Scratch table for your session |
CREATE PROCEDURE p … EXEC p | Saved program with parameters |
CREATE TRIGGER … AFTER INSERT | Runs automatically on a change |
Common mistakes
- ❌ Using a view to "speed up" a slow query → it still runs the query each time. ✅ CTAS / indexed view.
- ❌ Using a CTAS table for data that must always be current → stale numbers. ✅ view.
- ❌ Creating permanent tables for scratch work and forgetting to drop them. ✅
#temptables clean themselves up. - ❌ Hiding business rules in many triggers → surprising side effects. ✅ prefer stored procedures for explicit jobs.
Interview tip
Q: CTE vs temp table vs view — when would you use each? A CTE to make one complex query readable (or recursive); a temp table when an intermediate result is large or reused by several statements in your session; a view when the logic must be shared and reused by many users, possibly to hide columns for security.
Next up: Performance Optimization — make your SQL fast.
Key points to remember
- Pick a technique by asking: does it store data, how long must it live, and who reuses it?
- Subqueries and CTEs live for one query and store nothing.
- Views store only the query, live until dropped, and are always fresh.
- Temp tables and CTAS tables store real rows — fast to read but a snapshot, not live.
- Stored procedures are reusable programs run with EXEC; triggers run automatically on data changes.
- Freshness vs speed: live tools (subquery, CTE, view) re-compute; stored tools (CTAS, temp) are pre-computed.
Try it yourself
Subquery, CTE and view — same answer
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
CTAS and temp table versions (SQL Server)
-- 4. CTAS: a permanent snapshot
SELECT team, SUM(runs) AS total
INTO team_totals_snapshot
FROM players
GROUP BY team;
SELECT * FROM team_totals_snapshot WHERE total >= 700;
-- 5. Temp table: private to this session, dropped on disconnect
SELECT team, SUM(runs) AS total
INTO #team_totals
FROM players
GROUP BY team;
SELECT * FROM #team_totals WHERE total >= 700;SQL Server-specific — run this in SQL Server (SSMS / Azure Data Studio).
Practice: combine a CTE with a subquery
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Revision Flashcards
Tap a card to flip it and see the answer.
