← Back to SQL, page 9

sql-0162 · Medium mid

Write a query that lists customers whose most recent order is more than 30 days before 2026-03-15.

Group orders by customer, take MAX(order_date) as the last order, and keep groups where it is earlier than date('2026-03-15', '-30 days'). Compare against a computed cut-off rather than computing a difference per row, so the condition stays simple and comparable.

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
SELECT c.name, MAX(o.order_date) AS last_order
FROM customers c JOIN orders o ON o.customer_id = c.id
GROUP BY c.id, c.name
HAVING MAX(o.order_date) < date('2026-03-15', '-30 days')
ORDER BY c.name;
namelast_order
Ana2026-02-11

The cut-off is 2026-02-13. Ana's latest order is 11 February, so she qualifies. Bilal ordered on 9 March and Chloe on 1 March, so both are recent.

Two choices to discuss with the business: whether cancelled orders count as activity (here they do), and whether customers who never ordered should appear. Dev never ordered; add a LEFT JOIN and OR MAX(o.order_date) IS NULL to include such customers.

In production, the reference date would be date('now') or CURRENT_DATE, passed in as a parameter for testable queries.

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, MAX(o.order_date) AS last_order
FROM customers c JOIN orders o ON o.customer_id = c.id
GROUP BY c.id, c.name
HAVING MAX(o.order_date) < date('2026-03-15', '-30 days')
ORDER BY c.name;

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • MAX(order_date) per customer gives the last order
  • Compare against a precomputed cut-off date
  • Decide whether cancelled orders count
  • Handle never-ordered customers explicitly

Common mistakes

  • Filtering individual orders in WHERE instead of the latest per customer
  • Hard-coding today's date and making tests flaky

Follow-ups

  • How would you list customers with no orders in the last 30 days, including those who never ordered?
  • How would you compute a churn rate per month?

Tips

  • Ask whether cancelled orders count as activity