Skip to content

Rolling retention after day 30 by first-order quarter

The task

The store wants to know how many new customers order again after their first month. This is "rolling" retention: a customer counts if they are active on day 30 or at any time after it.

Rules:

  • Only completed orders count, both for the first order and for later ones.
  • A customer's first order date is the date of their earliest completed order. Their cohort is the calendar quarter of that date.
  • A customer is retained if they have a completed order dated on or after the first order date plus 30 days.
  • Customers who are too new to judge are left out: only include a customer when their first order date plus 30 days is on or before the date of the latest ordered_at in orders (any status).
  • Customers with no completed order are not in any cohort.

Return cohort_quarter (the first day of the quarter), customers, retained_customers and retention_pct (retained customers as a percentage of customers, rounded to 1 decimal place). Sort by cohort_quarter.

Expected output

cohort_quartercustomersretained_customersretention_pct
  • Sorted by cohort_quarter ascending.
  • Numbers rounded to 1 decimal place.
  • Checked on the sample and on 2 hidden datasets. How grading works

Tables Online store · click a name to insert it

customers150 rows in the sample

One row per customer account. Key: customer_id.

intUnique id.
textName on the account.
textNULL when the customer never gave one (marketplace buyers).
textTwo-letter country code.
dateDay the account was created.
textorganic, paid_search, social, referral or email. NULL when unknown.
products36 rows in the sample

The catalog. Key: product_id.

intUnique id.
textDisplay name.
textKitchen, Home, Outdoors, Books, Beauty or Toys.
numeric(10,2)Current list price in USD.
dateFirst day on sale.
orders700 rows in the sample

One row per order. Key: order_id.

intUnique id.
intWho placed it.
timestampWhen it was placed (UTC).
textcompleted, cancelled or refunded.
textweb, app or marketplace.
numeric(10,2)Shipping charged, USD.
textCoupon used, NULL if none.
order_items1,266 rows in the sample

One row per product in an order. Key: order_id, product_id.

intThe order.
intThe product.
intUnits bought.
numeric(10,2)Price paid per unit, after discounts, USD.
returns79 rows in the sample

Items sent back. Key: return_id.

intUnique id.
intThe original order.
intThe product returned.
dateDay the return arrived.
intUnits returned.
textdamaged, wrong_item, not_as_described or changed_mind. NULL when not given.

Related questions

Loading the editor and PostgreSQL