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.
Author |
Topic |
srinath
Starting Member
16 Posts |
Posted - 2006-10-16 : 07:17:16
|
Hi All,I have a query to know the list of employees in a EMP table who are not superiors to any one likeFor Ex: In a table EMp with columns EMPNo and MGR I have the data as followingEMPNo MGR----- ---7369 79027499 76987521 76987566 78397654 76987698 78397782 78397788 75667839 NULL7844 76987876 77887900 76987902 75667934 7782I need to get the records as EMPNo-----73697499752176547844787679007934I wrote the query as SELECT distinct EMPNO,MGR FROM DBO.EMP WHERE EMPNO NOT IN (SELECT MGR FROM DBO.EMP) which didn't returny any valuesBut When i changed the query as SELECT * FROM DBO.EMP WHERE EMPNO NOT IN (SELECT EMP.EMPNO FROM DBO.EMP EMP,DBO.EMP MGR WHERE EMP.EMPNO = MGR.MGR )I am getting the exact answer what requiredCan You guys please let me know what is the differnce between the 2 queries and why the first one is not working?Thank You,Srinath. |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-10-16 : 07:22:49
|
In the first query, all but one emp has mgr. Writing NOT IN for a NULL value does work.Change your first query toSELECT EMPNO, MGR FROM dbo.EMP WHERE EMPNO NOT IN (SELECT MGR FROM dbo.EMP where mgr is not null) Peter LarssonHelsingborg, Sweden |
 |
|
srinath
Starting Member
16 Posts |
Posted - 2006-10-16 : 07:27:59
|
Thanks for your reply Peter, I haven't noticed the Null. The query you gave is working fine. |
 |
|
|
|
|