End of Summer - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Inserting and updating NULLs
Conditions in UPDATE and DELETE
Updating the list of columns in one query
Using values from another column
Return rows in INSERT, UPDATE, DELETE
17. Display values in UPDATE
Summary

Instruction

Good job! As you probably guessed, RETURNING can also be used in UPDATE queries. It's quite similar to how it's used in DELETE and INSERT statements.

The teacher entered the wrong subject for the student Alan Stanley, id = 10. Let's take a look:

UPDATE exam
SET subject = 'Italian'
WHERE student_id = 10
RETURNING subject AS new_subject;

This statement changes the exam subject from Spanish to Italian. Notice that the RETURNING clause is placed at the end of the query. Using RETURNING allows us to see the updated record.

Note that we used an alias for the name of the returning column. In PostgreSQL, RETURNING returns values after they are changed by the UPDATE statement. You can't display the old values in UPDATE in PostgreSQL.

Look at the result:

new_subject
'Italian'

Exercise

The student Angela Smith (id = 2) married Peter Morgan. The teacher has to update Angela's last name to Morgan. Update the data and display Angela's new last name (column name new_last_name).