HomeQuery LibraryShopify

How well do you retain customers month over month?

A cohort retention table is the gold standard for understanding customer loyalty. This query groups customers by their first purchase month and tracks what percentage return to buy again in each subsequent month.

📊 Shopify👥 Shopify merchants and growth teams🔤 Plain-English → SQL

This is part of the Taptic Data Shopify query library. Explore related queries: SQL query for Shopify revenue by month, SQL query for Shopify customer lifetime value, SQL query for Shopify refund rate by product, and more.

Show me a cohort retention table — percentage of customers who return each month after their first purchase
Shopify Cohort Retention Table — generated by Taptic Data AI
WITH first_purchase AS (
  SELECT
    customer_id,
    DATE_TRUNC('month', MIN(created_at))::date AS cohort_month
  FROM shopify_orders
  WHERE financial_status IN ('paid', 'partially_refunded')
    AND cancelled_at IS NULL
    AND customer_id IS NOT NULL
  GROUP BY customer_id
),
subsequent AS (
  SELECT
    fp.cohort_month,
    EXTRACT(YEAR FROM AGE(DATE_TRUNC('month', o.created_at), fp.cohort_month)) * 12
      + EXTRACT(MONTH FROM AGE(DATE_TRUNC('month', o.created_at), fp.cohort_month))
                                    AS months_since_first,
    COUNT(DISTINCT o.customer_id)  AS returning_customers
  FROM shopify_orders o
  JOIN first_purchase fp ON fp.customer_id = o.customer_id
  WHERE o.financial_status IN ('paid', 'partially_refunded')
    AND o.cancelled_at IS NULL
  GROUP BY fp.cohort_month, months_since_first
),
cohort_sizes AS (
  SELECT cohort_month, COUNT(*) AS cohort_size
  FROM first_purchase
  GROUP BY cohort_month
)
SELECT
  TO_CHAR(s.cohort_month, 'YYYY-MM')        AS cohort,
  cs.cohort_size,
  s.months_since_first,
  s.returning_customers,
  ROUND(s.returning_customers * 100.0
    / NULLIF(cs.cohort_size, 0), 1)          AS retention_pct
FROM subsequent s
JOIN cohort_sizes cs USING (cohort_month)
WHERE s.months_since_first BETWEEN 0 AND 11
  AND s.cohort_month >= NOW() - INTERVAL '12 months'
ORDER BY s.cohort_month, s.months_since_first
Schema-aware SQL generated from plain English40 lines

This query was generated by Taptic Data from plain English against a real Shopify schema. In Taptic, you type the question — the AI writes the SQL, runs it, and returns the result. You can edit the SQL, ask for explanations, and save it as a refreshable report.

Breaking it down line by line

  1. CTE "first_purchase" finds each customer's cohort (month of their first order)
  2. CTE "subsequent" calculates months since first purchase for every order, counting distinct returning customers
  3. CTE "cohort_sizes" gets the total number of first-time buyers per cohort month
  4. Final SELECT divides returning customers by cohort size to get retention percentage
  5. Filtered to 12 months of cohorts and 0-11 months of follow-up to keep the table readable

Result description

A cohort retention matrix showing the percentage of customers from each first-purchase month who returned in months 1 through 11.

The business impact

Cohort retention separates sustainable growth from leaky bucket growth. If your Month 3 retention is dropping cohort over cohort, you're spending more to acquire customers who leave faster. If it's improving, your product and experience investments are working.

Common questions

How do I generate a "Shopify Cohort Retention Table" query automatically?
In Taptic Data, type "Show me a cohort retention table — percentage of customers who return each month after their first purchase" and the AI generates schema-aware SQL against your real Shopify data — no manual writing required.
What database does this query work with?
This query is designed for Shopify. Taptic reads your live schema so the generated SQL always matches your actual table and column names.
Can I edit the generated SQL?
Yes. Taptic shows you the exact SQL it generated. You can edit it directly, ask the AI to explain any line, or request a revision in plain English.
Can I save this as a scheduled report?
Yes. Once you run this query in Taptic, you can save it as a report, add charts and KPIs, and schedule it to email your team on any cadence — daily, weekly, or monthly.

Skip the SQL. Ask the question.

In Taptic Data, you type "Show me a cohort retention table — percentage of customers w..." and this SQL runs automatically against your real Shopify data.

Try Taptic Free — $29.99/mo

No credit card required. Connect your data source in under 5 minutes.