UPDATE with RETURNING clause in PostgreSQL - Time & Space Complexity
When we update rows in a database, it takes time depending on how many rows we change.
We want to know how the time grows when we update more rows and use the RETURNING clause to get results back.
Analyze the time complexity of the following code snippet.
UPDATE employees
SET salary = salary * 1.1
WHERE department = 'Sales'
RETURNING id, salary;
This code updates salaries for all employees in the Sales department and returns their id and new salary.
- Primary operation: Scanning and updating each matching row in the employees table.
- How many times: Once for each employee in the Sales department.
As the number of employees in Sales grows, the update and returning work grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 updates and 10 rows returned |
| 100 | About 100 updates and 100 rows returned |
| 1000 | About 1000 updates and 1000 rows returned |
Pattern observation: The work grows directly with the number of rows updated and returned.
Time Complexity: O(n)
This means the time grows in a straight line with the number of rows updated and returned.
[X] Wrong: "The RETURNING clause does not affect the time because it just shows results."
[OK] Correct: Returning rows means the database must collect and send data for each updated row, adding to the work done.
Understanding how updates and returning results scale helps you explain database performance clearly and confidently.
"What if we removed the WHERE clause and updated all rows? How would the time complexity change?"
Practice
RETURNING clause do in a PostgreSQL UPDATE statement?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 AQuick Check:
RETURNING shows updated rows [OK]
- Thinking RETURNING deletes rows
- Assuming RETURNING creates backups
- Believing RETURNING stops the update
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 AQuick Check:
RETURNING follows WHERE in UPDATE [OK]
- Placing RETURNING before SET clause
- Using SHOW RETURNING which is invalid
- Putting RETURNING between SET and WHERE
employees with columns id, salary, and department, what will this query return?UPDATE employees SET salary = salary + 500 WHERE department = 'Sales' RETURNING id, salary;
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 CQuick Check:
RETURNING outputs updated rows only [OK]
- Thinking all rows are returned
- Expecting only count of updated rows
- Believing RETURNING cannot specify columns
UPDATE products RETURNING * SET price = price * 1.1 WHERE category = 'Books';
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 BQuick Check:
RETURNING must come after WHERE and SET [OK]
- Placing RETURNING before SET
- Omitting WHERE clause when needed
- Misordering clauses in UPDATE
id, old_salary, and new_salary. Which query correctly does this using UPDATE with RETURNING?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 DQuick Check:
Old salary = new salary / 1.1 [OK]
- Confusing old and new salary calculations
- Wrong aliasing in RETURNING clause
- Using expressions incorrectly in RETURNING
Ready to prove it?
Pass the test and this pattern moves unknown
