Skip to Main Content
  • Questions
  • Passing multiple value in IN clause in SQL query

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Mansi.

Asked: February 07, 2017 - 12:26 pm UTC

Last updated: October 06, 2017 - 5:35 am UTC

Version: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit

Viewed 10K+ times! This question is

You Asked

Hi
I have SQL query which takes around 50000 as input value in IN clause. What is best approach to passing value into IN clause which are more than 1000 in count.


and Connor said...

I'd recommend you load the values into a global temporary table, and then do

select *
from my_table
where col in ( select vals from my_gtt );


You *could* do a series of:

where col in ( [1000 vals] )
or col in ( [next 1000 vals] )


and so forth but generally you end with horribly long SQL which takes forever to parse. That's why I recommend the GTT

Rating

  (5 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

RE

GJ, February 08, 2017 - 10:00 am UTC

Is this going to be a user who is going to pickup those 50000 values for use by the procedure via a reporting screen perhaps?. It seems very unlikely.

Another possibility is that a program which is going to feed these 50000 values? then it begs for a join with the source_table_with_50000 values /gtt lookup.




multiple search value using like

A reader, February 08, 2017 - 2:55 pm UTC

CREATE TABLE NAMES(Id integer PRIMARY KEY, Name text);

INSERT INTO NAMES VALUES(1,'one,');
INSERT INTO NAMES VALUES(2,'two,');
INSERT INTO NAMES VALUES(3,'two,one,');
INSERT INTO NAMES VALUES(4,'three,');
INSERT INTO NAMES VALUES(5,'one,three');
COMMIT;

Now I fire below query.

SELECT * FROM NAMES where name like ('%one%','%two%');

then i get error so what's other way to search multiple value.
Connor McDonald
February 10, 2017 - 5:20 pm UTC

Same approach of a temp table will work

select *
from my_table t,
     gtt
where t.col like gtt.col||'%'


use regexp_like

Peter, February 08, 2017 - 5:36 pm UTC

Hi, you can use:

SELECT
*
FROM
NAMES
WHERE
REGEXP_LIKE(NAME,'one|two')

ID NAME
-- -------------------------
1 one,
2 two,
3 two,one,
5 one,three



Thanks

A reader, February 11, 2017 - 4:06 pm UTC

Thanks Connor,
I have tried second approach and it is really creating long SQL. Will try using GTT now.

matching all values from IN clause

Neeraj Bedi, October 05, 2017 - 9:02 pm UTC

I have a table where there are multiple values corresponding to one single value(1:n table)
I want to query and find out the parent value if all the supplied values exist for the parent.

for eg, for below set of data in a table:

name1 val1
name1 val2
name1 val3
name2 bval1
name2 bval2
name3 val98
name3 val99
name3 val100
name4 val100
name4 val10


i want to supply the below input, and expect the mentioned result:


abc, val2,val3      return name1
bval1               return nothing
val100,val10        return name4
val100,val10,val11  return nothing


I would like to know is this possible using an IN query or it'd require something else.

Thanks for your help.
Connor McDonald
October 06, 2017 - 5:35 am UTC

REVIEW_WARNING