being a new learner im unable to fetch accounts which have contacts only with this Query==> select name,(select lastname form contacts) from account<== with this getting all accounts
1 Answer
Use this query - the outer query looks for Accounts only in the set of Contacts that have an AccountId populated.
select id, name from Account where ID IN (select accountId from Contact)
-
-
1@salesforceNT This approach (using another query in the
WHERE
clause) is called a "semi join" by Salesforce. There's documentation for this (comparison operators in the SOQL and SOSL reference) in case you want to dig deeper.– Derek FOct 11, 2020 at 1:14