Multiple CTEs
Multiple CTEs is a SQL statement in the CTE (WITH) category. Define multiple CTEs in one WITH clause, comma-separated. Each can reference previous CTEs. The syntax is WITH cte1 AS (query1), cte2 AS (query2) SELECT .... It returns multiple CTEs. A typical example: WITH dept_stats AS ( SELECT dept_id, AVG(salary) AS avg_sal FROM employees GROUP BY dept_id ), top_depts AS ( SELECT dept_id FROM dept_stats WHERE avg_sal > 70000 ) SELECT d.name, s.avg_sal FROM dept_stats s JOIN top_depts t ON s.dept_id = t.dept_id JOIN departments d ON d.id = s.dept_id; A close relative is WITH (CTE), which common Table Expression — define a named temporary result set that can be referenced in the main query. A close relative is Recursive CTE, which a CTE that references itself, allowing hierarchical or recursive queries (org charts, tree structures). A close relative is CTE in INSERT, which use a CTE to compute data before inserting it into a table.