You need to sign in to do that
Don't have an account?

SOQL to find Accounts with no contacts
I'd like to run a query to find all Accounts without Contacts. I'm not sure how to form the SOQL query to acheive that.
Can anyone make any suggestions?
TIA
I am not sure it works.
but try something simillar
select Id form Account where Not Id In (select AccountId form Contact)
hope this may give you an idea. By the way i have not tried subqueries yet though
Hey
That's almost right. The idea is correct, the syntax just needs a tweak
select Id,name from Account where Id not In (select AccountId from Contact)
Good answers.
Cheers,
Wes
select Id from Account where Id not In (select AccountId from Contact).
I have already run this soql
For fetching records (account) who have no contacts plz refer below soql.
This code will run.
select Id, name from Account where Id In (select AccountId from Contact)
if we are using not in mentioned in place of In, like the soql "select Id, name from Account where Id not In (select AccountId from Contact) "
than it will return all Accounts which have contacts.
List<Contact> conList=new List<Contact>();
List<Account> accountList=new List<Account>();
conList=[Select AccountId From Contact Limit 10000];
Set<Id> idSet=new Set<Id>();
if(conList.size()>0){
for(Contact con:conList){
idSet.add(con.AccountId);
}
}
if(idSet.size()>0){
accountList=[Select Id,Name From Account where Id Not In:idSet Limit 10000];
}
system.debug('accountList->'+accountList);
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
select name from account where id not in (select accountid from contact)
List<Account> accList = new List<Account>();
for(Account result : results){
if(result.Contacts.size() == 0){
accList.add(result);
}
}
System.debug('Acc List'+accList);
Good Luck Guys.