6
/services/data/v26.0/query?q=select+a.Id,+(select+c.Id,+c.FirstName,+c.GUID__c,+c.LastName,+c.Middle_Name__c,+c.Phone,+c.Email,+c.Professional_Role__c,+c.HCP__c+from+Contacts+c)+from+Account+a +where+a.Contacts!=null

I am using this i am running into error of no field found named contacts.

How will i get only those accounts with corresponding contacts which have atleast one contact.

I want to eliminate all those accounts that has no contacts .Please note i want to do it in a single REST API call.

1 Answer 1

14

How about this?

Only Accounts with Contacts

SELECT Id, Name, (SELECT Email FROM Contacts)
FROM Account
WHERE Id IN (SELECT AccountId FROM Contact)

The subquery in SELECT optional of course (do you want the contacts returned too or not?)

Only Accounts that have no Contacts

SELECT Id, Name
FROM Account
WHERE Id NOT IN (SELECT AccountId FROM Contact)
4
  • Thank you.Appreciate quick response and perfect answer.Wish i had one more vote to vote. Commented Nov 8, 2012 at 17:42
  • No probs, it took me a while too before I discovered how to us it too (hard to search for it if you don't know proper keyword). It's "anti joins" in the official docs: salesforce.com/us/developer/docs/api/Content/…
    – eyescream
    Commented Nov 8, 2012 at 17:45
  • 1
    Ya correct.Always getting different perspective to a problem helps it to solve quicker and this site is awesome .Isn't it? Commented Nov 8, 2012 at 17:48
  • +1 - as using inner queries in criteria is least known feature of SOQL Commented Nov 12, 2012 at 4:14

You must log in to answer this question.

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