Sub topic 11
Using EXISTS in UPDATE and DELETE
Assume you know SELECT, WHERE, INNER JOIN, LEFT JOIN, table aliases, aggregate functions, and NULL. This chapter applies the same correlated-subquery idea to data-changing statements.
EXISTS can restrict which rows an UPDATE or DELETE changes. The inner query checks related rows for the current row in the statement being modified.
Updating rows that have a related row
Suppose customers contains customers and orders contains their orders. You want to mark every customer who has placed at least one order as active:
UPDATE customers
SET status = 'active'
WHERE EXISTS (
SELECT 1
FROM orders o
WHERE o.customer_id = customers.customer_id
);The reference to customers.customer_id connects the inner query to the current customer. For each customer, the database checks whether at least one matching order exists. Only customers for whom the condition is true are updated.
The SELECT 1 value is conventional. EXISTS checks whether a row exists, not what value the inner query selects.
You can add conditions inside the subquery. This example activates only customers with an order placed during 2026:
UPDATE customers
SET status = 'active'
WHERE EXISTS (
SELECT 1
FROM orders o
WHERE o.customer_id = customers.customer_id
AND o.order_date >= DATE '2026-01-01'
AND o.order_date < DATE '2027-01-01'
);The date literal syntax shown here is supported by many SQL databases. Some systems use different date-literal syntax.
Deleting rows with no related rows
NOT EXISTS is useful for finding rows that have no match. This statement deletes customers who have never placed an order:
DELETE FROM customers
WHERE NOT EXISTS (
SELECT 1
FROM orders o
WHERE o.customer_id = customers.customer_id
);The inner query is correlated in the same way as the UPDATE. The difference is that NOT EXISTS is true only when the inner query returns no rows.
A foreign-key constraint may prevent this deletion if another table refers to the customer. A database may reject the statement, or the constraint may be configured to delete related rows automatically. Check the schema before deleting.
For example, preview the customers selected by the deletion:
SELECT customers.customer_id, customers.name
FROM customers
WHERE NOT EXISTS (
SELECT 1
FROM orders o
WHERE o.customer_id = customers.customer_id
);Then run the DELETE only after checking the result.
A safe workflow
Use a transaction when your database supports it:
BEGIN;
SELECT customers.customer_id, customers.name
FROM customers
WHERE NOT EXISTS (
SELECT 1
FROM orders o
WHERE o.customer_id = customers.customer_id
);
DELETE FROM customers
WHERE NOT EXISTS (
SELECT 1
FROM orders o
WHERE o.customer_id = customers.customer_id
);
-- Inspect the result, then choose one:
-- COMMIT;
-- ROLLBACK;COMMIT makes the changes permanent. ROLLBACK cancels the changes made in the transaction. The exact transaction commands can differ between database systems.
The important safety property is that the preview and the data-changing statement share the same predicate. If you change one condition but not the other, the preview may no longer describe the rows that will change.
How the correlation controls the target rows
The database can evaluate this logically one customer at a time, although the optimizer may transform the statement into a more efficient plan.
Common mistakes
| Mistake | Result | Safer approach |
|---|---|---|
| Omitting the correlation condition | Every customer may match the same order | Connect the inner key to the target key |
| Using `NOT IN` with a nullable column | `NULL` can make the predicate UNKNOWN | Prefer `NOT EXISTS` |
| Skipping the preview query | Unexpected rows may change | Run a matching `SELECT` first |
| Forgetting transaction control | A mistake may be difficult to undo | Use `BEGIN`, inspect, then commit or roll back |
Some databases allow an alias for the target table in UPDATE or DELETE; others use different syntax. Follow the syntax of your database while preserving the same correlation logic.
Exercise
Write a DELETE statement that removes products for which no row exists in orderitems with the same productid.
Show answer
DELETE FROM products WHERE NOT EXISTS ( SELECT 1 FROM orderitems oi WHERE oi.productid = products.product_id );