Top Performing Salesperson for Each Month
Consider two tables:
Salespersons table has columns:
Sales table has columns:
quantity (int), and
Write a SQL query to identify the salesperson with the highest total quantity sold for each month.
DB Fiddle Instructions
Click ‘Edit on DB Fiddle’ to be taken to the in browser SQL playground where you can run your queries.
First, we create a CTE
MonthlySales to calculate the total quantity sold by each salesperson for each month. Then, we create another CTE
RankSales to rank salespersons in each month by their total quantity sold in descending order. In the main query, we select the salespersons with the highest rank (1) in each month.