Beyondrelational

Friday, March 26, 2010

Sample Queries

------ Given ------------------
emp_no emp_name supervisor_no
————————————————-
1 vinay 3
2 kamesh 1
3 muhu NULL
NULL NULL NULL

This is my table datas.
I want the result to be in this format;

for vinay the suprevisor is muhu which is from the same table…

emp_name supervisor_name
——————————————
vinay muhu
kamesh vinay
muhu NULL
*/


Select * From Emptable



Select a.EMP_Name,b.Supervisor_Name From
(Select Emp_no as SUP_No,Emp_Name as Supervisor_Name From EmpTable Where Emp_No in (Select Distinct SUP_NO From EmpTable)) as b,
EmpTable as a
where a.SUP_NO=b.SUP_NO and a.Emp_Name not in ('null')

--///////////////////////Another Way///////////////////////////////-----------

SELECT e.emp_name emp_name, s.emp_name supervisor_name
FROM EmpTable e
LEFT OUTER JOIN EmpTable s
ON s.emp_no=e.sup_no
Where e.Emp_Name not in ('null')

No comments:

Post a Comment