SQL - Find which values in List is not in table

I have been provided a list of approx 1000 values.  I need to perform an update based on these ID's.  I performed a selection on this list.  As an example

Select * from myTable where id IN (1, 2, 3, 4, 5, .... )

The results returned 995 out of the 1000 id's that were provided.  Is there a way to write a script to determine which of the 5 id's that were provided are not in the list?
LVL 2
CipherISAsked:
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.

Jim HornMicrosoft SQL Server Data DudeCommented:
Create a new table (ids) with a single column (id) and insert your list into it.

>Is there a way to write a script to determine which of the 5 id's that were provided are not in the list?
SELECT ids.ID
FROM ids
   -- LEFT JOIN means return all rows from the list ids...
   LEFT JOIN myTable mt ON mt.id = ids.id
-- And then filter only on the ones not in MyTable
WHERE mt.id IS NULL

Open in new window


<edited after original post>
David L. HansenCEOCommented:
SELECT * FROM myTable WHERE NOT IN (1,2,3,4,5,...)

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
awking00Information Technology SpecialistCommented:
With the list in a table, idlist -
select id from idlist except select id from mytable;
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.

CipherISAuthor Commented:
Sorry, I should clarify.  There are 100,000 records in the table.  I was provided a list of 1000 to search for but only 995 returned and I want to know which 5 in the list did not return.
CipherISAuthor Commented:
What I did was used the list used your approach.  I created two internal tables.  One to select from the table creating an internal table #t1.  Then I created an internal table #t2 from the list that I was provided to search with.  I used your example to search for the nulls.  There were none.  Found out that there were duplicates so I had to write another sql statement to find the duplicate.  Thank You.
Jim HornMicrosoft SQL Server Data DudeCommented:
>I was provided a list of 1000 to search for but only 995 returned and I want to know which 5 in the list did not return.
Yep, did you try the first comment in this question?
awking00Information Technology SpecialistCommented:
That's exactly what the set operator would do. If you create a one table for the list, you don't need another.
select id from list except select id from mytable will produce any ids in the list not in the table and will do so regardless of duplicates.
David L. HansenCEOCommented:
Glad to help CipherIS.
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 2008

From novice to tech pro — start learning today.