The correct syntax for a T-SQL subquery and a possible join

北城以北 提交于 2020-01-25 11:38:09

问题


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! :-)



来源:https://stackoverflow.com/questions/3126627/the-correct-syntax-for-a-t-sql-subquery-and-a-possible-join

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!