Example 1
Mark orders as shipped for all customers based in the US
The subquery returns customer IDs 1 and 3 (Alice and Carol, both in the US). The UPDATE sets status = 'shipped' for orders 1 and 3, which belong to those customers. Order 2 (Bob, Canada) is untouched and remains pending.
CREATE TABLE customers (id INT, name VARCHAR(50), country VARCHAR(10));
INSERT INTO
customers
VALUES
(1, 'Alice', 'US'),
(2, 'Bob', 'CA'),
(3, 'Carol', 'US');
CREATE TABLE orders (id INT, customer_id INT, status VARCHAR(20));
INSERT INTO
orders
VALUES
(1, 1, 'pending'),
(2, 2, 'pending'),
(3, 3, 'pending');UPDATE orders
SET
status = 'shipped'
WHERE
customer_id IN (
SELECT
id
FROM
customers
WHERE
country = 'US'
);
SELECT
id,
status
FROM
orders
ORDER BY
id;| id | status |
|---|---|
| 1 | shipped |
| 2 | pending |
| 3 | shipped |
Identical syntax and result across all engines.