Md. Nazmul Hossain Bilash Md. Nazmul Hossain Bilash - 3 months ago 6
MySQL Question

How to get top 2 customers from customer table using mysql?

In customer table:

ID| Name | Cost
-------------------------
1| name1 | 1000
2| name2 | 200
3| name3 | 700
4| name2 | 700
5| name3 | 800


I need to find out Top 2 customers name & ID & cost from customer table who buy .
Can anyone help me ?

My expected Output :

ID| Name | Cost
----------------------
1| name1 | 1000
2| name2 | 900

Answer
SELECT 
    GROUP_CONCAT(ID) as payment_ids, 
    Name, 
    SUM(Cost) as total_cost 
FROM 
    Customer 
GROUP BY 
    Name 
ORDER BY 
    total_cost DESC 
LIMIT 2