SQL Statement

Hello

I need a SQL statement that selects records where a column value in one table does not exist in another table

For example
TableA has a column called Part. There could be 1 million plus records in this table

TableB has a column called NonPart. This table could have several thousand records.

What I need is a list of all items in TableA where the PART is NOT IN TableB NonPart column.

I've tried several things, nothing seems to work. I end up getting some records that are correct, and some that are not (meaning it does exist in TableB).

Any help is greatly appreciated.
WaldaIncAsked:
Who is Participating?

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

x
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.

Faiga DiegelSr Database EngineerCommented:
Did you try this style?

SELECT TableA.*
FROM TableA
WHERE TableA.Part NOT IN
     (SELECT NonPart FROM TableB)

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
rshafakianCommented:
SELECT * FROM myTable WHERE part NOT IN (SELECT otherpart FROM myOtherTable)
WaldaIncAuthor Commented:
No. I didn't try NOT IN.

Man I was trying <> and stuff like that.  I had the inner select correct just didn't use the NOT IN

Thank you so much. I think you save me pulling a few hairs out today.
StanyslawCommented:
as an alternative - OUTER JOIN may be used.

SELECT TableA.* FROM TableA LEFT OUTER JOIN TableB ON Part=NonPart WHERE NonPart is NULL

In some engines (not sure about MS SQL 2005) it works faster.
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 2005

From novice to tech pro — start learning today.