+ Start a Discussion
shra1_devshra1_dev 

Nested Queries in soql

Hi,

 

   i'm new to apex. I need help in writing nested Query in soql.

 

I need the following nested query in SOQL form. IS it possible write in this way?Then how to write it in SOQL.

please help me

 

the query is written in sql form:

 

select emailid from userprofile where empname in (select deptHead from department where deptId in (select deptId from Userprofile where empId='1001'));

 

-Shravan

Best Answer chosen by Admin (Salesforce Developers) 
naresh johnnaresh john

Please see this link. http://blog.jeffdouglas.com/2010/02/22/soql-how-i-query-with-thee-let-me-count-the-ways/   

 

It has sufficient information.

 

Thanks,

Naresh

All Answers

naresh johnnaresh john

Please see this link. http://blog.jeffdouglas.com/2010/02/22/soql-how-i-query-with-thee-let-me-count-the-ways/   

 

It has sufficient information.

 

Thanks,

Naresh

This was selected as the best answer
DharmeshDharmesh

You can do one level of nested query at like below

 

List<Contact> contacts = [select id from Contact c where c.AccountId in (select id from Account ) ];

 

but if you add one more level like 

List<Contact> contacts = [select id from Contact c where c.AccountId in (select id from Account where createdby in (select id from User) ];

it gives error

ToyetoyetoyeToyetoyetoye

Thanks for sharing that link!

shra1_devshra1_dev

Hi ,

 

So dharmesh is it not possible to do three level nested query in SOQL.

i'm still having the difficulty in doing this query

 

 

thanks to all...

DharmeshDharmesh

Yes the third level sub query will give error.