0
0
PostgreSQLquery~5 mins

UPDATE with RETURNING clause in PostgreSQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
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
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;
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
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
Which SQL dialect supports UPDATE with RETURNING clause?
APostgreSQL
BMySQL (all versions)
CSQLite
DOracle
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.