Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle SQL -- find the values NOT in a table

Tags:

sql

oracle

Take this table WORDS

WORD
Hello
Aardvark
Potato
Dog
Cat

And this list:

('Hello', 'Goodbye', 'Greetings', 'Dog')

How do I return a list of words that AREN'T in the words table, but are in my list?

If I have a table that "contains all possible words", I can do:

SELECT * from ALL_WORDS_TABLE
where word in ('Hello', 'Goodbye', 'Greetings', 'Dog')
and word not in 
(SELECT word from WORDS
where word in ('Hello', 'Goodbye', 'Greetings', 'Dog')
);

However I do not have such a table. How else can this be done?

Also, constructing a new table is not an option because I do not have that level of access.

like image 511
Jeremy Avatar asked Apr 03 '12 15:04

Jeremy


People also ask

How do you find records not exist in another table in Oracle?

We can get the records in one table that doesn't exist in another table by using NOT IN or NOT EXISTS with the subqueries including the other table in the subqueries.

What does != Mean in Oracle?

It (<>) is a function that is used to compare values in database table. != (Not equal to) functions the same as the <> (Not equal to) comparison operator.

Does Isnull work in Oracle?

ISNULL replaced the Oracle NVL function in the SQL server. When an expression in SQL server is NULL, the ISNULL function allows you to return an alternative value for the null. ISNULL checks whether the value or an expression is true or false.


1 Answers

Instead of hard coding the list values into rows, use DBMS_DEBUG_VC2COLL to dynamically convert your delimited list into rows, then use the MINUS operator to eliminate rows in the second query that are not in the first query:

select column_value 
from table(sys.dbms_debug_vc2coll('Hello', 'Goodbye', 'Greetings', 'Dog'))
minus
select word
from words;
like image 148
Wolf Avatar answered Sep 21 '22 05:09

Wolf