← Back to SQL, page 5

sql-0097 · Medium mid

Write a query that returns each customer's most recent order using a correlated subquery.

Keep an order only if its date equals the maximum order date for the same customer: WHERE o.order_date = (SELECT MAX(o2.order_date) FROM orders o2 WHERE o2.customer_id = o.customer_id). If a customer has two orders on the latest date, both are returned, so add a tie-breaker if exactly one is required.

Full answer

"Latest row per group" is one of the most common interview patterns. A correlated subquery expresses it directly.

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
SELECT c.name, o.id, o.order_date
FROM orders o
JOIN customers c ON c.id = o.customer_id
WHERE o.order_date = (SELECT MAX(o2.order_date)
                      FROM orders o2
                      WHERE o2.customer_id = o.customer_id)
ORDER BY c.name;
nameidorder_date
Ana1022026-02-11
Bilal1052026-03-09
Chloe1042026-03-01

For each order, the subquery finds the newest date among orders of the same customer, and the order is kept only if it matches. Ana's January order is dropped; her February order is kept.

Ties need a decision. If Ana had two orders on 2026-02-11, both would be returned. When the business wants exactly one, compare on a unique key instead, such as the highest id among the latest-date orders:

sql
SELECT c.name, o.id, o.order_date
FROM orders o
JOIN customers c ON c.id = o.customer_id
WHERE o.id = (SELECT o2.id
              FROM orders o2
              WHERE o2.customer_id = o.customer_id
              ORDER BY o2.order_date DESC, o2.id DESC
              LIMIT 1)
ORDER BY c.name;
nameidorder_date
Ana1022026-02-11
Bilal1052026-03-09
Chloe1042026-03-01

Order 106 has no customer and drops out of the join with customers, which is correct for a per-customer report.

For large tables, an index on (customer_id, order_date) lets each subquery jump straight to the newest order for that customer. Window functions (ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY order_date DESC, id DESC)) are the other common answer and handle ties in the same explicit way.

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

SELECT c.name, o.id, o.order_date
FROM orders o
JOIN customers c ON c.id = o.customer_id
WHERE o.order_date = (SELECT MAX(o2.order_date)
                      FROM orders o2
                      WHERE o2.customer_id = o.customer_id)
ORDER BY c.name;

SELECT c.name, o.id, o.order_date
FROM orders o
JOIN customers c ON c.id = o.customer_id
WHERE o.id = (SELECT o2.id
              FROM orders o2
              WHERE o2.customer_id = o.customer_id
              ORDER BY o2.order_date DESC, o2.id DESC
              LIMIT 1)
ORDER BY c.name;

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • Correlated MAX per group finds the latest row
  • Ties on the max value return several rows
  • Use a unique key or a deterministic ORDER BY ... LIMIT 1 for exactly one
  • An index on (group, date) makes each lookup cheap

Common mistakes

  • Using GROUP BY customer with MAX(date) and then joining back on date only, which duplicates ties
  • Selecting other columns next to MAX() and getting values from a different row

Follow-ups

  • Rewrite this with ROW_NUMBER().
  • How would you get the latest two orders per customer?

Tips

  • Mention how ties are handled before the interviewer asks