CTE in INSERT
CTE in INSERT is a SQL statement in the CTE (WITH) category. Use a CTE to compute data before inserting it into a table. The syntax is WITH cte AS (SELECT ...) INSERT INTO target_table SELECT * FROM cte;. It returns rows inserted. A typical example: WITH high_earners AS ( SELECT name, salary, dept_id FROM employees WHERE salary > 80000 ) INSERT INTO employee_reports (name, salary, report_date) SELECT name, salary, CURDATE() FROM high_earners; -- Computes data via CTE, then inserts into reporting table 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 Multiple CTEs, which define multiple CTEs in one WITH clause, comma-separated. Each can reference previous CTEs.