ManiMuthuPandi ManiMuthuPandi - 4 years ago 135
SQL Question

How to use join here

I am having the below table

enter image description here

From this table I have to display Username and SupervisorName. I am having both

, and
in the same table.

My Expected Output is:

enter image description here

Answer Source

Just join the table with itself:

SELECT u.Name As UserName,
       s.Name As SupervisorName
FROM dbo.TableName u
LEFT OUTER JOIN dbo.TableName s
    ON u.SupervisorUID = s.UID

I've used a LEFT OUTER JOIN for the case that a user could have no supervisor. If it cannot be NULL you can use INNER JOIN.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download