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
rakesh kondarakesh konda 

subquery concept

Hello Guys ,

i am confused with the sub query concept in triggers can any one explain me the sub query concept
for eg:
select id,(select id from opportunities) from Account where id IN: Trigger.New

Thanks & Regards,
konda
 
Best Answer chosen by rakesh konda
Apoorv Saxena 4Apoorv Saxena 4
Hi Rakesh,

Sub Queries or Inner Queries are used when we need to get all the associated child records from parent record.

So whenever you need to query for Parent-to-Child Relationships, you will need to use sub query.

In the example provided by you :

select id,(select id from opportunities) from Account where id IN: Trigger.New

The following query returns the id for each account in trigger.new and then for each account another collection of opportunities containing id.

So suppose there is an Account on which your trigger is executed and it has 3 opportunities associated with it, so this query will return Id of that Account and 3 ID's of child opportunities associated with that account.

Hope this helps!

Please let me know if you have any doubts, else if this answered your question then mark it as Solved so that others can view it as a proper solution.

Thanks,
Apoorv