4

I just stumbled upon a soql query where somebody did the following

List<String> names = new List<String>{'John', 'Moe', 'Jeanette', 'Tony'};

List<Client__c> clients = [
    SELECT Id
    FROM Client__c
    WHERE First_Name__c = :names
];

I am not familiar with the usage of the equal sign(=) to check against a list/set as opposed to WHERE First_Name__c IN :names.

I am having a hard time finding the difference as it seems like using = works similarly but does it work the same way? Are there any dangers to using it?

Adrian Larson
  • 149,971
  • 38
  • 239
  • 420
Arthlete
  • 4,917
  • 9
  • 59
  • 103
  • 2
    Note: this special behavior is not available in any API; it only works in Apex code. – sfdcfox Jan 18 '19 at 19:53
  • @sfdcfox Can you provide an example if you don't mind? What API would this not work in? – Arthlete Jan 18 '19 at 20:46
  • 2
    Any of them. For example, in the Apex Data Loader, you cannot say select name from lead where name = ('john doe','jane doe'). This is one of the few magic behaviors of SOQL in Apex. – sfdcfox Jan 18 '19 at 20:52

2 Answers2

8

I've not seen a difference functionally; I strongly prefer using IN as that is what would be required in SQL, and it it more accurate, and more descriptive.

First_Name__c is not logically equal to a list.

Using the syntax First_Name___c IN :names makes it clear that names is a collection, and that the developer knows that it's a collection

Mike Lockett
  • 301
  • 1
  • 4
3

They are functionally equivalent. I agree with Mike that IN is more syntactically clear, but either one works.

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

This equivalence can be quite useful when building dynamic SOQL.

Adrian Larson
  • 149,971
  • 38
  • 239
  • 420