I need to write a query that determines the customer that has spent the most on music for each country. Write a query that returns the country along with the top customer and how much they spent. For countries where the top amount spent is shared, provide all customers who spent this amount.
You should only need to use the Customer and Invoice tables.
Check Your Solution
Though there are only 24 countries, your query should return 25 rows because the United Kingdom has 2 customers that share the maximum.
I build the query but There's one group having two maximum result I have to show
SELECT CustomerId , FirstName, LastName, Country, MAX(TotalSpent) AS TotalSpent
from (select c.CustomerId as CustomerId, c.Firstname As FirstName, c.LastName as LastName, i.BillingCountry as Country, SUM(i.Total) as TotalSpent
from customer c join invoice i
on c.CustomerId = i.CustomerId
group by 1,2,3,4
order by 5 desc
limit by 1 ) AS temp
group by 4
the expected outcome must be 25 rows not 24 United Kingdom have two customer share the same amount of maximum spent amount