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
Lakshmi SLakshmi S 

soql query for fetching records from Account not related to Contact object?

Hi Dev's,

SOQL query for fetching records from Account not related to Contact object?(Only account records, which are not related to contact)?

Regards
Lakshmi

Thanks in advance..
 

All Answers

bilal malikbilal malik
        List<Contact> con = [Select name,Account.id from contact];
        List<Id> acc_ids = new List<Id>();
        for(Contact c : con){
            if( c.account.id != null)
            {
                acc_ids.add(c.account.id);
            }
        }
        List<Account> list_accounts = [Select id,name from account where id not in :acc_ids];
        system.debug(list_accounts);
Mahesh K 22Mahesh K 22
Hi Lakshmi

1.This query for without relationship with contact ,that means only account records.

list<account> acc = [select id,name from account where Id not in(select accountid from contact)];
system.debug('========='+acc.size());
 

2.This query for relationship between contact and account records.

list<account> acc2 = [select id,name from account where Id in(select accountid from contact)];
system.debug('========='+acc2.size());