SQL Practice: Customer Lifetime Value
Rank customers by shipped revenue and keep only meaningful lifetime-value totals.
Customer lifetime value is a grouped revenue query. You need customer names, completed orders, and line-item revenue, then one result row per customer.
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 is not a row-level fact. It only exists after all of a customer's shipped line items have been summed, so the filter belongs inHAVING.
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 descSchema · Garden ShopTable · customers9 columns · 20 rows
One row per customer. Some customers have no phone on file.
For shipped orders only, return customers with at least 75 in lifetime revenue. Include customer_id, customer_name, distinct orders, and roundedlifetime_revenue. Sort by lifetime_revenue descending, then customer_name.
- Columns: customer_id, customer_name, orders, lifetime_revenue.
- Rows: customers with at least 75 in shipped revenue.
- Sort the highest lifetime revenue first.