8

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.

6

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.

| improve this answer | |
  • Thanks a lot @crmprogdev. Does the selection of operator affects in case of non-selective query ? – Abbas Jun 8 '15 at 14:36
  • Generally speaking, I wouldn't think so. Instead, I think you'd get a malformed query error. – crmprogdev Jun 8 '15 at 16:05

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.