SOQL Query Where Id “In” Versus “Equals”(=)












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?










share|improve this question




















  • 2





    Note: this special behavior is not available in any API; it only works in Apex code.

    – sfdcfox
    Jan 18 at 19:53











  • @sfdcfox Can you provide an example if you don't mind? What API would this not work in?

    – Arthlete
    Jan 18 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 at 20:52
















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?










share|improve this question




















  • 2





    Note: this special behavior is not available in any API; it only works in Apex code.

    – sfdcfox
    Jan 18 at 19:53











  • @sfdcfox Can you provide an example if you don't mind? What API would this not work in?

    – Arthlete
    Jan 18 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 at 20:52














4












4








4


1






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?










share|improve this question
















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?







apex soql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 26 at 18:27









Adrian Larson

108k19115243




108k19115243










asked Jan 18 at 19:32









ArthleteArthlete

919715




919715








  • 2





    Note: this special behavior is not available in any API; it only works in Apex code.

    – sfdcfox
    Jan 18 at 19:53











  • @sfdcfox Can you provide an example if you don't mind? What API would this not work in?

    – Arthlete
    Jan 18 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 at 20:52














  • 2





    Note: this special behavior is not available in any API; it only works in Apex code.

    – sfdcfox
    Jan 18 at 19:53











  • @sfdcfox Can you provide an example if you don't mind? What API would this not work in?

    – Arthlete
    Jan 18 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 at 20:52








2




2





Note: this special behavior is not available in any API; it only works in Apex code.

– sfdcfox
Jan 18 at 19:53





Note: this special behavior is not available in any API; it only works in Apex code.

– sfdcfox
Jan 18 at 19:53













@sfdcfox Can you provide an example if you don't mind? What API would this not work in?

– Arthlete
Jan 18 at 20:46





@sfdcfox Can you provide an example if you don't mind? What API would this not work in?

– Arthlete
Jan 18 at 20:46




2




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 at 20:52





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 at 20:52










2 Answers
2






active

oldest

votes


















6














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






share|improve this answer































    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.






    share|improve this answer























      Your Answer








      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "459"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f247214%2fsoql-query-where-id-in-versus-equals%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      6














      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






      share|improve this answer




























        6














        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






        share|improve this answer


























          6












          6








          6







          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






          share|improve this answer













          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







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 18 at 19:44









          Mike LockettMike Lockett

          1363




          1363

























              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.






              share|improve this answer




























                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.






                share|improve this answer


























                  3












                  3








                  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.






                  share|improve this answer













                  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.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 18 at 19:47









                  Adrian LarsonAdrian Larson

                  108k19115243




                  108k19115243






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Salesforce Stack Exchange!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f247214%2fsoql-query-where-id-in-versus-equals%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Human spaceflight

                      Can not write log (Is /dev/pts mounted?) - openpty in Ubuntu-on-Windows?

                      File:DeusFollowingSea.jpg