Oracle has a limit of 1000 items for its IN clause. Is there a similar limit in SOQL? The SF documentation on the IN clause does not mention any limit. To clarify the definition of an item, the following example has 2 items in its IN clause:

SELECT Name FROM Widget__c
WHERE Name IN ('Ring', 'Flange')
  • the above code was taken from the linked SF documentation.

I think you are limited to the size of the query not exceeding 20,000 characters.


SOQL statements can't exceed 20,000 characters. For SOQL statements that exceed this maximum length, the API returns a MALFORMED_QUERY exception code ; no result rows are returned.

Maybe someone can weigh in on whether this applies to bound lists - if just comma-delimiting a bunch of IDs, one won't get more than about 1000 in there.

Edit: @sfdcfox has run an experiment and concluded bind variables do not contribute to query length.

Edit: It seems that when using bind variables, the number of items in an IN clause is determined by the amount of available heap space.

| improve this answer | |
  • 3
    It's 20,000 characters now. – sfdcfox Jan 31 '14 at 16:35
  • 8
    If you bind to a list, you can easily get around the 20,000 character limit. For example, I created 6,000 assets (total number of characters for 6,000 18-character ID values would be 125999 characters, including commas and quotes), and then specifically tried to query them like this: map<id, asset> assets = new map<id, asset>([select id from asset limit 10000]); asset[] r = [select id, name from asset where id in :assets.keyset()]; -- this works, even though if you tried to make it a string, it would fail. – sfdcfox Jan 31 '14 at 16:44
  • Since bound variables do not contribute to query length, the maximum number of items in a SOQL IN clause becomes limited by either the maximum number of items in a collection, or by available system memory. Is that correct? – edgartheunready Jan 31 '14 at 17:23
  • 2
    I was able to run a query with an in clause with a bound list of 1.5 million ids (duplicated a list of 1000 unique ids). Adding more that that caused heap space errors. So it looks like when using a bound list, the limit becomes heap space. (here is the code I used to come to this conclusion. I have about 1.5k task objects in the org i ran this code in. pastebin.com/9TEnc9Nz) – edgartheunready Jan 31 '14 at 19:49
  • The above pastebin file has a bug, and i couldn't edit the comment. Here is the bug free code: pastebin.com/T7H7MKNU – edgartheunready Feb 3 '14 at 14:15

I don't think the accepted answer is correct. While the TOTAL length of a SOQL query must be less than 20,000 characters, the WHERE clause can only be 4,000 characters. And the IN clause is part of that 4,000 characters.

Therefore, the actual maximum length of the IN clause is less than 4,000 characters (because the field name and the string ' IN ' contribute to the 4,000 characters.


| improve this answer | |

I tried using a list bind variable (a Map keyset) and as the size of the list grew to over 3300 entries eventually I hit the SF CPU time limit at around 2m 6s. Strangely, the logs showed that the query took a couple of seconds - the timestamp on the "SOQL_EXECUTE_END" log entry was only 2 seconds after the query start time - but the next log entry was 2m 6s later. The CPU limit was exceeded only .5s after that. I don't know what this means (yet) but this is a limit you can hit even if the character limit isn't a problem.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.