0

I have a requirement where I need to soql contacts from account I wrote an soql which should only return account with contacts. Instead I also get accounts without contacts, why?:

[SELECT id, Name, (SELECT id,lastname,firstname FROM Contacts WHERE lastname  LIKE '%b%') 
 FROM Account 
 WHERE id IN (Select AccountId from contact WHERE Contact.Id != null)
 LIMIT 20]
3

Your filter criteria is not the same as the sub-query filters. As such, there are contacts on those accounts, but none that match your criteria. Try this instead:

SELECT id,Name,
    (SELECT id,lastname,firstname FROM Contacts WHERE lastname  LIKE '%b%') 
FROM Account 
WHERE id IN (Select AccountId from contact WHERE lastname  LIKE '%b%')
Limit 20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.