0
0
PostgreSQLquery~10 mins

Returning modified rows with RETURNING in PostgreSQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Returning modified rows with RETURNING
Start UPDATE/DELETE/INSERT
Modify rows in table
RETURNING clause activated?
NoNo rows returned
Yes
Collect modified rows
Return rows to client
When you run an UPDATE, DELETE, or INSERT with RETURNING, the database modifies rows and then sends back the changed rows to you.
Execution Sample
PostgreSQL
UPDATE employees
SET salary = salary * 1.1
WHERE department = 'Sales'
RETURNING id, salary;
This query increases salary by 10% for Sales department employees and returns their id and new salary.
Execution Table
StepRow IDOriginal SalaryCondition (department='Sales')ActionReturned Row
110150000TrueSalary updated to 55000id=101, salary=55000
210248000FalseNo changeNo
310352000TrueSalary updated to 57200id=103, salary=57200
410447000FalseNo changeNo
510551000TrueSalary updated to 56100id=105, salary=56100
6----Execution ends: all rows checked
💡 All rows processed; only rows matching condition updated and returned.
Variable Tracker
VariableStartAfter 1After 2After 3Final
salary for id=1015000055000550005500055000
salary for id=1024800048000480004800048000
salary for id=1035200052000572005720057200
salary for id=1044700047000470004700047000
salary for id=1055100051000510005610056100
Key Moments - 2 Insights
Why do some rows not appear in the RETURNING output even though they exist in the table?
Only rows that meet the WHERE condition and are modified by the UPDATE appear in RETURNING, as shown in execution_table rows 2 and 4 where condition is False and no rows are returned.
Does RETURNING show the old values or the new updated values?
RETURNING shows the new updated values after the modification, as seen in execution_table rows 1, 3, and 5 where salaries are increased.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the salary of employee with id=103 after the update?
A5200
B52000
C57200
DNone
💡 Hint
Check the 'Returned Row' and 'Action' columns for row 3 in execution_table.
At which step does the condition department='Sales' become false?
AStep 2
BStep 3
CStep 1
DStep 5
💡 Hint
Look at the 'Condition' column in execution_table rows to find where it is False.
If the RETURNING clause was removed, what would change in the execution?
AModified rows would still be returned
BNo rows would be returned after update
CThe update would not happen
DOnly the first row would be returned
💡 Hint
RETURNING controls what rows are sent back; without it, no rows are returned.
Concept Snapshot
UPDATE/DELETE/INSERT with RETURNING
modifies rows then returns those rows
RETURNING shows new values after change
Only modified rows matching condition return
Useful to get updated data immediately
Full Transcript
This visual execution shows how PostgreSQL processes an UPDATE with RETURNING. First, it checks each row in the table. If the row matches the condition (department='Sales'), it updates the salary by 10%. Then, because of RETURNING, it sends back the id and new salary of those updated rows. Rows not matching the condition are not changed and not returned. The variable tracker shows salary changes step by step. Key moments clarify why only some rows return and that RETURNING shows new values. The quiz tests understanding of these steps.