The correct syntax for a T-SQL subquery and a poss

2019-08-31 02:36发布

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条回答
做个烂人
2楼-- · 2019-08-31 03:11

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

查看更多
登录 后发表回答