WITH (CTE)
WITH (CTE) is a SQL statement in the CTE (WITH) category. Common Table Expression — define a named temporary result set that can be referenced in the main query. The syntax is WITH cte_name AS (select_statement) SELECT * FROM cte_name;. It returns temporary named result. A typical example: WITH high_earners AS ( SELECT name, salary, dept_id FROM employees WHERE salary > 80000 ) SELECT h.name, h.salary, d.name AS department FROM high_earners h JOIN departments d ON h.dept_id = d.id; 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 Multiple CTEs, which define multiple CTEs in one WITH clause, comma-separated. Each can reference previous CTEs. A close relative is CTE in INSERT, which use a CTE to compute data before inserting it into a table. More about this category: Common Table Expressions — WITH, recursive CTE, multiple CTEs.