function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
neil12neil12 

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?

 

Thanks

 

 

ryanjuptonryanjupton

neil12,

 

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.

Vinny12Vinny12
Thanks alot !!

Do SQL support " not in" ??

Yes, it is a 1-m relationship
ryanjuptonryanjupton

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