Full answer
sql
CREATE TABLE customers (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
country TEXT
);
CREATE TABLE orders (
id INTEGER PRIMARY KEY,
customer_id INTEGER REFERENCES customers(id),
order_date TEXT NOT NULL,
status TEXT NOT NULL
);
CREATE TABLE order_items (
order_id INTEGER REFERENCES orders(id),
product TEXT NOT NULL,
qty INTEGER NOT NULL,
unit_price INTEGER NOT NULL
);
INSERT INTO customers VALUES (1,'Ana','Spain'),(2,'Bilal','UAE'),(3,'Chloe','France'),(4,'Dev','India');
INSERT INTO orders VALUES
(101,1,'2026-01-05','shipped'),(102,1,'2026-02-11','shipped'),
(103,2,'2026-02-14','cancelled'),(104,3,'2026-03-01','shipped'),
(105,2,'2026-03-09','pending'),(106,NULL,'2026-03-12','shipped');
INSERT INTO order_items VALUES
(101,'Notebook',2,4),(101,'Pen',5,1),(102,'Bag',1,30),(103,'Pen',10,1),
(104,'Notebook',3,4),(104,'Bag',1,30),(105,'Lamp',1,25),(106,'Pen',2,1);sql
UPDATE orders SET status = 'vip'
FROM customers c
WHERE c.id = orders.customer_id AND c.country = 'Spain';
SELECT id, customer_id, status FROM orders ORDER BY id;| id | customer_id | status |
|---|---|---|
| 101 | 1 | vip |
| 102 | 1 | vip |
| 103 | 2 | cancelled |
| 104 | 3 | shipped |
| 105 | 2 | pending |
| 106 | NULL | shipped |
Ana's two orders changed; the rest did not. The FROM clause joins the other table, and the WHERE carries both the join condition and the filter.
The subquery form works on every engine and older SQLite versions:
sql
UPDATE orders SET status = 'vip'
WHERE customer_id IN (SELECT id FROM customers WHERE country = 'Spain');
SELECT COUNT(*) AS vip_orders FROM orders WHERE status = 'vip';| vip_orders |
|---|
| 2 |
With UPDATE ... FROM, if one target row matches several rows in the joined table, the row is updated once with an arbitrary match. When the new value comes from the joined table, make sure the join is one-to-one, or aggregate first. MySQL uses UPDATE orders JOIN customers ... syntax instead (not run here).
sql
CREATE TABLE customers (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
country TEXT
);
CREATE TABLE orders (
id INTEGER PRIMARY KEY,
customer_id INTEGER REFERENCES customers(id),
order_date TEXT NOT NULL,
status TEXT NOT NULL
);
CREATE TABLE order_items (
order_id INTEGER REFERENCES orders(id),
product TEXT NOT NULL,
qty INTEGER NOT NULL,
unit_price INTEGER NOT NULL
);
INSERT INTO customers VALUES (1,'Ana','Spain'),(2,'Bilal','UAE'),(3,'Chloe','France'),(4,'Dev','India');
INSERT INTO orders VALUES
(101,1,'2026-01-05','shipped'),(102,1,'2026-02-11','shipped'),
(103,2,'2026-02-14','cancelled'),(104,3,'2026-03-01','shipped'),
(105,2,'2026-03-09','pending'),(106,NULL,'2026-03-12','shipped');
INSERT INTO order_items VALUES
(101,'Notebook',2,4),(101,'Pen',5,1),(102,'Bag',1,30),(103,'Pen',10,1),
(104,'Notebook',3,4),(104,'Bag',1,30),(105,'Lamp',1,25),(106,'Pen',2,1);
UPDATE orders SET status = 'vip'
FROM customers c
WHERE c.id = orders.customer_id AND c.country = 'Spain';
SELECT id, customer_id, status FROM orders ORDER BY id;
UPDATE orders SET status = 'vip'
WHERE customer_id IN (SELECT id FROM customers WHERE country = 'Spain');
SELECT COUNT(*) AS vip_orders FROM orders WHERE status = 'vip';Checked on SQLite 3.46.1, run twice with identical output.
Key points
- UPDATE ... FROM joins another table into an update
- Supported in PostgreSQL and SQLite 3.33+
- WHERE ... IN (subquery) is the portable form
- Multiple matches per target row give an arbitrary value
Common mistakes
- Forgetting the join condition and updating every row
- Joining one-to-many and getting unpredictable values
Follow-ups
- How would you set each order's total from its order lines?
- How would you preview which rows will change?
Tips
- Run the same WHERE as a SELECT first