SQL SUM Practice: Totaling Rental Revenue by Customer
Turn a stack of rentals into a per-customer revenue summary.
Each row in
rentals
is one transaction with an
amount. To answer "who are our best customers?" you group those transactions by
customer and add the money up.
COUNT
and
SUM
often travel together: one tells you how many rentals a customer made, the
other tells you how much they were worth.
Keep money readable
Money reads best at two decimal places, so round it for display. Here
round(sum(amount), 2)
presents the total to two decimal places. Sorting by the total descending
puts the highest spenders on top, which makes a classic leaderboard.
select group_column,
count(*) as items,
round(sum(amount_column), 2) as total
from table_name
group by group_column
order by total desc Schema · Movie Rentals Table · rentals 6 columns · 26 rows
One row per rental. Movies still out have a null returned_date.
For each customer_id, return the number of rentals (rentals) and the total amount they paid, rounded to two decimals (total_spent). Rank by total_spent descending, then customer_id.
Run your query to inspect the result.
Expected answer
- Columns: customer_id, rentals, total_spent.
- Rows: 10 customers.
- Customer 1 tops the list with 4 rentals and 15.96 spent.
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.