HomeQuery LibraryShopify

Who are your most valuable Shopify customers, and what did they first buy?

Customer lifetime value is the metric that separates sustainable eCommerce businesses from ones that are just burning acquisition budget. This query calculates it directly from your Shopify orders.

📊 Shopify👥 Shopify merchants🔤 Plain-English → SQL
Part of the Explore all Shopify analytics queries

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 customer lifetime value — total spend, order count, and first product purchased — ranked by highest LTV
Shopify Customer Lifetime Value — generated by Taptic Data AI
WITH customer_orders AS (
  SELECT
    o.customer_id,
    COUNT(DISTINCT o.id)            AS order_count,
    SUM(o.total_price)              AS lifetime_value,
    MIN(o.created_at)               AS first_order_date,
    MAX(o.created_at)               AS last_order_date
  FROM shopify_orders o
  WHERE o.financial_status = 'paid'
  GROUP BY o.customer_id
),
first_product AS (
  SELECT DISTINCT ON (o.customer_id)
    o.customer_id,
    li.title                        AS first_product
  FROM shopify_orders o
  JOIN shopify_order_line_items li ON li.order_id = o.id
  ORDER BY o.customer_id, o.created_at ASC
)
SELECT
  c.email,
  COALESCE(c.first_name || ' ' || c.last_name, c.email) AS customer_name,
  co.order_count,
  ROUND(co.lifetime_value::numeric, 2)                  AS lifetime_value,
  ROUND(co.lifetime_value::numeric
    / NULLIF(co.order_count, 0), 2)                     AS avg_order_value,
  fp.first_product,
  co.first_order_date::date                             AS first_order,
  co.last_order_date::date                              AS last_order,
  (co.last_order_date - co.first_order_date)
    / NULLIF(co.order_count - 1, 0)                     AS avg_days_between_orders
FROM customer_orders co
JOIN shopify_customers c ON c.id = co.customer_id
LEFT JOIN first_product fp ON fp.customer_id = co.customer_id
WHERE co.order_count >= 2
ORDER BY co.lifetime_value DESC
LIMIT 100
Schema-aware SQL generated from plain English37 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. This type of analysis is commonly used in shopify revenue dashboard and ecommerce reporting.

Breaking it down line by line

  1. CTE "customer_orders" aggregates per-customer: order count, total spend, and first/last order dates — filtered to paid orders only
  2. CTE "first_product" uses DISTINCT ON to efficiently grab the product title from the first order per customer
  3. Joins to shopify_customers for name and email display
  4. Calculates AOV per customer (total spend / orders) and average days between orders as a repurchase frequency proxy
  5. HAVING order_count >= 2 filters to repeat customers — the signal of true loyalty vs one-time buyers

Result description

A ranked table of your best repeat customers showing lifetime value, order count, average order value, first product purchased, first/last order dates, and average repurchase frequency.

The business impact

LTV drives every major eCommerce decision — how much to spend on acquisition, which products to promote, which customers to target with retention campaigns. Knowing what your highest-LTV customers first bought tells you which products to prioritize in acquisition campaigns.

New to this metric? How to calculate Shopify customer lifetime value — definition, formula, and business context.

All queries and use cases on this page relate to analyzing your Shopify data in Taptic. To see the full analytics workflow, explore shopify revenue dashboard, ecommerce reporting.

Common questions

How do I generate a "Shopify Customer Lifetime Value" query automatically?
In Taptic Data, type "Show me customer lifetime value — total spend, order count, and first product purchased — ranked by highest LTV" 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 customer lifetime value — total spend, order count, ..." 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.

Next step
Run this query on your own Shopify data
Connect 🟢 Shopify to Taptic Data and this SQL generates automatically from plain English — against your real schema, your real tables.
Analytics hub
More Shopify queries, use cases, and analytics resources in one place.
Explore all Shopify analytics queries