Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Retrieve records from a table.

Author  Topic 

chitturiii
Starting Member

2 Posts

Posted - 2010-11-09 : 06:45:09
Hi All,
Please help me with the below pblm.

Suppose there is a table 'Employee' with columns EmpID,EmpName,ManagerID and with records as below:

EmpId EmpName MngrID
1 aaa 4
2 bbb 3
3 ccc 4
4 ddd 2

I want a query to get the EmpName and ManagerName as resultset from the Employee table. (Here Manager is again an employee in the company and so if Mngrid is 4 then his name is ddd) .
So finally my result should be:

EmpName ManagerName
aaa ddd
bbb ccc
ccc ddd
ddd bbb

pk_bohra
Master Smack Fu Yak Hacker

1182 Posts

Posted - 2010-11-09 : 06:51:49
I feel its a homework.

Hint for you to start with.

Join the same table two times with different alias and the joining link will be EmpID of one table with MgrID of next table.

Try and frame the sql and post the sql if you face any problem.

Go to Top of Page

chitturiii
Starting Member

2 Posts

Posted - 2010-11-09 : 07:16:52
Thank You for the hint :)
The below query works:


Select a.EmpName EmployeeName, b.EmpName ManagerName from Employee a, Employee b
where a.MngrID=b.EmpID


quote:
Originally posted by pk_bohra

I feel its a homework.

Hint for you to start with.

Join the same table two times with different alias and the joining link will be EmpID of one table with MgrID of next table.

Try and frame the sql and post the sql if you face any problem.



Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-11-09 : 07:30:12
Better use the JOIN syntax:
SELECT
a.EmpName AS EmployeeName,
b.EmpName AS ManagerName
FROM Employee AS a
INNER JOIN Employee AS b
ON a.MngrID=b.EmpID



No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-11-09 : 08:50:15
quote:
Originally posted by webfred

Better use the ANSI JOIN syntax:
SELECT
a.EmpName AS EmployeeName,
b.EmpName AS ManagerName
FROM Employee AS a
INNER JOIN Employee AS b
ON a.MngrID=b.EmpID



No, you're never too old to Yak'n'Roll if you're too young to die.



Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-11-09 : 09:02:10
Yes yes yes


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page
   

- Advertisement -