beginnerCTEs — Common Table Expressions & Recursive CTEs
What is a writeable CTE in PostgreSQL?
A writeable CTE contains DML (INSERT, UPDATE, DELETE) in the CTE body and uses RETURNING to pass modified data to the main query or other CTEs. Example: WITH updated AS (UPDATE employees SET salary = salary * 1.1 WHERE dept_id = 1 RETURNING emp_id, salary) INSERT INTO audit(emp_id, new_salary) SELECT emp_id, salary FROM updated. Both UPDATE and INSERT execute in a single transaction — atomic. The
This is a Pro chapter
Sign in, then upgrade to Pro or Power to unlock this and the full Databases Mastery library.
What is a writeable CTE in PostgreSQL?