+ Start a Discussion
RoyalRoyal 

how to write a query on account, account should not have relation with at least one contact also ?

Best Answer chosen by Royal
RoyalRoyal
yes Naval your correct but small modification is innser query is return contact id's but outer query expeting account id's so query should be like bellow.

SELECT id FROM Account WHERE id Not IN (SELECT Accountid FROM Contact WHERE Accountid !=null)

Inner query also should be return Accountid.

Thanks Naval.
 

All Answers

Prakash NawalePrakash Nawale
Select Id ,Name, (select Id from Contacts where accountid=null ) from Account
RoyalRoyal
No Prakash, may be you are not understand my question.

from your query i am getting all accounts, those accounts have relation with contacts or not  like all accounts.

but my question is i want to get the accounts which is not have relation with contact ?

I hope this can be clear..
Vijay NagarathinamVijay Nagarathinam
Hi Boyapati,

You are asking to query the number of contacts without AccountId rite..?

Thanks,
Vijay
RoyalRoyal
yes Vijay, but i want to write quesry on Account object for the same result, is it possible ?
Vijay NagarathinamVijay Nagarathinam
Hi Boyapti,

Use the below query to find the contact records without account.

Select Id, Name, (Select Id, Name from Contacts where AccountId = NULL) from Account

Thanks,
Vijay
Naval Sharma4Naval Sharma4

Hi Boyapati,

Use the following query.

[SELECT Id FROM Account WHERE Account Id Not In (SELECT Id FROM Contact WHERE AccountId != Null)]

Let me know if this works for you.
RoyalRoyal
yes Naval your correct but small modification is innser query is return contact id's but outer query expeting account id's so query should be like bellow.

SELECT id FROM Account WHERE id Not IN (SELECT Accountid FROM Contact WHERE Accountid !=null)

Inner query also should be return Accountid.

Thanks Naval.
 
This was selected as the best answer