Example 1
Delete a row and return the removed values
The DELETE removes Widget from the inventory table and returns the removed row in the same response. PostgreSQL, MariaDB, and SQLite use RETURNING product_id, product_name, stock. SQL Server's OUTPUT DELETED.col syntax achieves the same result using the DELETED pseudo-table, which holds the row state before deletion.
CREATE TABLE inventory (
product_id INT,
product_name VARCHAR(50),
stock INT
);
INSERT INTO
inventory
VALUES
(1, 'Widget', 100),
(2, 'Gadget', 50),
(3, 'Doohickey', 75);DELETE FROM inventory
WHERE
product_id = 1 RETURNING product_id,
product_name,
stock;| product_id | product_name | stock |
|---|---|---|
| 1 | Widget | 100 |
CREATE TABLE inventory (
product_id INT,
product_name VARCHAR(50),
stock INT
);
INSERT INTO
inventory
VALUES
(1, 'Widget', 100),
(2, 'Gadget', 50),
(3, 'Doohickey', 75);DELETE FROM inventory OUTPUT DELETED.product_id,
DELETED.product_name,
DELETED.stock
WHERE
product_id = 1;| product_id | product_name | stock |
|---|---|---|
| 1 | Widget | 100 |
PostgreSQL, MariaDB, and SQLite use `RETURNING`. SQL Server uses `OUTPUT INSERTED.*`. MySQL has no equivalent.