[ACCEPTED]-SOQL query with subquery-soql

Accepted answer
Score: 20

You could use a semi-join on contacts to 3 filter the contacts to the set you want, something 2 like this

[select id, firstname, lastname, 
     (select id, status, comment__c, campaignid from CampaignMembers)
     from contact where id in 
          (select contactId from campaignMember where campaignId in :cams];

Another option would be to drive 1 from campaignMmeber instead.

[select contact.id, contact.firstname, contact.lastname, 
  status, comment__c, campaignId from campaignMembers 
  where contactId !='' and  campaignId in :cams];

More Related questions