HomeQuery LibraryDatabase

Who are your top 20 customers, and how much are they worth?

Pareto principle holds for most B2B businesses — 20% of customers drive 80% of revenue. This query finds yours, including their order history and average deal size.

📊 Any SQL database👥 Sales and finance teams🔤 Plain-English → SQL
Part of the Explore all SQL query examples

This is part of the Taptic Data Database query library. Explore related queries: SQL query for monthly revenue trend, SQL query for top customers by revenue, SQL query for revenue by region, and more.

Show me my top 20 customers by total revenue this year with order count and average deal size
Top Customers by Revenue Query — generated by Taptic Data AI
SELECT
  c.company_name,
  c.account_manager,
  COUNT(DISTINCT o.id)                          AS order_count,
  ROUND(SUM(o.total_amount)::numeric, 2)        AS total_revenue,
  ROUND(SUM(o.total_amount)::numeric
    / NULLIF(COUNT(DISTINCT o.id), 0), 2)       AS avg_deal_size,
  MIN(o.order_date)::date                       AS first_order,
  MAX(o.order_date)::date                       AS last_order,
  ROUND(SUM(o.total_amount)::numeric * 100
    / SUM(SUM(o.total_amount)) OVER ()::numeric, 2) AS pct_of_total_revenue
FROM orders o
JOIN customers c ON c.id = o.customer_id
WHERE o.order_date >= DATE_TRUNC('year', CURRENT_DATE)
  AND o.status NOT IN ('cancelled', 'refunded')
GROUP BY c.id, c.company_name, c.account_manager
ORDER BY total_revenue DESC
LIMIT 20
Schema-aware SQL generated from plain English18 lines

This query was generated by Taptic Data from plain English against a real Any SQL database 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 ai business analytics and text to sql tool.

Breaking it down line by line

  1. Joins orders to customers to include company name and account manager in results
  2. Filters to current year and excludes cancelled/refunded orders
  3. Uses a window function (SUM OVER ()) to calculate each customer's share of total revenue in one pass
  4. Includes first and last order dates to reveal customer tenure and recency
  5. NULLIF prevents division-by-zero in the average deal size calculation

Result description

Top 20 customers ranked by total revenue with order count, average deal size, first/last order dates, and percentage of total company revenue.

The business impact

Knowing your top customers by revenue enables smarter account management, more targeted retention efforts, and better CAC/LTV modeling. The percentage-of-total column immediately shows concentration risk — if 3 customers represent 60% of revenue, that's a business risk worth knowing.

All queries and use cases on this page relate to analyzing your Any SQL database data in Taptic. To see the full analytics workflow, explore ai business analytics, text to sql tool.

Common questions

How do I generate a "Top Customers by Revenue Query" query automatically?
In Taptic Data, type "Show me my top 20 customers by total revenue this year with order count and average deal size" and the AI generates schema-aware SQL against your real Any SQL database data — no manual writing required.
What database does this query work with?
This query is designed for Any SQL database. 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 my top 20 customers by total revenue this year with ..." and this SQL runs automatically against your real Any SQL database 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 SQL Server data
Connect 🗄️ SQL Server to Taptic Data and this SQL generates automatically from plain English — against your real schema, your real tables.
Analytics hub
More Database queries, use cases, and analytics resources in one place.
Explore all SQL query examples