0

In the example below, is it possible to filter only the Accounts that have a Contact that meets the Contact filter? As it is below it will only return Accounts where the Industry is 'media', but it will return Accounts that don't have a Contact that meets the CreateBy Alias requirement.

SELECT Name,
  (
    SELECT LastName
    FROM Contacts
    WHERE CreatedBy.Alias = 'x') 
 FROM Account WHERE Industry = 'media'

1 Answer 1

3

You can add a Left Inner Join:

SELECT ... FROM Account WHERE ...
AND Id IN (SELECT AccountId FROM Contact WHERE ...)
2
  • Perfect, exactly what I was looking for. Thanks! Is it possible to expand this to filter the Accounts that only have 1 Contact that matches the criteria?
    – Todd
    Mar 28 at 0:02
  • If you wanted to do that type of filtering, you would need to do an Aggregate query on Contact where you GROUP BY AccountId HAVING count(Id) = 1.
    – Adrian Larson
    Mar 28 at 0:46

You must log in to answer this question.

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