function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ron Mccrerey 20Ron Mccrerey 20 

Why do I get different results from SOQL in Query Editor than when run as anonymous apex code?

SOQL Query:
Select (Select product2.Name From OpportunityLineItems)  From Opportunity  
        Where Id IN 
        (Select OpportunityId from opportunityLineItem where Name  like '%LS00425%')
Returns: [{"Product2":{"attributes":{"type":"Product2","url":"/services/data/v48.0/sobjects/Product2/01t1I000004O9fMQAS"},"Name":"STE9923"}},{"Product2":{"attributes":{"type":"Product2","url":"/services/data/v48.0/sobjects/Product2/01t1I000004OzGwQAK"},"Name":"LS00425"}}]

Apex Query:
List<Opportunity> myOpportunity = [Select (Select product2.Name From OpportunityLineItems)  From Opportunity  
        Where Id IN 
        (Select OpportunityId from opportunityLineItem where Name  like '%LS00425%')];
Returns:
(Opportunity:{Id=0061I00000JT2c5QAD, RecordTypeId=0121I000000NvQqQAK})
AbhishekAbhishek (Salesforce Developers) 
Hi Ron,

Your query is answered in the below blogs,

https://salesforce.stackexchange.com/questions/85948/soql-execute-anonymous-and-query-editor-returning-different-results

https://salesforce.stackexchange.com/questions/161886/different-results-for-same-query-in-anonymous-execute-and-regular-class-executio

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks.