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
Iceman123Iceman123 

soql help: Is there a way to include a child object in a where clause?

SELECT Name, (SELECT lastname FROM Contacts WHERE CreatedBy.Alias = 'x') FROM Account

 

Take the above example, is there a way (in SOQL only) to only return Account rows where the child relationship subquery ((SELECT lastname FROM Contacts WHERE CreatedBy.Alias = 'x')) is not empty (ie. contains at least 1 child object row)?

 

thanks

Pi

Best Answer chosen by Admin (Salesforce Developers) 
SuperfellSuperfell

You can add a semi-join to the outer query to do this, e.g.

 

select name, (select lastname from contacts where createdBy.Alias='x') from account where id in (select accountId from contact where createdby.alias='x')

All Answers

SuperfellSuperfell

You can add a semi-join to the outer query to do this, e.g.

 

select name, (select lastname from contacts where createdBy.Alias='x') from account where id in (select accountId from contact where createdby.alias='x')

This was selected as the best answer
Iceman123Iceman123

cool thx!