Bird
0
0

Which of the following demonstrates the correct way to perform an UPDATE using a writable CTE in PostgreSQL?

easy📝 Syntax Q3 of 15
PostgreSQL - Common Table Expressions
Which of the following demonstrates the correct way to perform an UPDATE using a writable CTE in PostgreSQL?
AWITH updated AS (UPDATE employees SET salary = salary * 1.1 WHERE department = 'Sales' RETURNING *) SELECT * FROM updated;
BWITH updated AS (SELECT * FROM employees WHERE department = 'Sales') UPDATE employees SET salary = salary * 1.1 WHERE department = 'Sales';
CUPDATE employees SET salary = salary * 1.1 WHERE department = 'Sales' WITH updated AS RETURNING *;
DWITH updated AS (UPDATE employees SET salary = salary * 1.1) SELECT * FROM employees WHERE department = 'Sales';
Step-by-Step Solution
Solution:
  1. Step 1: Use a writable CTE with UPDATE and RETURNING

    The writable CTE must include the UPDATE statement followed by a RETURNING clause to capture updated rows.
  2. Step 2: Select from the CTE

    After the CTE, a SELECT statement retrieves the updated rows from the CTE alias.
  3. Final Answer:

    WITH updated AS (UPDATE employees SET salary = salary * 1.1 WHERE department = 'Sales' RETURNING *) SELECT * FROM updated; -> Option A
  4. Quick Check:

    Writable CTEs require RETURNING to output updated rows [OK]
Quick Trick: Writable CTE UPDATE needs RETURNING clause [OK]
Common Mistakes:
  • Omitting RETURNING clause in writable CTE
  • Trying to UPDATE inside a SELECT without RETURNING
  • Incorrect placement of WITH clause
  • Selecting from base table instead of CTE

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes