The RETURNING clause lets you see the rows that were changed by an INSERT, UPDATE, or DELETE right away. This helps you confirm what happened without running another query.
Returning modified rows with RETURNING in PostgreSQL
INSERT INTO table_name (column1, column2) VALUES (value1, value2) RETURNING *; UPDATE table_name SET column1 = value1 WHERE condition RETURNING *; DELETE FROM table_name WHERE condition RETURNING *;
The RETURNING clause goes at the end of your INSERT, UPDATE, or DELETE statement.
Using RETURNING * returns all columns of the affected rows, but you can specify specific columns instead.
INSERT INTO employees (name, role) VALUES ('Alice', 'Engineer') RETURNING id, name;
UPDATE employees SET role = 'Senior Engineer' WHERE name = 'Alice' RETURNING *;
DELETE FROM employees WHERE name = 'Alice' RETURNING id, name;
This example creates a temporary employees table, inserts a row, updates it, and then deletes it. Each step returns the affected rows so you can see the changes immediately.
CREATE TEMP TABLE employees (id SERIAL PRIMARY KEY, name TEXT, role TEXT); INSERT INTO employees (name, role) VALUES ('Bob', 'Developer') RETURNING id, name, role; UPDATE employees SET role = 'Lead Developer' WHERE name = 'Bob' RETURNING *; DELETE FROM employees WHERE name = 'Bob' RETURNING id, name;
RETURNING is specific to PostgreSQL and some other databases; not all SQL databases support it.
Using RETURNING can save time and reduce errors by avoiding extra SELECT queries.
RETURNING shows you the rows changed by INSERT, UPDATE, or DELETE immediately.
You can return all columns with RETURNING * or select specific columns.
This helps confirm changes and avoid extra queries.
Practice
RETURNING clause do in a PostgreSQL UPDATE statement?Solution
Step 1: Understand the purpose of RETURNING
The RETURNING clause is used to get the data of rows affected by an INSERT, UPDATE, or DELETE immediately.Step 2: Apply to UPDATE statement
When used with UPDATE, RETURNING shows the updated rows without needing a separate SELECT query.Final Answer:
It returns the rows that were updated immediately after the update. -> Option CQuick Check:
RETURNING shows changed rows = B [OK]
- Thinking RETURNING delays changes
- Assuming RETURNING prevents updates
- Confusing RETURNING with row count only
Solution
Step 1: Recall correct RETURNING syntax
PostgreSQL uses RETURNING followed by column names or * to return all columns.Step 2: Identify correct option
Only UPDATE table_name SET col = val RETURNING *; uses RETURNING * which is valid syntax to return all columns.Final Answer:
UPDATE table_name SET col = val RETURNING *; -> Option BQuick Check:
RETURNING * returns all columns [OK]
- Using RETURN instead of RETURNING
- Writing RETURNING ALL instead of RETURNING *
- Adding extra keywords like ALL COLUMNS
users(id INT, name TEXT, age INT) with rows:(1, 'Alice', 30), (2, 'Bob', 25)What will this query return?
UPDATE users SET age = age + 1 WHERE id = 2 RETURNING id, age;
Solution
Step 1: Identify updated row
The WHERE clause updates only the row with id = 2 (Bob).Step 2: Calculate new age and returned columns
Bob's age increases from 25 to 26. RETURNING id, age returns (2, 26).Final Answer:
(2, 26) -> Option AQuick Check:
Updated row with id=2 returns new age 26 [OK]
- Returning old values instead of updated
- Confusing which row is updated
- Ignoring WHERE clause effect
UPDATE products SET price = price * 1.1 RETURNING;
Solution
Step 1: Check RETURNING syntax
RETURNING must be followed by column names or *; empty RETURNING is invalid syntax.Step 2: Validate other parts
Multiplication and UPDATE without WHERE are valid; RETURNING with no columns is the error.Final Answer:
RETURNING clause requires columns or * to specify what to return. -> Option DQuick Check:
RETURNING needs columns or * [OK]
- Leaving RETURNING empty
- Thinking WHERE is mandatory for syntax
- Believing RETURNING disallowed in UPDATE
id and new salary. Which query correctly does this and returns the updated rows?Solution
Step 1: Identify correct UPDATE with WHERE and RETURNING
The query must update only department 5 employees and return id and salary.Step 2: Check syntax correctness
UPDATE employees SET salary = salary * 1.1 WHERE department_id = 5 RETURNING id, salary; uses correct syntax: UPDATE ... SET ... WHERE ... RETURNING id, salary;Step 3: Eliminate incorrect options
Adding a fixed 10 instead of multiplying by 1.1 doesn't implement a 10% increase. Placing RETURNING before WHERE is invalid syntax. Using RETURN instead of RETURNING is wrong.Final Answer:
UPDATE employees SET salary = salary * 1.1 WHERE department_id = 5 RETURNING id, salary; -> Option AQuick Check:
Correct syntax with WHERE and RETURNING columns [OK]
- Placing RETURNING before WHERE
- Using RETURN instead of RETURNING
- Adding fixed amount instead of percentage
Ready to prove it?
Pass the test and this pattern moves unknown
