How to select only those objects that have child relationship?

Let’s say I have object Parent that has a child relationship Child__r. I’d like to select only those parents that have children through SOQL. How can I do that?

Answer

You can use a subquery to filter out Parent Records that don’t have a related child,

eg

Select Id, Name from Account where Id In (Select AccountId from Contact) 

In your case

Select Id, Name from Parent__c where Id In (Select ParentId__c from Child__c)

Attribution
Source : Link , Question Author : ipavlic , Answer Author : techtrekker

Leave a Comment