SQL Basics: Top 10 customers by total payments amount


  • customer_id [int4]
  • email [varchar]
  • payments_count [int]
  • total_amount [float]
  • only returns the 10 top customers, ordered by total amount spent

DVD Rental ER Model

select c.customer_id as customer_id, c.email as email, count(*) as payments_count, sum(p.amount) as total_amount from customer c join
payment p on
c.customer_id = p.customer_id
GROUP BY customer_id, email
ORDER BY total_amount;



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store


In love with telling stories with data