← Back to SQL, page 15

sql-0284 · Medium mid

Write an UPDATE that stores each customer's latest order date in a last_order_date column.

Use a correlated subquery in SET: UPDATE customers SET last_order_date = (SELECT MAX(order_date) FROM orders o WHERE o.customer_id = customers.id). Customers with no orders get NULL, because MAX over no rows is NULL. Add a WHERE EXISTS if those rows should stay untouched.

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
ALTER TABLE customers ADD COLUMN last_order_date TEXT;
UPDATE customers SET last_order_date = (SELECT MAX(order_date) FROM orders o WHERE o.customer_id = customers.id);
SELECT name, last_order_date FROM customers ORDER BY id;
namelast_order_date
Ana2026-02-11
Bilal2026-03-09
Chloe2026-03-01
DevNULL

Each customer row runs the subquery for its own id. Dev has no orders, so his value is NULL.

UPDATE ... FROM with a pre-aggregated subquery computes all maxima in one pass, which helps on large tables:

sql
UPDATE customers SET last_order_date = m.d
FROM (SELECT customer_id, MAX(order_date) AS d FROM orders GROUP BY customer_id) m
WHERE m.customer_id = customers.id;
SELECT COUNT(last_order_date) AS customers_with_date FROM customers;
customers_with_date
3

Stored aggregates like this go stale as new orders arrive. Either recompute on a schedule, update it in the code path that creates orders, or use a trigger, and decide whether cancelled orders count.

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);

ALTER TABLE customers ADD COLUMN last_order_date TEXT;
UPDATE customers SET last_order_date = (SELECT MAX(order_date) FROM orders o WHERE o.customer_id = customers.id);
SELECT name, last_order_date FROM customers ORDER BY id;

UPDATE customers SET last_order_date = m.d
FROM (SELECT customer_id, MAX(order_date) AS d FROM orders GROUP BY customer_id) m
WHERE m.customer_id = customers.id;
SELECT COUNT(last_order_date) AS customers_with_date FROM customers;

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • Correlated subqueries can supply values in SET
  • No matching rows yields NULL
  • UPDATE ... FROM with a grouped subquery is a one-pass alternative
  • Stored aggregates need a refresh strategy

Common mistakes

  • Forgetting the correlation and setting one global value
  • Letting the stored value drift

Follow-ups

  • How would you keep the column current?
  • Should cancelled orders count?

Tips

  • Mention staleness