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?
Question posted in PhpMyAdmin
The official documentation can be found here.
The official documentation can be found here.
2
Answers
It should be something like this. You might have to play around but it will give you a good start.
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