tags:

views:

54

answers:

6

I am having a table Student and i have a set of 20 names.

by using his sql

select name from student st where st.name in (
 'abc', 'xyz', . . .
)

i can find out all student names which are in table and in the set.
Now, how can i find out which out of these 20 names are not in Student table.

A: 

SELECT NOT IN ?

postgresql: http://archives.postgresql.org/pgsql-sql/2002-08/msg00322.php

nilphilus
A: 
select name from student where name not in (
select name from student st where st.name in (
 'abc', 'xyz', . . .
))

EDIT: I might not get what you are looking for. Please run following script and it is giving the results.

declare @student table
(
    name varchar(50)
)

insert into @student select 'james'
insert into @student select 'will'
insert into @student select 'bill'
insert into @student select 'adam'
insert into @student select 'jon'
insert into @student select 'white'
insert into @student select 'green'

select name from @student where name in ('james', 'will', 'bill')

select name from @student where name not in (select name from @student where name in ('james', 'will', 'bill'))
Muhammad Kashif Nadeem
this is not giving desired results
Rakesh Juyal
+2  A: 

I'm assuming you want the names themselves. One option is to create a table with all the available student names, then select from it rows which don't have corresponding rows in the student tables, it will look something like this
select name from student_names
where name not in (select name from students)

JohnoBoy
+2  A: 
CREATE TABLE student(name VARCHAR(255));
INSERT INTO student VALUES('a'), ('abc');
CREATE TABLE temp(x VARCHAR(255));
INSERT INTO temp VALUES('abc'), ('xyz');
SELECT x FROM temp WHERE 
  NOT EXISTS (SELECT * FROM student st WHERE st.name = x);

Depending on the database you use, there might be an easier way. There is also a way using UNION.

Thomas Mueller
This is a bit neater than my solution. I didn't know about the `VALUES('abc'), ('xyz')` syntax so +1 for that alone. Depending on the database, this could probably be a table variable rather than a temporary physical table.
David Hedlund
Some databases support easier (non-standard) ways, for example the H2 database supports: SELECT temp.x FROM TABLE(x VARCHAR = ('abc', 'xyz')) temp WHERE NOT EXISTS (SELECT * FROM student st WHERE st.name = x); Disclaimer: I implemented this extension in H2.
Thomas Mueller
Actually the VALUES('abc'), ('xyz') syntax doesn't work in all databases (yet). Which is unfortunate.
Thomas Mueller
can't we do that without creating another table. Or if possible using temp table.
Rakesh Juyal
Yes, you can use a temporary table. But unfortunately, not all databases do support temporary tables. If you tell us what database you use we can provide a 'custom' solution. I know SQL is supposed to be a standard.
Thomas Mueller
@Thomas: i am using db2
Rakesh Juyal
A: 
DECLARE @names table ( name varchar(100) )
INSERT INTO @names VALUES ('abc')
...
INSERT INTO @names VALUES ('xyz')

SELECT name FROM @names WHERE name NOT IN ( SELECT DISTINCT Name FROM Student )
David Hedlund
A: 

Assuming that the tool you are using can generate dynamic sql, try generating an inline view consisting of your set of user names - like so:

select 'abc' check_name union all
select 'xyz' check_name union all

...

(The syntax of the inline view may depend on which version of SQL you are using - some versions of SQL require a from [dummy_table] clause in select statements that are not accessing a table.)

Then construct a query using this inline view with a not exists in student clause, like this:

select check_name from (
select 'abc' check_name union all
select 'xyz' check_name union all

...

) ilv where not exists
(select null from student st where st.name = ilv.check_name)
Mark Bannister