Homeโ€บQuery Libraryโ€บShopify

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
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.

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.

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.