Home > Back-end >  SQL Choose a manager in whose department the total value of sales for 1990 is the highest
SQL Choose a manager in whose department the total value of sales for 1990 is the highest

Time:02-08

I have a hard time trying to figure out the way the tables Sales_order and Employees are connected.

The question is "How can I extract data on the manager with the highest value of sales for 1990 within his department if there is no common column between these tables?"

Diagram

Tables

CodePudding user response:

join indirectly via customer to get manager_id

select top 1 manager_id,sum(total)
from   salesorders so
join   customers cus on cus.customer_id = so.customer_id
join   employee emp on emp.employee_id = cus.salesperson_id
where  so.order_date between '1999-01-01' and '1999-12-31'
group  by manager_id
order sum(total) desc
  •  Tags:  
  • Related