F11
F11

Reputation: 3806

Query for following scenario

I have a table with columns EMPLOYEEID, EMPLOYEENAME, MANAGERID.

MANGERID is from the column EMPLOYEEID. SOME EMPLOYEEID don't have any manangerid(i.e. NULL). Now I need a output from query such that it returns EMPLOYEENAME and corresponding MANGERNAME.

How can It be done?

I have tried self joins but not able to get desired output.

Upvotes: 2

Views: 871

Answers (1)

Mahmoud Gamal
Mahmoud Gamal

Reputation: 79929

You will need a self join, with LEFT JOIN to get those that has no manager:

SELECT
  e.EMPLOYEEID,
  e.EMPLOYEENAME,
  m.EmployeeName AS ManagerName
FROM Employees AS e
LEFT JOIN Employees AS m ON e.ManagerId = m.EmployeeID;

SQL Fiddle Demo

Upvotes: 2

Related Questions