Introduction
The DELETE statement removes rows from a table. The RETURNING clause lets you see which rows were deleted right away.
The DELETE statement removes rows from a table. The RETURNING clause lets you see which rows were deleted right away.
DELETE FROM table_name WHERE condition RETURNING column1, column2, ...;
DELETE FROM employees WHERE id = 5 RETURNING name, department;
DELETE FROM sessions WHERE expired = true RETURNING *;
This example creates a temporary users table, inserts three users, then deletes the inactive ones. It returns the id and name of deleted users.
CREATE TEMP TABLE users (id SERIAL PRIMARY KEY, name TEXT, active BOOLEAN); INSERT INTO users (name, active) VALUES ('Alice', true), ('Bob', false), ('Carol', false); DELETE FROM users WHERE active = false RETURNING id, name;
RETURNING is specific to PostgreSQL and some other databases; not all support it.
It helps avoid running a separate SELECT after DELETE to know what was removed.
DELETE removes rows from a table based on a condition.
RETURNING shows which rows were deleted immediately.
Use RETURNING * to get all columns of deleted rows.
RETURNING clause do in a DELETE statement in PostgreSQL?users where age < 18 and return all deleted rows?products with rows:{id:1, name:'Pen', stock:10}, {id:2, name:'Pencil', stock:0}, {id:3, name:'Eraser', stock:5}DELETE FROM products WHERE stock = 0 RETURNING id, name;
DELETE FROM orders RETURNING * WHERE status = 'cancelled';
staff who have salary < 30000 and return their id and name. Which query correctly does this and also returns the count of deleted rows?Pass the test and this pattern moves unknown