views:

43

answers:

1

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

+4  A: 

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

mdma
+1 for the effort of work without any code or clue from the question.
Jonathan