Skip to content
Advertisement

Query to display Employee and Manager

I have to write a SQL Server query to display the employee last name and his respective manager’s name, as given below.

These are the schema for the two tables.

Expected Output :

I tried this code but its not right

Detailed Schema:

Schema

Advertisement

Answer

I guess something as simple as this should do it

How it works:

  • The join from Employee e with Department d will get you exact 1 row in Department
  • Then join back from this one Department to employee e2, this will return exact 1 employee (the boss) from Employee e2
  • Now you have the employee in e, and the boss in e2

I tested with this

the result

EDIT

if you want to leave out the bosses, add this where clause to the query

or alternative this where clause

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement