Skip to content
Advertisement

Query using a column which contains the value of another column

I have the following table in SQL Server 2019

Note: ManagerUserID is nothing but the userid

The data is

How can we get user details and their manager details (they are just another user) by using one query?

These are my scripts

Advertisement

Answer

You can use a subquery or a self JOIN to achieve this:

Result:

Optionally, you can add COALESCE to fill in NULL values with something else like this:

Result:

Fiddle here.

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