Sub topic 4
Deleting Based on Another Table with `DELETE ... USING`
This chapter assumes that you can write SELECT statements with WHERE and INNER JOIN, use table aliases, and run single-table DELETE statements in PostgreSQL.
A DELETE statement normally chooses rows from one table:
DELETE FROM orders
WHERE status = 'cancelled';Sometimes the condition depends on another table. For example, you may want to delete orders belonging to customers whose accounts are closed. PostgreSQL provides USING for this purpose. USING adds tables that participate in the row-selection join.
The basic pattern
The general form is:
DELETE FROM target_table AS target
USING other_table AS other
WHERE join_condition
AND additional_condition;The table after DELETE FROM is the only table from which rows are removed. Tables after USING help decide which target rows match.
For example:
DELETE FROM orders AS o
USING customers AS c
WHERE o.customer_id = c.id
AND c.status = 'closed';This statement works as follows:
- PostgreSQL matches each order to its customer.
- It keeps matches where the customer status is
closed. - It deletes the matching rows from
orders. - It does not delete rows from
customers.
The join condition belongs in WHERE. Unlike a SELECT, DELETE ... USING does not have a separate ON clause.
Aliases and filters
Aliases make the roles clear. PostgreSQL requires you to use the target alias after defining one:
DELETE FROM orders AS o
USING customers AS c
WHERE o.customer_id = c.id
AND c.status = 'closed'
AND o.status = 'open';Here, only open orders for closed customers are deleted. Qualifying columns with aliases also prevents ambiguity when both tables have a column with the same name.
| Clause | Purpose |
|---|---|
| `DELETE FROM orders AS o` | Identifies the table and rows that can be removed |
| `USING customers AS c` | Adds a table used to find matching rows |
| `o.customer_id = c.id` | Connects each order to its customer |
| `c.status = 'closed'` | Filters the related customers |
| `o.status = 'open'` | Filters the target orders |
The missing-condition mistake
Every table in USING must connect to the target through a condition. If you omit the join condition, PostgreSQL forms a Cartesian product. A target row can then match unrelated rows in the USING table.
-- Dangerous: there is no condition connecting orders and customers
DELETE FROM orders AS o
USING customers AS c
WHERE c.status = 'closed';If at least one customer is closed, this can delete every order. Always inspect the matching rows before executing a bulk delete.
A USING clause is useful when the deletion depends on columns from a related table. If you only need to test whether a related row exists, EXISTS can express the intent directly:
DELETE FROM orders AS o
WHERE EXISTS (
SELECT 1
FROM customers AS c
WHERE c.id = o.customer_id
AND c.status = 'closed'
);Both forms can be correct. Use USING for a readable join-style operation and EXISTS when existence is the main idea.
Exercise
Write a DELETE ... USING statement that deletes open orders belonging to customers whose status is 'closed'.
Show answer
DELETE FROM orders AS o USING customers AS c WHERE o.customer_id = c.id AND c.status = 'closed' AND o.status = 'open';