0

I have this query:

SELECT Id, (SELECT Id FROM Account.Contacts) FROM Account

This would return something like this:

[
    {
        "Id": "0015O000001W683QAC"
        Contacts: [{"Id": "0015O000001W612QQA"}]
    },
    {
        "Id": "0015O000001W683QAC"
        Contacts: null
    }
]

How do I include the Account.Contacts in the WHERE clause like:

SELECT Id, (SELECT Id FROM Account.Contacts) FROM Account WHERE Account.Contacts != null
2
  • Hi Carl, Did you try using the query as below. Select Id, Name,(SELECT Id FROM Account.Contacts) From Account Where id IN (Select AccountId From Contact) May 5, 2022 at 6:04
  • Yeah, it worked. Thanks.
    – Carl Ortiz
    May 5, 2022 at 6:33

1 Answer 1

2

You will use ANTI JOINS and is well documented in the official docs here.

For Only Accounts with Contacts

 SELECT Id, Name, (SELECT Email FROM Contacts)
 FROM Account
 WHERE Id IN (SELECT AccountId FROM Contact)
1
  • It worked. Thanks.
    – Carl Ortiz
    May 5, 2022 at 6:33

You must log in to answer this question.

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