SQL Practice: Customer Lifetime Revenue
Rank customers by shipped revenue and keep only customers above a revenue threshold.
This lesson calculates lifetime revenue: shipped sales summed per customer. That is sometimes used as a simple CLV proxy, but true customer lifetime value can also account for margin, acquisition cost, refunds, retention, and future expected value.
This lesson brings together the most common analytics pieces: multi-table
joins, a calculated money value,
GROUP BY, and
HAVING.
Use HAVING for the threshold
The 75-dollar cutoff isn't a row-level fact. It only exists after all of a
customer's shipped line items have been summed, so the filter belongs in
HAVING.
select customer_id,
customer_name,
count(distinct order_id) as orders,
round(sum(line_revenue), 2) as lifetime_revenue
from joined_sales
where status = 'shipped'
group by customer_id, customer_name
having sum(line_revenue) >= 75
order by lifetime_revenue desc Schema · Garden Shop Tables · customers, orders, order_items 3 tables
One row per customer. Some customers have no phone on file.
One row per order. Unshipped orders have a null shipped_date.
One row per line item within an order.
For shipped orders only, return customers with at least 75 in lifetime revenue. Include customer_id, customer_name (first and last name joined by a space), distinct orders, and lifetime_revenue rounded to two decimals. Sort by lifetime_revenue descending, then customer_name.
Run your query to inspect the result.
Expected answer
- Columns: customer_id, customer_name, orders, lifetime_revenue.
- Rows: customers with at least 75 in shipped revenue.
- Sort the highest lifetime revenue first.
Related
COUNT, SUM, AVG, MIN, MAX, GROUP BY, and HAVING.
COUNT, SUM, AVG, GROUP BY, and HAVING.
Practice aggregate reports, HAVING, and answer shape.
Avoid truncating fractional results.
Group or aggregate every selected column.
Practice buckets, business labels, and conditional counts.