+ 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!