Writable CTEs let you change data (insert, update, delete) inside a query. This helps organize complex changes step-by-step.
CTE with INSERT, UPDATE, DELETE (writable CTEs) in PostgreSQL
WITH cte_name AS ( INSERT INTO table_name (column_list) VALUES (value_list) RETURNING * ) SELECT * FROM cte_name;
Writable CTEs use INSERT, UPDATE, or DELETE inside the WITH clause.
Use RETURNING to get the changed rows and use them in the next query.
WITH updated AS ( UPDATE employees SET salary = salary * 1.1 WHERE department = 'Sales' RETURNING * ) SELECT * FROM updated;
WITH inserted AS ( INSERT INTO products (name, price) VALUES ('New Product', 20) RETURNING * ) SELECT * FROM inserted;
WITH deleted AS ( DELETE FROM orders WHERE order_date < '2023-01-01' RETURNING * ) SELECT COUNT(*) FROM deleted;
This query adds 500 to the salary of all employees in the HR department and then shows their id, name, and new salary.
WITH updated_employees AS ( UPDATE employees SET salary = salary + 500 WHERE department = 'HR' RETURNING id, name, salary ) SELECT * FROM updated_employees ORDER BY id;
Writable CTEs run the data change first, then the following SELECT uses the changed data.
Always use RETURNING to get the affected rows; otherwise, you can't use the changed data in the next step.
Writable CTEs help keep queries clean and easy to understand when doing multiple steps.
Writable CTEs let you insert, update, or delete data inside a WITH clause.
Use RETURNING to get the changed rows and use them immediately.
This makes complex data changes easier to write and read.
Practice
Solution
Step 1: Understand writable CTEs
Writable CTEs allow data modification commands like INSERT, UPDATE, or DELETE inside a WITH clause.Step 2: Compare options
Options B, C, and D describe other features unrelated to writable CTEs.Final Answer:
To perform INSERT, UPDATE, or DELETE operations inside a WITH clause -> Option CQuick Check:
Writable CTEs = Data modification in WITH [OK]
- Confusing writable CTEs with temporary tables
- Thinking writable CTEs speed up SELECT queries
- Mixing writable CTEs with function definitions
Solution
Step 1: Review writable CTE update syntax
The correct syntax uses WITH updated AS (UPDATE ... RETURNING *) followed by a SELECT from updated.Step 2: Analyze options
WITH updated AS (UPDATE employees SET salary = salary * 1.1 RETURNING *) SELECT * FROM updated; matches the correct syntax. Options A, C, and D have syntax errors or misuse RETURNING.Final Answer:
WITH updated AS (UPDATE employees SET salary = salary * 1.1 RETURNING *) SELECT * FROM updated; -> Option DQuick Check:
Writable CTE update = WITH + UPDATE + RETURNING + SELECT [OK]
- Omitting RETURNING clause in writable CTE
- Trying to UPDATE inside WITH without SELECT
- Misplacing RETURNING outside the CTE
products(id, name, stock) with rows: (1, 'Pen', 10), (2, 'Pencil', 5), what will be the result of this query?WITH updated AS (UPDATE products SET stock = stock + 10 WHERE name = 'Pen' RETURNING *) SELECT * FROM updated;
Solution
Step 1: Identify updated rows
The UPDATE increases stock by 10 only for product with name 'Pen' (id=1), changing stock from 10 to 20.Step 2: Understand RETURNING and SELECT
The RETURNING * returns the updated row, and the SELECT outputs it.Final Answer:
[{id:1, name:'Pen', stock:20}] -> Option AQuick Check:
Updated Pen stock = 10 + 10 = 20 [OK]
- Assuming all rows are updated
- Ignoring RETURNING clause output
- Confusing stock before and after update
WITH deleted AS (DELETE FROM orders WHERE status = 'cancelled') SELECT * FROM deleted;
Solution
Step 1: Check DELETE in writable CTE
DELETE inside WITH must include RETURNING to produce rows for the outer SELECT.Step 2: Analyze the query
The query lacks RETURNING, so 'deleted' has no rows to select from, resulting in an empty result set.Final Answer:
Missing RETURNING clause in DELETE statement -> Option BQuick Check:
Writable CTE DELETE needs RETURNING [OK]
- Omitting RETURNING after DELETE in CTE
- Thinking DELETE can't be in WITH clause
- Misunderstanding SELECT after writable CTE
Solution
Step 1: Insert and capture new rows
Use a writable CTE to insert and RETURNING id, name to identify new users.Step 2: Update using CTE results
Update users joining with inserted rows on id, setting status to 'active', and RETURNING updated rows.Step 3: Analyze options
WITH ins AS (INSERT INTO users(name) VALUES ('Alice'), ('Bob') RETURNING id, name) UPDATE users SET status = 'active' FROM ins WHERE users.id = ins.id RETURNING users.*; correctly chains insert and update in one statement with proper RETURNING and join. INSERT INTO users(name) VALUES ('Alice'), ('Bob'); WITH upd AS (UPDATE users SET status = 'active' WHERE name IN ('Alice', 'Bob') RETURNING *) SELECT * FROM upd; separates insert and update into multiple statements. WITH ins AS (INSERT INTO users(name) VALUES ('Alice'), ('Bob')) UPDATE users SET status = 'active' WHERE id IN (SELECT id FROM ins); lacks RETURNING so CTE returns no rows and subquery updates nothing. WITH ins AS (INSERT INTO users(name) VALUES ('Alice'), ('Bob')) UPDATE ins SET status = 'active' WHERE name IN ('Alice', 'Bob'); incorrectly attempts to UPDATE the CTE which is invalid.Final Answer:
WITH ins AS (INSERT INTO users(name) VALUES ('Alice'), ('Bob') RETURNING id, name) UPDATE users SET status = 'active' FROM ins WHERE users.id = ins.id RETURNING users.*; -> Option AQuick Check:
Writable CTE insert + update with join = WITH ins AS (INSERT INTO users(name) VALUES ('Alice'), ('Bob') RETURNING id, name) UPDATE users SET status = 'active' FROM ins WHERE users.id = ins.id RETURNING users.*; [OK]
- Omitting RETURNING clause before using CTE in subquery
- Splitting insert and update into separate statements
- Omitting RETURNING to get updated rows
Ready to prove it?
Pass the test and this pattern moves unknown
