Here is a really ugly way:
SELECT t1.empname,
t1.shopname,
t2.maxsales
FROM
(
SELECT empname, shopname, SUM(rentalrate) AS sales
FROM frs_FilmRental
NATURAL JOIN frs_Employee
NATURAL JOIN frs_Shop
GROUP BY empname, shopname
) t1
INNER JOIN
(
SELECT t.shopname, MAX(t.Sales) AS maxsales
FROM
(
SELECT shopname, SUM(rentalrate) AS Sales
FROM frs_FilmRental
NATURAL JOIN frs_Employee
NATURAL JOIN frs_Shop
GROUP BY empname, shopname
) t
GROUP BY t.shopname
) t2
ON t1.shopname = t2.shopname AND
t1.sales = t2.maxsales
You will recognize the subquery t1
as simply being your original query. The t2
subquery restricts this result set to only those shops which had the highest sales, implying only the record corresponding to the employeee with the highest sales. In the event of a tie, both records would be returned for that shop.
The query is a bit verbose, but this owes in part to that MySQL does not have support for row number or common table expressions.