CTEs — WITH … AS in SQL
Named subqueries that make complex logic readable
A Common Table Expression is a named subquery introduced with WITH name AS (…) that the rest of the statement can use like a temporary view. Instead of one giant nested expression you build readable layers: define dept_stats once, then join or filter against it. CTEs exist only for the duration of the statement, and multiple CTEs can chain comma-separated, each seeing the previous ones.
Name the inner query
WITH dept_stats AS (SELECT department, AVG(salary) AS avg_sal FROM employees GROUP BY department) — the CTE computes once and behaves like a table for this statement.
Use it like a table
The main query joins employees to dept_stats on department — a completely ordinary join; nothing special remains about the CTE.
Filter against the computed column
WHERE e.salary > ds.avg_sal — each employee is compared to their own department average, computed in the layer below.
Chain layers with commas
WITH a AS (…), b AS (SELECT … FROM a WHERE …) — each CTE may reference the ones before it; complexity stays linear and readable.
It lives for one statement
The CTE exists only for this query — not stored, not shareable. Need it across queries? That is a view (next topic).
1WITH dept_stats AS (2 SELECT department, AVG(salary) AS avg_sal3 FROM employees GROUP BY department4)5SELECT e.name, e.department, e.salary6FROM employees e7JOIN dept_stats ds ON e.department = ds.department8WHERE e.salary > ds.avg_sal;
| name | department | salary |
|---|---|---|
| Alice | Engineering | 85000 |
| Cynthia | Engineering | 91000 |
| Elena | Sales | 58000 |
dept_stats holds each department’s average (83,000 / 55,000 / 47,000). Joining employees to it and keeping above-average earners gives Alice and Cynthia (Engineering, avg 83k) plus Elena (Sales, avg 55k). David sits exactly at his department’s average, so > excludes him.
Above department average
The flagship CTE query: compute per-group stats, then join and filter.
1WITH dept_stats AS (2 SELECT department, AVG(salary) AS avg_sal3 FROM employees GROUP BY department4)5SELECT e.name, e.department, e.salary6FROM employees e7JOIN dept_stats ds ON e.department = ds.department8WHERE e.salary > ds.avg_sal;
| name | department | salary |
|---|---|---|
| Alice | Engineering | 85000 |
| Cynthia | Engineering | 91000 |
| Elena | Sales | 58000 |
Chained CTEs — layers on layers
Each named step does one thing; the next step builds on it.
1WITH eng AS (2 SELECT * FROM employees WHERE department = 'Engineering'3),4rich_eng AS (5 SELECT * FROM eng WHERE salary > 800006)7SELECT name, salary FROM rich_eng ORDER BY salary DESC;
| name | salary |
|---|---|
| Cynthia | 91000 |
| Alice | 85000 |
CTE referenced twice — no subquery duplication
Name it once, use it twice — impossible with a bare subquery.
1WITH stats AS (2 SELECT MIN(salary) AS lo, MAX(salary) AS hi FROM employees3)4SELECT e.name, e.salary5FROM employees e, stats6WHERE e.salary = stats.lo OR e.salary = stats.hi;
| name | salary |
|---|---|
| Cynthia | 91000 |
| David | 47000 |
Same query without the CTE
What the WITH clause buys you is readability, not power.
1SELECT e.name, e.department, e.salary2FROM employees e3JOIN (SELECT department, AVG(salary) AS avg_sal FROM employees GROUP BY department) ds4 ON e.department = ds.department5WHERE e.salary > ds.avg_sal;
| name | department | salary |
|---|---|---|
| Alice | Engineering | 85000 |
| Cynthia | Engineering | 91000 |
| Elena | Sales | 58000 |
Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Take the queries you just visualized and see the data structures that power real databases.