Skip to content
/ Chapter 12 · Applied Practice: Movie Rentals
Lesson 12.3 · movie_rentals
Lesson 12.3

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.

Pattern
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
Table · rentals

One row per rental. Movies still out have a null returned_date.

6 columns · 26 rows
rental_id int customer_id int movie_id int rental_date date returned_date date amount decimal
Your task

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.