Skip to content
/Chapter 10 · Practical Analytics Patterns
Lesson 10.3·garden_shop
Lesson 10.3

Cohorts and Time Buckets in SQL

A cohort groups records by when they first appeared. date_trunc collapses dates into month buckets you can count.

Analysts love cohorts: groups of customers bucketed by when they first showed up. To build one, you need two layers: first find each customer's first order, then bucket those first orders by month and count them.

date_trunc('month', some_date) snaps a date down to the first of its month, which turns scattered dates into a handful of monthly buckets you can group on. The inner query (the starter) finds each customer's first order; wrap it to count per month.

Pattern
select date_trunc('month', signup_date) as cohort,
       count(*) as users
from accounts
group by cohort
order by cohort
Schema · Garden ShopTable · orders6 columns · 24 rows
Table · orders

One row per order. Unshipped orders have a null shipped_date.

6 columns · 24 rows
order_id intcustomer_id intorder_date dateshipped_date datestatus textcoupon_code text
Your task

Count new customers by cohort month. Using each customer's first order, return cohort_month (the month of that first order) and new_customers (how many customers' first order fell in that month), oldest month first.

SQL Workbench
query.sqlgarden_shop · SQL engine loading
⌘↵ to run
·
Expected answer
  • Columns: cohort_month, new_customers.
  • Rows: one row per month that gained its first customers.
  • cohort_month is the first of the month (e.g. 2024-01-01); each customer is counted once, in the month of their very first order.