Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6803371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:20:10+00:00 2026-05-26T19:20:10+00:00

I have a emp table, CREATE TABLE [dbo].[Emp]( [EmpId] [int] NULL, [EmpName] [nvarchar](50) COLLATE

  • 0

I have a emp table,

    CREATE TABLE [dbo].[Emp](
    [EmpId] [int] NULL,
    [EmpName] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [ManagerId] [int] NULL
) ON [PRIMARY]

Now, insert below values into the table

   Insert Into Emp Values(1,'A',0)
Insert Into Emp Values(2,'B',1)
Insert Into Emp Values(3,'C',2)
Insert Into Emp Values(4,'D',2)
Insert Into Emp Values(5,'E',4)
Insert Into Emp Values(6,'F',4)
Insert Into Emp Values(7,'G',4)
Insert Into Emp Values(8,'H',6)
Insert Into Emp Values(9,'I',5)
Insert Into Emp Values(10,'J',7)
Insert Into Emp Values(11,'K',4)

I want to list employee name and their manager name in select statement.

What I am doing now is creating a temporary table which has all manager name and their Id.

Then getting the name from the manager table based on Id.

But I know this is not a correct way, in fact it is complex.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T19:20:11+00:00Added an answer on May 26, 2026 at 7:20 pm

    You should use a recursive CTE (Common Table Expression) for this:

    -- define the recursive CTE and give it a name
    ;WITH Hierarchy AS
    (
        -- "anchor" - top-level rows to select, here those with ManagerId = 0
        SELECT EmpId, EmpName, NULL AS 'MgrId', CAST(NULL AS NVARCHAR(50)) AS 'MgrName', 1 AS 'Level'
        FROM dbo.Emp
        WHERE ManagerId = 0
    
        UNION ALL
    
        -- recursive part - join an employee to its manager via ManagerId -> mgr.EmpId
        SELECT e.EmpId, e.EmpName, mgr.EmpId, mgr.EmpName, mgr.Level + 1 AS 'Level'
        FROM dbo.Emp e
        INNER JOIN Hierarchy mgr ON e.ManagerId = mgr.EmpId
    )
    SELECT * FROM Hierarchy
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have two tables 1st table emp EmpID | EmpName|xyz...coloums 1. | Hrishi
I have a table emp as follows: empid empname homephone homeadd workphone workadd 1
I have a table names 'emp'. Columns are: uid,emp_name,emp_dept,emp_sal. Now i want to find
I am using MySQL. I have a table called EMP, and now I need
I have a table emp with following structure and data: name dept salary -----
SQL Server procedure can return result sets. I have a table emp(emp__id, emp__name, ...)
This is the question I have. Develop a salary sheet from emp table consisting
I have two tables. Table Emp id name 1 Ajay 2 Amol 3 Sanjay
I have a table with multiple date columns per employee: Emp 1, Date1, Date2,
I have a two column table as follows: ID Emp ID 1 1 1

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.