Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limit on the WHERE col IN (...) condition

I'm using the following code:

SELECT * FROM table WHERE Col IN (123,123,222,....) 

However, if I put more than ~3000 numbers in the IN clause, SQL throws an error.

Does anyone know if there's a size limit or anything similar?!!

like image 934
jDeveloper Avatar asked Jul 01 '09 14:07

jDeveloper


People also ask

What are the limitations of WHERE condition in SQL?

The maximum number of clauses in a WHERE clause is 40. LONGVARBINARY and LONGVARCHAR columns can be compared to literals of up to 255 characters in length, but cannot be compared using parameters.

Is there any limit on in clause in MySQL?

From my experience the maximum values is 1000 values in clause IN ('1',....,'1000') , I have 1300 value in my excel sheet,I put them all into IN ,MySQL return only 1000 .

How many conditions can a WHERE clause have?

You can specify multiple conditions in a single WHERE clause to, say, retrieve rows based on the values in multiple columns. You can use the AND and OR operators to combine two or more conditions into a compound condition. AND, OR, and a third operator, NOT, are logical operators.

What is the limit of in clause in DB2?

what is the upper limit for predicate WHERE IN in DB2. AFAIK, an SQL statement in DB2 (LUW) can be up to about 2 MiB, so you could in principle have somewhere in the region of 250k terms in the IN clause, allowing 8 characters per term including the comma.


2 Answers

Depending on the database engine you are using, there can be limits on the length of an instruction.

SQL Server has a very large limit:

http://msdn.microsoft.com/en-us/library/ms143432.aspx

ORACLE has a very easy to reach limit on the other side.

So, for large IN clauses, it's better to create a temp table, insert the values and do a JOIN. It works faster also.

like image 54
tekBlues Avatar answered Sep 22 '22 13:09

tekBlues


There is a limit, but you can split your values into separate blocks of in()

Select *  From table  Where Col IN (123,123,222,....) or Col IN (456,878,888,....) 
like image 38
Iain Hoult Avatar answered Sep 24 '22 13:09

Iain Hoult