Filter on Polymorphic Relationship Fields

You can search polymorphic relationship fields on a SOQL query. A polymorphic relationship is one where the current object can be one of several object types.
To filter on a polymorphic relationship field, use the Type qualifier.
WHERE polymorphicRelationship.Type comparisonExpression
Syntax Description
polymorphicRelationship A polymorphic relationship field in object being queried that can reference multiple object types. For example, the Who relationship field of a Task can be a Contact or a Lead.
comparisonExpression The comparison being made against the object type in the polymorphic relationship. For more information, see fieldExpression syntax . Note that type names returned by Type are string values, like ‘User’.
The following example only returns records where the What field of Event is referencing an Account or Opportunity. If an Event record referenced a Campaign in the What field, it wouldn’t be returned as part of this SELECT.
SELECT Id
FROM Event
WHERE What.Type IN ('Account', 'Opportunity')

See Understanding Relationship Fields and Polymorphic Fields for details on polymorphic relationships, and more filtering examples.