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 customer_id, id, order_date,
CAST(julianday(order_date) - julianday(LAG(order_date) OVER w) AS INTEGER) AS days_since_prev,
AVG(julianday(order_date) - julianday(LAG(order_date) OVER w)) OVER (PARTITION BY customer_id) AS avg_gap
FROM orders WHERE customer_id IS NOT NULL
WINDOW w AS (PARTITION BY customer_id ORDER BY order_date)
ORDER BY customer_id, order_date;
-- error on purposetext
Error: misuse of window function LAG()SQLite does not allow a window function inside another window function's argument, so this attempt fails, which is a useful lesson in itself. The fix is to compute the gaps in one step and average them in the next:
sql
WITH gaps AS (
SELECT customer_id, id, order_date,
julianday(order_date) - julianday(LAG(order_date) OVER (PARTITION BY customer_id ORDER BY order_date)) AS gap
FROM orders WHERE customer_id IS NOT NULL
)
SELECT customer_id, id, order_date, CAST(gap AS INTEGER) AS days_since_prev, AVG(gap) OVER (PARTITION BY customer_id) AS avg_gap
FROM gaps ORDER BY customer_id, order_date;| customer_id | id | order_date | days_since_prev | avg_gap |
|---|---|---|---|---|
| 1 | 101 | 2026-01-05 | NULL | 37.0 |
| 1 | 102 | 2026-02-11 | 37 | 37.0 |
| 2 | 103 | 2026-02-14 | NULL | 23.0 |
| 2 | 105 | 2026-03-09 | 23 | 23.0 |
| 3 | 104 | 2026-03-01 | NULL | NULL |
Ana's two orders are 37 days apart and Bilal's are 23 days apart, while Chloe has a single order and no gap. AVG ignores the NULL gap on each first order, so the averages are 37.0 and 23.0. In PostgreSQL, subtracting two dates gives an integer number of days 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);
-- error on purpose
SELECT customer_id, id, order_date,
CAST(julianday(order_date) - julianday(LAG(order_date) OVER w) AS INTEGER) AS days_since_prev,
AVG(julianday(order_date) - julianday(LAG(order_date) OVER w)) OVER (PARTITION BY customer_id) AS avg_gap
FROM orders WHERE customer_id IS NOT NULL
WINDOW w AS (PARTITION BY customer_id ORDER BY order_date)
ORDER BY customer_id, order_date;
WITH gaps AS (
SELECT customer_id, id, order_date,
julianday(order_date) - julianday(LAG(order_date) OVER (PARTITION BY customer_id ORDER BY order_date)) AS gap
FROM orders WHERE customer_id IS NOT NULL
)
SELECT customer_id, id, order_date, CAST(gap AS INTEGER) AS days_since_prev, AVG(gap) OVER (PARTITION BY customer_id) AS avg_gap
FROM gaps ORDER BY customer_id, order_date;Checked on SQLite 3.46.1, run twice with identical output.
Key points
- LAG over customer partitions finds the previous order
- julianday differences give days in SQLite
- Window functions cannot nest
- Compute gaps first, then aggregate
Common mistakes
- Nesting window functions
- Counting the first order's NULL gap as zero
Follow-ups
- How would you flag customers whose gap is growing?
- How would you predict the next order date?
Tips
- Split into two steps