I have to write a batch that will do some updates on parent records based on children.

Let's say I have objects Parent__c, Child1__c and Child2__c.

Children both have a Master-Detail to parent, but now I need to query all Parent__c record that have any children. I tried the following:

query = 'SELECT Id FROM Parent__c ' + 
        'WHERE Id IN (SELECT Parent__c FROM Child1__c) ' + 
        'OR Id IN (SELECT Parent__c FROM Child2__c)';

But that gave me this error:

First error: Semi join sub-selects are not allowed with the 'OR' operator

So the question is: how can I query all parent records that have at least 1 child?

1 Answer 1


Yes, the easy way would be a roll up summary for each child, then a formula to sum the child counts.

WHERE BothChildCount__c > 0

Having of course the formula for BothChildCount__c look like:

Child1Count__c + Child2Count__c
  • but no programmatic way of achieving this?
    – Novarg
    Commented Dec 19, 2017 at 14:34
  • 2
    I would argue that it is programmmatic. Creating formulas to extend the query language is an age old strategy with SOQL. And Rollup Summary Fields are just an extension of this pattern.
    – Adrian Larson
    Commented Dec 19, 2017 at 14:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .