Single SOQL query for Accounts without Contacts

How to query those Account which has not any related Contacts ? I have Queried Account and contact in inner query. I found All Account with that Account I looped and find those Account whose Contact equals Null. But I want, can it be acheive by query only no need to loop over Acccout . Please Help

Answer

This query will give you accounts that DO NOT have any contacts.

Select Id, Name From Account Where Id NOT IN (Select AccountId From Contact)

On the flip, you can query all Accounts that do have at least 1 contact like this

Select Id, Name From Account Where Id IN (Select AccountId From Contact)

Attribution
Source : Link , Question Author : SFDC 44 , Answer Author : Chris Duncombe

Leave a Comment