Challenge - 5 Problems
Writable CTE Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Output of UPDATE using writable CTE
Given the table
employees with columns id, name, and salary, what will be the output of the following query?WITH updated AS ( UPDATE employees SET salary = salary * 1.1 WHERE id = 2 RETURNING id, salary ) SELECT * FROM updated;
PostgreSQL
WITH updated AS ( UPDATE employees SET salary = salary * 1.1 WHERE id = 2 RETURNING id, salary ) SELECT * FROM updated;
Attempts:
2 left
💡 Hint
The CTE returns rows affected by the UPDATE with new salary values.
✗ Incorrect
The UPDATE affects only the employee with id=2, increasing salary by 10%. The RETURNING clause outputs the updated row with new salary.
❓ query_result
intermediate2:00remaining
Result of DELETE with writable CTE
Consider the table
products with columns product_id and stock. What will this query return?WITH deleted AS ( DELETE FROM products WHERE stock = 0 RETURNING product_id ) SELECT COUNT(*) FROM deleted;
PostgreSQL
WITH deleted AS ( DELETE FROM products WHERE stock = 0 RETURNING product_id ) SELECT COUNT(*) FROM deleted;
Attempts:
2 left
💡 Hint
The CTE returns all deleted rows, then counts them.
✗ Incorrect
The DELETE removes all products with stock=0 and RETURNING outputs those rows. COUNT(*) counts how many were deleted.
📝 Syntax
advanced2:00remaining
Identify syntax error in writable CTE with INSERT
Which option contains a syntax error in this writable CTE that inserts a new row?
WITH ins AS ( INSERT INTO orders (order_id, amount) VALUES (101, 250) RETURNING order_id ) SELECT * FROM ins;
PostgreSQL
WITH ins AS ( INSERT INTO orders (order_id, amount) VALUES (101, 250) RETURNING order_id ) SELECT * FROM ins;
Attempts:
2 left
💡 Hint
Check the syntax of the INSERT statement inside the CTE.
✗ Incorrect
Option A misses parentheses around columns in INSERT INTO clause, causing syntax error.
❓ optimization
advanced2:00remaining
Best practice for updating multiple rows with writable CTE
You want to increase salary by 5% for all employees in department 10 and get their updated salaries. Which query is most efficient and correct?
Options:
A) Use a writable CTE with UPDATE and RETURNING, then select from it.
B) Use a subquery to select employees, then update outside.
C) Use two separate queries: one UPDATE, one SELECT.
D) Use a writable CTE with DELETE and INSERT to replace rows.
Options:
A) Use a writable CTE with UPDATE and RETURNING, then select from it.
B) Use a subquery to select employees, then update outside.
C) Use two separate queries: one UPDATE, one SELECT.
D) Use a writable CTE with DELETE and INSERT to replace rows.
Attempts:
2 left
💡 Hint
Writable CTEs can update and return rows in one query.
✗ Incorrect
Option A uses writable CTE to update and return updated rows efficiently in one step.
🧠 Conceptual
expert3:00remaining
Effect of writable CTE on transaction visibility
In PostgreSQL, when using a writable CTE with DELETE and RETURNING inside a transaction, what happens if you try to SELECT the deleted rows outside the CTE but before committing?
Choose the correct behavior:
Choose the correct behavior:
Attempts:
2 left
💡 Hint
Think about how RETURNING works and transaction isolation in PostgreSQL.
✗ Incorrect
Writable CTE's RETURNING shows deleted rows immediately inside the CTE, but outside SELECTs still see old data until commit.