Introduction
The UPDATE statement changes data in a table. The RETURNING clause shows the changed rows right away.
The UPDATE statement changes data in a table. The RETURNING clause shows the changed rows right away.
UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition RETURNING column1, column2, ...;
UPDATE employees SET salary = salary * 1.1 WHERE department = 'Sales' RETURNING id, salary;
UPDATE products SET stock = stock - 1 WHERE id = 101 RETURNING id, stock;
UPDATE tasks SET status = 'completed' RETURNING id, status;
This creates a users table, adds three users, then increases Bob's age by 1 and returns his id, name, and new age.
CREATE TABLE users ( id SERIAL PRIMARY KEY, name TEXT, age INT ); INSERT INTO users (name, age) VALUES ('Alice', 30), ('Bob', 25), ('Carol', 27); UPDATE users SET age = age + 1 WHERE name = 'Bob' RETURNING id, name, age;
RETURNING helps avoid running a separate SELECT after UPDATE.
You can return all columns with RETURNING * if you want.
Be careful with UPDATE without WHERE; it updates all rows.
UPDATE changes data in a table.
RETURNING shows updated rows immediately.
This saves time by combining update and fetch in one step.
RETURNING clause do in a PostgreSQL UPDATE statement?employees with columns id, salary, and department, what will this query return?UPDATE employees SET salary = salary + 500 WHERE department = 'Sales' RETURNING id, salary;
UPDATE products RETURNING * SET price = price * 1.1 WHERE category = 'Books';
id, old_salary, and new_salary. Which query correctly does this using UPDATE with RETURNING?Pass the test and this pattern moves unknown