Days between a customer's orders
Hardrow order matters
For customer 1, show each order_date and days_since_previous (whole days since their previous order; NULL for the first), in date order.
Tables: customers(customer_id, name, city, country, joined) · products(product_id, name, category, price) · orders(order_id, customer_id, order_date, status) · order_items(order_id, product_id, quantity, unit_price)
Your query✔ Solved
Show the expected result
| order_date | days_since_previous |
|---|---|
| 2025-02-27 | NULL |
| 2025-03-25 | 26 |
| 2025-05-17 | 53 |
| 2025-06-10 | 24 |
| 2025-10-26 | 138 |
5 rows
Show a solution
SELECT order_date, CAST(julianday(order_date) - julianday(LAG(order_date) OVER (ORDER BY order_date)) AS INTEGER) AS days_since_previous FROM orders WHERE customer_id = 1 ORDER BY order_date;
SQL used
New to SQL? The free SQL course teaches everything these problems use, with a self-checking exercise in every lesson.