Sub topic 9
Returning Columns from Joined Tables with `FROM` and `USING`
Assume you know basic SELECT, UPDATE, DELETE, table aliases, and PostgreSQL clients. This chapter shows how RETURNING can include values from tables joined during an UPDATE or DELETE.
`UPDATE ... FROM ... RETURNING`
UPDATE ... FROM updates rows in one target table while using another table to find matching data. The RETURNING clause can return columns from both the target table and the joined table.
CREATE TABLE customers (
id bigint PRIMARY KEY,
name text NOT NULL
);
CREATE TABLE orders (
id bigint PRIMARY KEY,
customer_id bigint NOT NULL REFERENCES customers(id),
status text NOT NULL,
total_cents integer NOT NULL
);
INSERT INTO customers (id, name)
VALUES (1, 'Ava'), (2, 'Noah');
INSERT INTO orders (id, customer_id, status, total_cents)
VALUES
(101, 1, 'pending', 2500),
(102, 2, 'pending', 4800);
UPDATE orders AS o
SET status = 'ready'
FROM customers AS c
WHERE o.customer_id = c.id
AND o.total_cents >= 3000
RETURNING
o.id AS order_id,
o.status,
c.name AS customer_name,
o.total_cents;The orders table is the target because it follows UPDATE. The customers table appears in the FROM clause and helps select matching rows. The returned result contains the updated order values and the customer's name.
The joined table is used to choose rows, but PostgreSQL still updates only the target table. It does not update customers.
Aliases matter
If you give the target table an alias, use that alias in RETURNING. In this example, write o.id, not orders.id.
UPDATE orders AS o
SET status = 'cancelled'
FROM customers AS c
WHERE o.customer_id = c.id
AND c.name = 'Ava'
RETURNING o.id, c.name, o.status;An alias gives the table a new name for the statement. Referring to the original name can produce a “missing FROM-clause entry” error.
`DELETE ... USING ... RETURNING`
DELETE ... USING has a similar purpose. It deletes rows from the target table after matching rows in one or more additional tables.
DELETE FROM orders AS o
USING customers AS c
WHERE o.customer_id = c.id
AND c.name = 'Noah'
RETURNING
o.id AS deleted_order_id,
o.status AS previous_status,
c.name AS customer_name;The returned row contains values from the deleted order and the customer row used by the join. This is useful when an application needs an audit event, a log entry, or information for an undo operation.
Avoiding accidental multiple matches
A target row should normally match at most one row in the joined table. If an UPDATE ... FROM target row matches several source rows, PostgreSQL updates the target row once, but which matching source row supplies returned values is not predictable.
For example, this is risky when customer_tags can contain duplicate rows:
UPDATE orders AS o
SET status = t.new_status
FROM order_status_rules AS t
WHERE o.status = t.old_status
RETURNING o.id, t.new_status;Make the source relation unique, or reduce it to one row per target before updating. A common approach is a subquery with DISTINCT ON, an aggregate, or a uniqueness constraint.
| Statement | Target table | Joined table syntax | What `RETURNING` can include |
|---|---|---|---|
| `UPDATE` | Table after `UPDATE` | `FROM` | Target and `FROM` columns |
| `DELETE` | Table after `DELETE FROM` | `USING` | Target and `USING` columns |
Exercise
Write a DELETE statement that deletes every order for customer ID 1 and returns the order ID and customer ID from the joined tables.
Show answer
DELETE FROM orders AS o USING customers AS c WHERE o.customerid = c.id AND c.id = 1 RETURNING o.id AS orderid, c.id AS customer_id;