Reputation: 977
I have some values in Excel, I copy the values and it look like this:
1 2 3 4
I want to write a query that select from above data that is not present in a table. Somethink like:
SELECT * FROM [1, 2, 3, 4] as foo where foo IS NOT ( SELECT id from table )
Supposing id column have 1,2,4 the query answer will be 3.
Preferably a query that can work in postgres and sql server
Thank you!
Upvotes: 14
Views: 8022
Reputation: 125414
select *
from
(values (1),(2),(3),(4)) v (id)
left join
t using (id)
where t.id is null
Upvotes: 3
Reputation: 1270431
You need a left join
or something similar for this. Here is one method:
SELECT v.*
FROM (VALUES (1), (2), (3), (4)) v(foo)
WHERE foo NOT IN ( SELECT id from table );
Upvotes: 18