skip to Main Content

I have a database about the shop, and I need to return full information of the first 5 customers, which has spent the most amount, and in returned results also display that total amount. (counting all
his/her payments together from the payments table). Order descending by the total amount.
Can someone help with the query for this?

Customers Table
enter image description here

Payments Table
enter image description here

2

Answers


  1. It should be something like this. You might have to play around but it will give you a good start.

    SELECT c.CustomerName, SUM(p.amount) AS Total
    FROM CustomersTable c
    INNER JOIN PaymentsTable p
    ON c.customerNumber = p.customerNumber
    GROUP BY p.customerNumber DESC LIMIT 5
    
    Login or Signup to reply.
  2. SELECT min(c.Customername), SUM(p.amount) AS Total
    FROM Customers c
    INNER JOIN payments p
    on c.Customernumber=p.Customernumber

    GROUP BY p.Customernumber
    order by 2 DESC

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search