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
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.

Aditya P HegdeAditya P Hegde

Hello, 

The link below explain nested queries with simplest example, you would understand the concept and apply to your business requirement. 


https://www.sfdc99.com/2013/06/24/example-how-to-write-a-cross-object-soql-query-part-2/