Challenge - 5 Problems
Master of UPDATE with RETURNING
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Output of UPDATE with RETURNING clause
Given the table employees with columns
id, name, and salary, what is the output of this query?UPDATE employees SET salary = salary + 1000 WHERE id = 3 RETURNING id, salary;
PostgreSQL
UPDATE employees SET salary = salary + 1000 WHERE id = 3 RETURNING id, salary;
Attempts:
2 left
💡 Hint
The RETURNING clause outputs the updated rows after the UPDATE.
✗ Incorrect
The query increases salary by 1000 for employee with id 3. If original salary was 5000, new salary is 6000. RETURNING shows the updated row.
🧠 Conceptual
intermediate1:30remaining
Purpose of RETURNING clause in UPDATE
What is the main purpose of using the RETURNING clause in an UPDATE statement in PostgreSQL?
Attempts:
2 left
💡 Hint
Think about what you want to see immediately after updating rows.
✗ Incorrect
RETURNING lets you see the updated rows and their new values without running a separate SELECT.
📝 Syntax
advanced1:30remaining
Identify the syntax error in UPDATE with RETURNING
Which of the following UPDATE statements with RETURNING clause is syntactically correct?
Attempts:
2 left
💡 Hint
RETURNING must specify columns or * and end with semicolon.
✗ Incorrect
Option B correctly lists columns and ends with semicolon. B misses columns. C misses comma. D misses semicolon.
❓ optimization
advanced2:00remaining
Optimizing UPDATE with RETURNING for multiple rows
You want to update salaries for all employees in department 5 by 10% and get their new salaries. Which query is the most efficient?
Attempts:
2 left
💡 Hint
Consider how many queries run and how data is returned.
✗ Incorrect
Option D updates and returns updated rows in one query. A runs two queries. C returns old salaries before update. D has syntax error.
🔧 Debug
expert2:00remaining
Error caused by incorrect RETURNING usage
What error will this query produce?
UPDATE orders SET status = 'shipped' RETURNING WHERE order_date < '2024-01-01';
Attempts:
2 left
💡 Hint
RETURNING must be followed by columns or *.
✗ Incorrect
RETURNING clause requires columns or * after it. Here it is empty, causing syntax error.