4

While testing some SOQL queries I noticed that the Equal operator behaves like the IN operator when using a list bind variable

All the queries below run without any error and return the same result

List<String> accNames=new List<String>{'Acme Corporation','Smith Enterprises'};
System.debug('Dynamic Query bind list with Equal operator :'+Database.query('select Name from account where Name = :accNames'));
System.debug('Static Query bind list with Equal operator :'+[select name from account where name = :accNames]);
System.debug('Dynamic Query bind list with IN operator :'+Database.query('select Name from account where Name in :accNames'));
System.debug('Static Query bind list with IN operator :'+[select name from account where name in :accNames]);

Debug

DEBUG|Dynamic Query bind list with Equal operator :(Account:{Name=Acme Corporation, Id=0010Y00000E7whoQAB}, Account:{Name=Smith Enterprises, Id=0010Y00000E7wXsQAJ})
DEBUG|Static Query bind list with Equal operator :(Account:{Name=Acme Corporation, Id=0010Y00000E7whoQAB}, Account:{Name=Smith Enterprises, Id=0010Y00000E7wXsQAJ})
DEBUG|Dynamic Query bind list with IN operator :(Account:{Name=Acme Corporation, Id=0010Y00000E7whoQAB}, Account:{Name=Smith Enterprises, Id=0010Y00000E7wXsQAJ})
DEBUG|Static Query bind list with IN operator :(Account:{Name=Acme Corporation, Id=0010Y00000E7whoQAB}, Account:{Name=Smith Enterprises, Id=0010Y00000E7wXsQAJ})

Are the IN and = operators equal when using a list bind variable?

6

Yes, = and IN operators are functionally equivalent when filtering against a list. It is not well covered in the Force.com SOQL and SOSL Reference for Comparison Operators, but the operators are interchangeable as far as I have observed, if the bind variable is a List.

One notable exception is when using a Left Inner Join, where only IN will work.

Valid

SELECT Id FROM Profile WHERE Id IN (SELECT ProfileId FROM User)

Invalid

SELECT Id FROM Profile WHERE Id = (SELECT ProfileId FROM User)

See also: How to find out if an Object is a collection or single value?

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.