SQL SUM Practice: Totalling 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
Summed decimals can drift into long trailing digits. Wrapping the total inround(sum(amount), 2) presents clean currency without changing the ranking. 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 descSchema · Movie RentalsTable · rentals6 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.
- Columns: customer_id, rentals, total_spent.
- Rows: 10 customers.
- Customer 1 tops the list with 4 rentals and 15.96 spent.