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.
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.
The question
The SQL Taptic generates
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
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.
How this query works
What it returns
A cohort retention matrix showing the percentage of customers from each first-purchase month who returned in months 1 through 11.
Why it matters
FAQ
Generate this automatically
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/moNo credit card required. Connect your data source in under 5 minutes.
Compare tools