I am trying to write a query to find the top sales person in the shop and trying to Display the employee name, the shop where he/she is employed and the value of sales. I only want to output the employee with highest number of sales at the moment, my query outputs all the employees with their figures.
SELECT empnin, shopname, SUM( Rentalrate ) AS Sales
FROM frs_FilmRental
NATURAL JOIN frs_Shop
GROUP BY empnin
This is the following results I get with my query:
If you want the top sales person per shop, then you need to filter. This is best done in the
WHEREclause.In MySQL, this type of query is actually most easily accomplished using variables:
Also, don't use
natural join. It is a bug waiting to happen, because it hides the keys used for joining and some unexpected columns might end up being used.