Text-to-SQL
Show the five customers with the highest lifetime revenue.

Lifetime revenue is the total of paid orders per customer. This query groups the orders, filters out unpaid ones, and returns the top five.

It assumes orders.total stores the order amount and status = 'paid' marks settled orders. Adjust the status value if your schema uses a different one.

sql
SELECT
  c.id,
  c.name,
  SUM(o.total) AS lifetime_revenue
FROM customers AS c
JOIN orders AS o ON o.customer_id = c.id
WHERE o.status = 'paid'
GROUP BY c.id, c.name
ORDER BY lifetime_revenue DESC
LIMIT 5;

12s

Example conversation

Sign in to run this generator on your own schema.

Sign in to try it