Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Employee Table:-
EMP_ID DESINGNATION EMP_NAME
1 MD Sunil
2 PM Kapil
3 PL Sachin
4 TL Rahul
5 PG Ajay

Supervison_Mapping table:-
EMP_ID SUPERVISON_CODE
1 NULL
2 1
3 2
4 2
5 3

Require output as below by joining above tables.
Emp_ID
Supervisor_code
Emp_Designation
Emp_Name
Supervisor_Designation
Supervisor_Name

What I have tried:

select a.*,b.* from employee a, supervisor_mapping b where a.emp_id = b.emp_id;

by above query able to get below
Emp_ID
Supervisor_code
Emp_Designation
Emp_Name

but not below
Supervisor_Designation
Supervisor_Name
Posted
Updated 31-Jan-19 3:35am
Comments
#realJSOP 31-Jan-19 9:33am    
You're doing it wrong.

See? My answer matches you question with regards to useful inormation.

1 solution

Your code:
SQL
select a.*,b.* from employee a, supervisor_mapping b where a.emp_id = b.emp_id;

Don't do joins like that, it is very old-fashioned, not very robust and not recommended. Use explicit joins.

It is also good practice to list the columns you want rather than using *

Try something like this (untested):
SQL
SELECT a.Emp_ID,b.Supervisor_code,a.Emp_Designation,a.Emp_Name,c.Emp_Designation AS Supervisor_Designation,c.Emp_Name AS Supervisor_Name
FROM employee a
INNER JOIN supervisor_mapping b on a.emp_id=b.emp_id
INNER JOIN employee c on b.supervision_code=c.emp_id

This is what is known as a "self join" where you join to the same table again. Notice that I have used c.Emp_Name AS Supervisor_Name
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900