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
Ready to master this question?
Generate a complete walkthrough — background, the full answer in plain language, a working code example explained line by line, a real-world scenario, common mistakes, and how this same question gets asked in different ways.
Sign in to generate a response