[SalesForce] Difference between IN and = operator in SOQL query

Can somebody please help me to understand the difference between these two scenarios

List<String> contactIdList = new List<String>{'00328000003Jms2',
                         '00328000003JbfQ', '00328000003HdMp', '00328000003HcRS'};

1 – Using IN operator List<Contact> contactList = [SELECT Id, Name From Contact WHERE ID IN :contactIdList];

2 – Using = operator List<Contact> contactList = [SELECT Id, Name From Contact WHERE ID = :contactIdList];

Both the queries give me the same result. Then what is the difference between these two ?

Which operator I should use and how does it affects the non-selective criteria ?

Any other information regarding the same would be much appriciated.

Thanks.

Best Answer

If you're looking for the value of a single variable, then it would make sense that the operator would be "equal" to the value of the specified variable, it wouldn't be "IN" the variable as if your query has to sort among a group of records.

According to syntax, if your variable is a list or set, you'd expect the value to be "IN" the list or set, not "equal" to the value of the list or set. Often times it makes no difference which one you use for a list or set as Apex is smart enough to understand what you intended. It could also be a question of did you want all the values contained in the list or set, or only some of them that makes the difference on which to use that clarifies the intent, but generally if a list or set, using "IN" would be the proper syntax.

Where it's essential to use "IN" is when you use a map. A value of an operator can't be "equal" to a map can it? Instead, it has to be contained "IN" the map.

Related Topic