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]

1 Answer 1

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
1

You must log in to answer this question.

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