What would be the correct syntax and join (if any) of a subquery that would return all of the employees first and last name from the employee’s table, and return their department name from the department table, but only those employees who more than the average salary for their department? Thanks for your answers
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
This query should give you what you are looking for.
select firstName, lastName, departmentName
from Employees e join
(select departmentID, departmentName, AVG(salary) AS averageSalary
from Department d
join Employees e ON e.departmentID=d.departmentID
group by departmentId, departmentName) ds
on ds.departmentID=e.departmentID
where e.salary>ds.AverageSalary
(PS: I agree with the comment above. It's SO etiquette to post what you have tried so far. You were lucky this time! :-)