x

retrieving manager name from table containing eid, ename and mgrid

Hi ,

I have a table like emp(empid,managerid,empname).i want to retrieve the employee details with their manager name.please help me writing the query .

thanks jaggs
more ▼

asked Sep 01, 2011 at 09:07 PM in Default

happy gravatar image

happy
21 1 1 1

(comments are locked)
10|1200 characters needed characters left

1 answer: sort oldest

I assume you also have a managers table that would contain something like (managerid, mangername) If so you can join your emp table to the manager table on managerid. Your query would resemble something like

 SELECT E.empid, E.empname, M.managername  
 FROM emp E  
      JOIN manager M on E.managerid = M.managerid  
more ▼

answered Sep 01, 2011 at 09:15 PM

Tim gravatar image

Tim
36.4k 35 41 139

there is no manager table ,managerid is the column which is nothing but the empid of the respective manager.so here I think I need to do self join but how i dont know.please help me
Sep 01, 2011 at 09:49 PM happy

This should do it.

 SELECT A.EMPID, A.EMPNAME, B.EMPNAME AS MANAGER  
 FROM EMP A  
JOIN EMP B ON A.MANGERID = B.EMPID
Sep 01, 2011 at 09:58 PM Tim
(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

New code box

There's a new way to format code on the site - the red speech bubble logo will automatically format T-SQL for you. The original code box is still there for XML, etc. More details here.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.

Topics:

x116

asked: Sep 01, 2011 at 09:07 PM

Seen: 1501 times

Last Updated: Sep 01, 2011 at 09:53 PM