0
0
PostgreSQLquery~20 mins

UPDATE with RETURNING clause in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Master of UPDATE with RETURNING
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2: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;
A[{ "id": 3, "salary": 6000 }]
B[{ "id": 3, "salary": 5000 }]
C[]
DSyntaxError
Attempts:
2 left
💡 Hint
The RETURNING clause outputs the updated rows after the UPDATE.
🧠 Conceptual
intermediate
1:30remaining
Purpose of RETURNING clause in UPDATE
What is the main purpose of using the RETURNING clause in an UPDATE statement in PostgreSQL?
ATo return the rows that were updated with their new values
BTo rollback the update if a condition fails
CTo update multiple tables at once
DTo create a backup of the updated rows
Attempts:
2 left
💡 Hint
Think about what you want to see immediately after updating rows.
📝 Syntax
advanced
1:30remaining
Identify the syntax error in UPDATE with RETURNING
Which of the following UPDATE statements with RETURNING clause is syntactically correct?
AUPDATE products SET price = price * 1.1 RETURNING;
BUPDATE products SET price = price * 1.1 RETURNING id, price;
CUPDATE products SET price = price * 1.1 RETURNING id price;
DUPDATE products SET price = price * 1.1 RETURNING id, price
Attempts:
2 left
💡 Hint
RETURNING must specify columns or * and end with semicolon.
optimization
advanced
2: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?
AUPDATE employees SET salary = salary * 1.1 WHERE department_id = 5; SELECT id, salary FROM employees WHERE department_id = 5;
BUPDATE employees SET salary = salary * 1.1 RETURNING id, salary WHERE department_id = 5;
CSELECT id, salary FROM employees WHERE department_id = 5; UPDATE employees SET salary = salary * 1.1 WHERE department_id = 5;
DUPDATE employees SET salary = salary * 1.1 WHERE department_id = 5 RETURNING id, salary;
Attempts:
2 left
💡 Hint
Consider how many queries run and how data is returned.
🔧 Debug
expert
2: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';
ANo error, query runs successfully
BRuntimeError: invalid date format
CSyntaxError: missing columns after RETURNING
DTypeError: wrong data type in SET clause
Attempts:
2 left
💡 Hint
RETURNING must be followed by columns or *.