I'm trying to figure out an efficient (Oracle) SQL statement that verifies whether a column contains a specific list of values at least once.
One option would be to filter for that list, output all distinct values and then count them. So, something like this:
SELECT count(*)
FROM (
SELECT DISTINCT columnname
FROM table
WHERE columnname in ('a', 'b', 'c')
)
;
(And then check whether count(*) returns the number 3)
The problem with this is that the DISTINCT statement looks at the whole table, which is very bad performance-wise. All three values of my list could be at the very beginning, so i don't need to look at the millions of other rows. I only want to know that the column contains 'a', 'b' and 'c'.
Does anyone has an idea to solve this efficiently?
Thanks in advance!