Question

I have a list with 10 items on my IN clause, in my SELECT only 8 items were found. how do you know which items contained in the IN clause were not found?

SELECT * FROM SOME_TABLE WHERE ID IN (1,2,3,4,5,6,7,8,9,10)

My table is :

**ID   NAME** 
------------------
1    JOAO 
2 JOSE
3 WILLIAM
4 MARIA
5 CARLOS
6 BENJAMIN
7 DANIELLE
9 VERA
11 JOAQUIM
Was it helpful?

Solution

If you need to know what items listed in the IN clause did not find a match in the table, you could take advantage of a user-defined or built-in collection - nested table or variable array. The example below uses built-in collection sys.odcinumberlist() elements of which are of number data type:

/*sample of data */ 
with t1(col) as(
   select level
     from dual
  connect by level <= 11
 )

 select s.column_value as missing_val
   from t1      /* here you list the elements as you would using IN clause*/
  right join table(sys.odcinumberlist(1,2,3,4,5,6,7,8,9,10, 70)) s
     on (t1.col = s.column_value)
  where t1.col is null

Result:

MISSING_VAL
------------
          70

if elements in your list are of varchar data type, the OdciVarchar2List() collection can be used or ODCIDateList() for elements of date data type. You also free to create your own SQL type collection. For example:

create or replace type T_Type_Name is table of number
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top