Home / SQL / Practice problems / Hard

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
Show the expected result
order_datedays_since_previous
2025-02-27NULL
2025-03-2526
2025-05-1753
2025-06-1024
2025-10-26138

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.