Bird
Raised Fist0
PostgreSQLquery~10 mins

UPDATE with RETURNING clause in PostgreSQL - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - UPDATE with RETURNING clause
Start UPDATE command
↓
Find rows matching WHERE condition
↓
Update specified columns
↓
Collect updated rows
↓
RETURNING clause outputs updated rows
↓
End
The UPDATE command finds rows matching the condition, changes specified columns, then RETURNING outputs the updated rows.
Execution Sample
PostgreSQL
UPDATE employees
SET salary = salary + 500
WHERE department = 'Sales'
RETURNING id, name, salary;
This updates salaries for Sales department employees and returns their id, name, and new salary.
Execution Table
StepActionRows MatchedColumns UpdatedRETURNING Output
1Start UPDATE---
2Find rows WHERE department = 'Sales'Rows with dept='Sales' found: 2--
3Update salary = salary + 5002 rowssalary column updated-
4RETURNING id, name, salary2 rows-[{"id": 3, "name": "Alice", "salary": 3500}, {"id": 5, "name": "Bob", "salary": 4200}]
5End---
💡 All matching rows updated and their new values returned by RETURNING clause.
Variable Tracker
VariableStartAfter Step 3Final
salary of Alice300035003500
salary of Bob370042004200
Key Moments - 2 Insights
Why do we see output rows after UPDATE when normally UPDATE returns only count?
Because of the RETURNING clause (see execution_table step 4), the updated rows are output instead of just a count.
Does RETURNING show old or new values?
RETURNING shows the new updated values after the change (execution_table step 4 shows updated salaries).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, how many rows matched the WHERE condition?
A2
B3
C0
D5
💡 Hint
Check the 'Rows Matched' column at step 2 in execution_table.
At which step does the salary column get updated?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'Columns Updated' column in execution_table.
If we remove RETURNING clause, what changes in the output?
ANo rows are updated
BUpdated rows are still returned
COnly count of updated rows is returned
DQuery will fail
💡 Hint
RETURNING clause controls output; without it, UPDATE returns count only.
Concept Snapshot
UPDATE table_name
SET column = value
WHERE condition
RETURNING columns;

- Updates rows matching condition
- RETURNING outputs updated rows
- Useful to see new values immediately
Full Transcript
The UPDATE with RETURNING clause in PostgreSQL first finds rows matching the WHERE condition. Then it updates the specified columns in those rows. After updating, the RETURNING clause outputs the updated rows with the columns requested. This lets you see the new values right away instead of just the count of updated rows. For example, updating salaries in the Sales department and returning id, name, and new salary shows exactly which rows changed and their new data.

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

  1. Step 1: Understand the purpose of UPDATE

    The UPDATE statement changes data in existing rows of a table.
  2. Step 2: Role of RETURNING clause

    The RETURNING clause immediately outputs the rows that were updated, showing their new values.
  3. Final Answer:

    It returns the updated rows immediately after the update. -> Option A
  4. 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

  1. Step 1: Identify correct UPDATE syntax

    The UPDATE statement must have SET clause before WHERE clause.
  2. Step 2: Position of RETURNING clause

    RETURNING clause comes after WHERE clause to output updated rows.
  3. Final Answer:

    UPDATE table_name SET column1 = value1 WHERE condition RETURNING *; -> Option A
  4. 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

  1. Step 1: Understand the UPDATE condition

    The query updates salary by adding 500 only for employees in the Sales department.
  2. Step 2: Effect of RETURNING clause

    RETURNING id, salary outputs the id and updated salary of only those employees updated.
  3. Final Answer:

    All employees' ids and new salaries in the Sales department after the raise. -> Option C
  4. 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

  1. Step 1: Check clause order in UPDATE

    UPDATE syntax requires SET before WHERE and RETURNING last.
  2. Step 2: Identify error in query

    RETURNING appears before SET, which is invalid and causes syntax error.
  3. Final Answer:

    The RETURNING clause is placed before SET, causing a syntax error. -> Option B
  4. 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

  1. Step 1: Understand the salary update

    Salary is multiplied by 1.1 to increase by 10%, so new salary is salary * 1.1.
  2. Step 2: Calculate old salary in RETURNING

    After update, salary column shows new salary. To get old salary, divide new salary by 1.1.
  3. Step 3: Check aliasing in RETURNING

    RETURNING must show id, new_salary (salary), and old_salary (salary / 1.1) with correct aliases.
  4. 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
  5. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard