Date Operations & Time-Based Analytics SQL Topic exerciseHardVerified answerSQLite + PostgreSQL + MySQL + SQL Server live · 1 guided

Days Between Consecutive Orders per Customer

Return every order with the previous order date and day gap within that customer.

  • Window functions
  • Date analysis
  • Type conversion
  • Sorting

Exercise brief

Understand the request

Lifecycle analytics engineer Reorder cadence analysis needs the elapsed days between each customer’s consecutive purchases.

Reorder cadence analysis needs the elapsed days between each customer’s consecutive purchases. Return every order with the previous order date and day gap within that customer.

Return

  • Return customer_id, order_id, order_date, prev_order_date, days_since_prev in this exact left-to-right order.

Constraints

  • Use LAG partitioned by customer_id.
  • Order the window by order_date and order_id.
  • Keep the first order with NULL previous date and gap.

Data you will use

Review the relevant tables before deciding how to join, filter, or aggregate them.

orders

  • order_idINTEGER
  • customer_idINTEGER
  • order_dateDATE

Hints, when you need them

Open one clue at a time so you still do the reasoning.

Hint 1

PARTITION BY customer_id so LAG only looks within one customer's history.

Hint 2

LAG(order_date) returns the previous row’s date; the first order per customer is NULL.

Hint 3

days_since_prev = day-difference between this order and the LAG value.

Verified SQL answer

Attempt the problem first, then compare structure and reasoning—not just syntax.

Reveal solution and explanation
SELECT customer_id, order_id, order_date, LAG(order_date) OVER (PARTITION BY customer_id ORDER BY order_date, order_id) AS prev_order_date, CAST(julianday(order_date) - julianday(LAG(order_date) OVER (PARTITION BY customer_id ORDER BY order_date, order_id)) AS INTEGER) AS days_since_prev FROM orders ORDER BY customer_id, order_date, order_id;

Why this works

Inter-purchase gap (recency cadence) drives reorder and retention models. The pattern is LAG partitioned by customer and ordered by date, then a day-difference between the current and previous date. The first order in each partition has no predecessor, so its gap is NULL. Only the day-difference syntax changes per engine.

Success check

Every order remains at order grain and gaps never cross customer boundaries.

Expected result

Use this output to verify values, aliases, ordering, and row count.

customer_idorder_idorder_dateprev_order_datedays_since_prev
112024-01-15NULLNULL
132024-02-102024-01-1526
172024-04-012024-02-1051
1122024-06-152024-04-0175
1172024-11-202024-06-15158
2202023-02-15NULLNULL
222024-01-202023-02-15339
252024-03-052024-01-2045
2102024-05-202024-03-0576
2152024-09-012024-05-20104

Previewing 10 of 20 expected rows. Run the query in the editor to inspect the full result.

Learn the concepts behind this answer

Strengthen your understanding with these targeted learning topics:

Continue practicing

SQL Practice Online

Open the interactive workspace and practice across SQL topics.