Finding User First Purchase, Repeat Purchase & Time-Between-Orders
Identify first/repeat purchases and measure days between orders.
In plain words
Number each customer's orders in date order; number 1 is their first purchase, the rest are repeats.
Think of it like…
A café's loyalty card: the first stamp marks the first visit, every later stamp is a repeat visit, and the dates between stamps show how often someone comes back.
Where you’ve seen it
A D2C skincare brand asks: "Of customers whose first order was in Q1, what share ordered again within 90 days, and what's the median time to the second order?" The query numbers valid orders per customer, takes order 1 and order 2, keeps customers whose first order is at least 90 days old, and reports a 31% repeat rate with a median of 38 days to the second order.
Words you’ll hear
- Order number
- ROW_NUMBER over each customer's orders by date; 1 = first purchase.
- Repeat purchase rate
- Customers with a second order ÷ customers with a first order, in a fixed window.
- Inter-order time
- Days between consecutive orders, from LAG or LEAD.
- Right-censoring
- Recent customers haven't had time to repeat yet, which biases rates downward.
Visual diagram
Theory & example queries
Numbering orders and measuring gaps
Filter to orders that count first: paid, not cancelled or fully refunded, not test accounts. Then number them per customer by time with a tie-breaker, and add LAG(created_at) OVER (PARTITION BY customer_id ORDER BY created_at, order_id) for the previous order's time. Days between orders is the difference, which is NULL for each customer's first order.
Those columns answer several questions directly. New vs returning revenue per month: SUM(amount) FILTER (WHERE order_number = 1) and FILTER (WHERE order_number > 1). Typical purchase cycle: PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY days_since_prev) over repeat orders. Customers who bought exactly once: order count = 1. Decide up front whether two orders on the same day are one purchase occasion or two.
| id | customer_id | status | amount | coupon_code |
|---|---|---|---|---|
| 101 | 1 | paid | 249 | NULL |
| 102 | 2 | paid | 120 | WELCOME10 |
| 103 | 1 | shipped | 89.5 | NULL |
WITH valid AS ( SELECT * FROM orders WHERE status = 'paid' AND NOT is_test ), numbered AS ( SELECT customer_id, order_id, created_at, amount, ROW_NUMBER() OVER w AS order_number, created_at::date - LAG(created_at::date) OVER w AS days_since_prev FROM valid WINDOW w AS ( PARTITION BY customer_id ORDER BY created_at, order_id ) )SELECT *FROM numbered;PostgreSQL is the lesson baseline unless noted otherwise.
Read this line together with the surrounding statement as part of the same SQL expression.
Expected output4 sample rows
| customer_id | order_id | created_at | amount | order_number | days_since_prev |
|---|---|---|---|---|---|
| 42 | 501 | 2026-01-05 09:00:00 | 450 | 1 | NULL |
| 42 | 520 | 2026-03-12 11:30:00 | 800 | 2 | 66 |
| 77 | 512 | 2026-02-01 15:00:00 | 120 | 1 | NULL |
| 77 | 540 | 2026-05-01 10:15:00 | 360 | 2 | 89 |
Note: Illustrative valid, paid, non-test orders numbered within each customer.
Key takeaway: Filter valid orders, ROW_NUMBER for position, LAG for the gap.
A fair repeat purchase rate
"Share of customers who ever ordered twice" punishes recent customers, who haven't had time to come back, so the rate seems to fall as the business grows. Use a fixed window from the first order, such as "second order within 90 days", and include only customers whose first order is at least 90 days before the data's end. Report it by first-order month so cohorts can be compared.
Pivot the first two orders per customer with conditional aggregation: MIN(created_at) FILTER (WHERE order_number = 1) and FILTER (WHERE order_number = 2). Then the repeat flag is second_at <= first_at + interval '90 days', and time to second order is their difference. Keep customers without a second order in the denominator; they're the ones who didn't come back.
WITH firsts AS ( SELECT customer_id, MIN(created_at) FILTER ( WHERE order_number = 1 ) AS first_at, MIN(created_at) FILTER ( WHERE order_number = 2 ) AS second_at FROM numbered GROUP BY customer_id )SELECT date_trunc('month', first_at) AS cohort, COUNT(*) AS customers, ROUND( 100.0 * COUNT(*) FILTER ( WHERE second_at <= first_at + interval '90 days' ) / COUNT(*), 1 ) AS repeat_90d_pctFROM firstsWHERE first_at <= now() - interval '90 days'GROUP BY 1ORDER BY 1;PostgreSQL is the lesson baseline unless noted otherwise.
Read this line together with the surrounding statement as part of the same SQL expression.
Expected output3 sample rows
| cohort | customers | repeat_90d_pct |
|---|---|---|
| 2026-01-01 | 520 | 31.5 |
| 2026-02-01 | 610 | 34.1 |
| 2026-03-01 | 575 | 29.7 |
Note: Illustrative mature cohorts whose first orders are at least 90 days old relative to the sample execution date.
Key takeaway: Fixed window from first order, only mature cohorts, non-repeaters in the denominator.
How it works, step by step
Follow one practical SQL workflow from start to finish. Select any step or play through the flow.
Define a valid order
Exclude cancelled, fully refunded and test orders, and decide how to treat same-day orders.
status = 'paid' AND NOT is_test
When to use it & trade-offs
What it is
Purchase-behaviour metrics start by numbering each customer's orders: ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY created_at, order_id). Order 1 is the first purchase, order 2 or later is a repeat purchase, and created_at - LAG(created_at) gives the time between orders. From those columns come the repeat purchase rate, time to second order, and new versus returning revenue.
Why it matters: Repeat customers drive most of the profit in e-commerce, subscriptions and food delivery, so growth teams track "what share of first-time buyers come back, and how fast?" constantly. Interviewers use these questions because they combine window functions with careful definitions: which orders count, and how to be fair to customers who only bought recently.
When to reach for it
- Repeat rate and time-to-second-order dashboards
- New vs returning revenue reports
- Timing retention emails
When to avoid or be careful
- "Ever repeated" rates compared across months
- Counting refunded or test orders
- Dropping one-time buyers from the denominator
Ways to measure repeat purchasing
Strengths: Fair across old and new customers.
Watch out: Recent cohorts can't be reported until N days pass.
Strengths: Simple to compute.
Watch out: Biased against recent customers; falls as you grow.
Strengths: Describes the purchase cycle directly.
Watch out: Says nothing about customers who never return.
Interview signals & numbers to know
When this topic comes up
- “What percentage of customers buy again?”
- “Average time between orders”
- “Revenue from new vs returning customers”
- Customer lifecycle metrics
- E-commerce analytics
Benchmarks & rules of thumb
- 1
- order_number of a first purchase
- 90 days
- a common repeat window
- NULL
- days_since_prev on each customer's first order
Common mistakes & follow-ups
Common interview mistakes
- Computing repeat rate over all customers including last week's first-time buyers, making it look like retention is collapsing.
- Ordering by created_at alone, so two orders at the same second swap first and second between runs.
- Using AVG days between orders, which a few customers with year-long gaps pull far above the typical cycle.
Likely interviewer follow-ups
- How would you calculate a 90-day repeat purchase rate by month of first order?
- How do you split monthly revenue into new and returning customers?
- Why use the median rather than the average time between orders?
Knowledge check
Put it into practice
Practice related interview Q&A
Apply Finding User First Purchase, Repeat Purchase & Time-Between-Orders with hands-on SQL practice and interview Q&A.