I have a use case that needs to query all records only if they have child records. For example:
SELECT Id (SELECT Id FROM Child__r) FROM Parent__c WHERE *Child__r has record*. Please give me a solution for it. Thank you!
4 réponses
You can use the following SOQL to achieve your solution:
SELECT Id FROM Parent WHERE Id IN (SELECT ParentId FROM Child)
So, if you want to fetch all the accounts that have Contact child, use the SOQL below:
SELECT Id FROM Account WHERE Id IN (SELECT AccountId FROM Contact)
Please mark this as the best answer if it helps. Thank you.