← Back to SQL, page 22

sql-0425 · Hard senior

Find the order at which each customer's cumulative spend first reached 40 or more.

Compute a running total of order value per customer ordered by date, then keep the first order where the running total is at least 40 and the previous running total was below 40 (or there was none). ROW_NUMBER over the qualifying orders, or a comparison of the running total minus the current order, identifies the crossing.

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
WITH order_totals AS (
  SELECT o.id, o.customer_id, o.order_date, SUM(oi.qty * oi.unit_price) AS value
  FROM orders o JOIN order_items oi ON oi.order_id = o.id WHERE o.customer_id IS NOT NULL GROUP BY o.id
), running AS (
  SELECT *, SUM(value) OVER (PARTITION BY customer_id ORDER BY order_date, id ROWS UNBOUNDED PRECEDING) AS cum FROM order_totals
)
SELECT customer_id, id AS crossing_order, order_date, cum FROM running
WHERE cum >= 40 AND cum - value < 40 ORDER BY customer_id;
customer_idcrossing_orderorder_datecum
11022026-02-1143
31042026-03-0142

Ana's first order is worth 13 and her second 30, so her cumulative spend reaches 43 at order 102. Chloe's single order 104 is worth 42 and crosses at once. Bilal's two orders total 35, so he never reaches 40 and does not appear. The test cum - value < 40 checks that the total before this order was below the threshold, which finds exactly one crossing row per customer without a second window.

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

WITH order_totals AS (
  SELECT o.id, o.customer_id, o.order_date, SUM(oi.qty * oi.unit_price) AS value
  FROM orders o JOIN order_items oi ON oi.order_id = o.id WHERE o.customer_id IS NOT NULL GROUP BY o.id
), running AS (
  SELECT *, SUM(value) OVER (PARTITION BY customer_id ORDER BY order_date, id ROWS UNBOUNDED PRECEDING) AS cum FROM order_totals
)
SELECT customer_id, id AS crossing_order, order_date, cum FROM running
WHERE cum >= 40 AND cum - value < 40 ORDER BY customer_id;

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • Aggregate order values first
  • Running SUM per customer
  • cum - value < threshold <= cum finds the crossing
  • Customers who never cross are absent

Common mistakes

  • Returning every order after the crossing
  • Running totals without a tiebreaker on equal dates

Follow-ups

  • How would you report how many days it took to cross?
  • How would you include customers who never crossed with NULL?

Tips

  • Show the crossing condition