What does the RETURNING clause do in an UPDATE statement?
It returns the updated rows immediately after the UPDATE operation, showing the new values without needing a separate SELECT query.
Click to reveal answer
beginner
Write a simple UPDATE statement with RETURNING to change a user's email and get the updated email back.
UPDATE users SET email = 'newemail@example.com' WHERE id = 1 RETURNING email;
Click to reveal answer
intermediate
Why is using RETURNING in UPDATE useful compared to running UPDATE then SELECT?
It saves time and resources by combining update and retrieval in one step, reducing the number of queries and network traffic.
Click to reveal answer
intermediate
Can RETURNING clause return multiple columns? Give an example.
Yes, it can return multiple columns. Example: UPDATE products SET price = price * 1.1 RETURNING id, price;
Click to reveal answer
beginner
What happens if no rows match the UPDATE condition when using RETURNING?
No rows are updated and the RETURNING clause returns an empty result set.
Click to reveal answer
What is the purpose of the RETURNING clause in an UPDATE statement?
ATo rollback the update if a condition fails
BTo delete rows after updating
CTo return the updated rows immediately after the update
DTo create a backup of updated rows
✗ Incorrect
The RETURNING clause returns the updated rows right after the UPDATE operation.
Which of the following is a valid use of RETURNING in PostgreSQL?
AUPDATE users SET name = 'Alice' WHERE id = 1;
BUPDATE users RETURNING name WHERE id = 1;
CRETURNING UPDATE users SET name = 'Alice';
DUPDATE users SET name = 'Alice' RETURNING id, name;
✗ Incorrect
Option D correctly places RETURNING after the SET clause to return specified columns.
If an UPDATE statement with RETURNING affects no rows, what is returned?
AAn error message
BAn empty result set
CAll rows in the table
DThe previous values before update
✗ Incorrect
No rows updated means RETURNING returns an empty result set.
Can RETURNING be used to get the old values before update?
ANo, RETURNING only returns new values
BYes, by using RETURNING * BEFORE UPDATE
CYes, by specifying OLD.column_name
DNo, you must use a trigger for old values
✗ Incorrect
RETURNING returns the new values after update; old values require triggers or other methods.
Which SQL dialect supports UPDATE with RETURNING clause?
APostgreSQL
BMySQL (all versions)
CSQLite
DOracle
✗ Incorrect
PostgreSQL supports UPDATE with RETURNING; MySQL and SQLite do not support it natively.
Explain how the RETURNING clause works in an UPDATE statement and why it is useful.
Think about how you get updated data without a second query.
You got /3 concepts.
Write an example UPDATE query using RETURNING to update a record and get back multiple columns.
Use RETURNING after SET and list columns separated by commas.
You got /4 concepts.
Practice
(1/5)
1. What does the RETURNING clause do in a PostgreSQL UPDATE statement?
easy
A. It returns the updated rows immediately after the update.
B. It deletes the rows after updating them.
C. It creates a backup of the updated rows.
D. It prevents the update from happening.
Solution
Step 1: Understand the purpose of UPDATE
The UPDATE statement changes data in existing rows of a table.
Step 2: Role of RETURNING clause
The RETURNING clause immediately outputs the rows that were updated, showing their new values.
Final Answer:
It returns the updated rows immediately after the update. -> Option A
Quick Check:
RETURNING shows updated rows [OK]
Hint: RETURNING shows updated rows right after update [OK]
Common Mistakes:
Thinking RETURNING deletes rows
Assuming RETURNING creates backups
Believing RETURNING stops the update
2. Which of the following is the correct syntax to update a table and return the updated rows in PostgreSQL?
easy
A. UPDATE table_name SET column1 = value1 WHERE condition RETURNING *;
B. UPDATE table_name SET column1 = value1 RETURN * WHERE condition;
C. UPDATE table_name RETURNING * SET column1 = value1 WHERE condition;
D. UPDATE table_name SET column1 = value1 WHERE condition SHOW RETURNING *;
Solution
Step 1: Identify correct UPDATE syntax
The UPDATE statement must have SET clause before WHERE clause.
Step 2: Position of RETURNING clause
RETURNING clause comes after WHERE clause to output updated rows.
Final Answer:
UPDATE table_name SET column1 = value1 WHERE condition RETURNING *; -> Option A
Quick Check:
RETURNING follows WHERE in UPDATE [OK]
Hint: RETURNING always comes after WHERE clause [OK]
Common Mistakes:
Placing RETURNING before SET clause
Using SHOW RETURNING which is invalid
Putting RETURNING between SET and WHERE
3. Given the table employees with columns id, salary, and department, what will this query return?
UPDATE employees SET salary = salary + 500 WHERE department = 'Sales' RETURNING id, salary;
medium
A. Only the number of rows updated, no data returned.
B. All employees' ids and salaries regardless of department.
C. All employees' ids and new salaries in the Sales department after the raise.
D. An error because RETURNING cannot specify columns.
Solution
Step 1: Understand the UPDATE condition
The query updates salary by adding 500 only for employees in the Sales department.
Step 2: Effect of RETURNING clause
RETURNING id, salary outputs the id and updated salary of only those employees updated.
Final Answer:
All employees' ids and new salaries in the Sales department after the raise. -> Option C
Quick Check:
RETURNING outputs updated rows only [OK]
Hint: RETURNING shows only updated rows, not all rows [OK]
Common Mistakes:
Thinking all rows are returned
Expecting only count of updated rows
Believing RETURNING cannot specify columns
4. What is wrong with this query?
UPDATE products RETURNING * SET price = price * 1.1 WHERE category = 'Books';
medium
A. The price calculation is invalid syntax.
B. The RETURNING clause is placed before SET, causing a syntax error.
C. The WHERE clause is missing.
D. RETURNING cannot be used with UPDATE.
Solution
Step 1: Check clause order in UPDATE
UPDATE syntax requires SET before WHERE and RETURNING last.
Step 2: Identify error in query
RETURNING appears before SET, which is invalid and causes syntax error.
Final Answer:
The RETURNING clause is placed before SET, causing a syntax error. -> Option B
Quick Check:
RETURNING must come after WHERE and SET [OK]
Hint: RETURNING always comes last after WHERE [OK]
Common Mistakes:
Placing RETURNING before SET
Omitting WHERE clause when needed
Misordering clauses in UPDATE
5. You want to increase the salary by 10% for employees in the 'Engineering' department and immediately get their id, old_salary, and new_salary. Which query correctly does this using UPDATE with RETURNING?
hard
A. UPDATE employees SET salary = salary * 1.1 WHERE department = 'Engineering' RETURNING id, salary, salary / 1.1 AS old_salary;
B. UPDATE employees SET salary = salary * 1.1 WHERE department = 'Engineering' RETURNING id, salary, salary * 1.1 AS old_salary;
C. UPDATE employees SET salary = salary * 1.1 WHERE department = 'Engineering' RETURNING id, salary AS old_salary, salary * 1.1 AS new_salary;
D. UPDATE employees SET salary = salary * 1.1 WHERE department = 'Engineering' RETURNING id, salary AS new_salary, salary / 1.1 AS old_salary;
Solution
Step 1: Understand the salary update
Salary is multiplied by 1.1 to increase by 10%, so new salary is salary * 1.1.
Step 2: Calculate old salary in RETURNING
After update, salary column shows new salary. To get old salary, divide new salary by 1.1.
Step 3: Check aliasing in RETURNING
RETURNING must show id, new_salary (salary), and old_salary (salary / 1.1) with correct aliases.
Final Answer:
UPDATE employees SET salary = salary * 1.1 WHERE department = 'Engineering' RETURNING id, salary AS new_salary, salary / 1.1 AS old_salary; -> Option D
Quick Check:
Old salary = new salary / 1.1 [OK]
Hint: Use RETURNING with expressions and aliases for old and new values [OK]
Common Mistakes:
Confusing old and new salary calculations
Wrong aliasing in RETURNING clause
Using expressions incorrectly in RETURNING
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.