sql query returning unexpected results on NOT IN

I have  query that should return results, but doesn't.

First I have an employeeUnitJob table. It has no records in it for this employee:
The query is:
select * from EmployeeUnitJobType   where EmployeeId = 159795

Open in new window

It returns no rows as expected.

I want to do a query that returns the employees who have no rows in the employeeunitJobtype table, so I do this query, and just for testing I add the employeeID of this one employee that I know doesn't have a row in the table:

 FROM Employee 
 Employee.EmployeeId NOT IN (select EmployeeId from EmployeeUnitJobType)
  AND EmployeeId = 159795

Open in new window

But this ALSO returns no rows. as though everyone has a unit/job and this person does not!

Is there something I am doing wrong? Why is the above query NOT returning any rows? it should return this employee?

Starr DuskkASP.NET VB.NET DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Scott PletcherSenior DBACommented:
dsackerContract ERP Admin/ConsultantCommented:
Take off the "AND EmployeeId = 159795". That is limiting your query to that one employee.

And if that doesn't yield what you expect, re-code your query this way:
SELECT Employee.*
FROM  Employee
LEFT JOIN (SELECT EmployeeId from EmployeeUnitJobType
         GROUP BY EmployeeId) t
ON t.EmployeeId = Employee EmployeeId
WHERE t.EmployeeId IS NULL
AND Employee.EmployeeId = 159795

Open in new window

Scott PletcherSenior DBACommented:

NOT IN (select EmployeeId from EmployeeUnitJobType where EmployeeId is not null)

NULLs prevent a valid True/False result from NOT EXISTS.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
IT Pros Agree: AI and Machine Learning Key

We’d all like to think our company’s data is well protected, but when you ask IT professionals they admit the data probably is not as safe as it could be.

Starr DuskkASP.NET VB.NET DeveloperAuthor Commented:
Both work, but I do prefer the second, although it might not be as politically correct. We have a lot of units and jobs per employee and I'm thinking the second might return multiple rows and I only want one row returned.

Scott PletcherSenior DBACommented:
"NOT IN" is roughly equivalent to a "NOT EXISTS", both of which take advantage of the anti semi join.  SQL won't usually convert a left join and check for NULL into an anti join, and, if not, it's usually less efficient.

Btw, DISTINCT performs better than GROUPing when all you need to do is get distinct values, so if you do decide to use that approach, code it like this instead:
LEFT JOIN (SELECT DISTINCT EmployeeId from EmployeeUnitJobType) t
Starr DuskkASP.NET VB.NET DeveloperAuthor Commented:
thanks scott!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.