+ Start a Discussion

SQL Query on Salesforce objects

Hi All,


I have two objects.

Contacts and Funds.


I want all the contact records who didn't fund any to the organization.

This can be determined by the id's


select * from contact,Funds

where contact.id=funds.contactid----> this means there are funds from this contact.


select * from contact,Funds

where contact.id not exists in Funds.contactid--> this means there are no funds from this contact.


This means that there are contact records who gave same funds also who didn't give any funds. We want 2 diff files.


Can someone help me with the SQL Query?








You didn't say much about the relationship so I'm assuming it's a one to many between contact and fund__c. If so and all your looking for is a list of contact ids that don't have matching fund__c ids then you can use a standars anti-join like this 


select id from contact where id not in (select contact__c from fund__c)

 That worked in my simple test using the assumptions I mentioned above. Feel free to respond with more clarification if need be. You can also enhance the query by using the relationship fields to get other related data you might want in your query.

Thanks alot !!

Do SQL support " not in" ??

Yes, it is a 1-m relationship

Yes, SOQL supports not in. Keep in mind though that it makes your query non-selective.