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
Adarsh NandanAdarsh Nandan 

Best Way to Use Parent Child Query and Child Parent QUery

Just I always getting Confused in ParentChild Query and  Child Parent Query  Can u please Explain it Properly with Examples ??
Best Answer chosen by Adarsh Nandan
NagendraNagendra (Salesforce Developers) 
Hi Adarsh,

Use Case:

I have the university as parent object & college as a child object.
There is a master-detail relationship defined on child object i.e college

I have a parent-child soql as follows
List<University__c> univList=[select Name,(select Name from colleges__r) from university__c];
system.debug(univList);

Also I have a child-parent soql as follows
List<college__c> univList=[select Name,university__r.Name from college__c
system.debug(univList);

when I execute the parent-child query I do not get any child related records.
Also when I execute child-parent query I do get the name of the college but the university ID I get but I want the university name also not the ID

There are 2 things to consider before you decide which type of query to use,

1) Parent records must have at least one or more child records.
       Use child to parent relationship query.

2) Parent records may or may not have child records
       Use parent to child relationship query

Make sure that you have checked the child relationship name for university__c object. In child-parent soql the field university__r.Name might be the autonumber. Please do check whether the university name is stored on any other field on the university__c record. If so use that field in the relationship query.

1)Parent to Child SOQL query for the above use case:
for(University__c u: [select Name,(select Name from colleges__r) from university__c])
{   
    for(College__c c:u.Colleges__r)
         System.debug('University Name:'+u.Name+'   College Name:'+c.Name);
}
"This code will give you all universities name and colleges under university"

2)Child to Parent SOQL query for the above use case:
For(College__c c:[select Name,university__r.Name,university__c from college__c])
{
  system.debug('College Name:'+c.Name+'     University Name:'+c.university__r.Name);
}
"This code will give you all colleges name with respected university."

Hope this helps.

Please mark this as solved if the information helps so that it gets removed from the unanswered queue which results in helping others who are encountering a similar issue.

Thanks,
Nagendra

All Answers

NagendraNagendra (Salesforce Developers) 
Hi Adarsh,

Use Case:

I have the university as parent object & college as a child object.
There is a master-detail relationship defined on child object i.e college

I have a parent-child soql as follows
List<University__c> univList=[select Name,(select Name from colleges__r) from university__c];
system.debug(univList);

Also I have a child-parent soql as follows
List<college__c> univList=[select Name,university__r.Name from college__c
system.debug(univList);

when I execute the parent-child query I do not get any child related records.
Also when I execute child-parent query I do get the name of the college but the university ID I get but I want the university name also not the ID

There are 2 things to consider before you decide which type of query to use,

1) Parent records must have at least one or more child records.
       Use child to parent relationship query.

2) Parent records may or may not have child records
       Use parent to child relationship query

Make sure that you have checked the child relationship name for university__c object. In child-parent soql the field university__r.Name might be the autonumber. Please do check whether the university name is stored on any other field on the university__c record. If so use that field in the relationship query.

1)Parent to Child SOQL query for the above use case:
for(University__c u: [select Name,(select Name from colleges__r) from university__c])
{   
    for(College__c c:u.Colleges__r)
         System.debug('University Name:'+u.Name+'   College Name:'+c.Name);
}
"This code will give you all universities name and colleges under university"

2)Child to Parent SOQL query for the above use case:
For(College__c c:[select Name,university__r.Name,university__c from college__c])
{
  system.debug('College Name:'+c.Name+'     University Name:'+c.university__r.Name);
}
"This code will give you all colleges name with respected university."

Hope this helps.

Please mark this as solved if the information helps so that it gets removed from the unanswered queue which results in helping others who are encountering a similar issue.

Thanks,
Nagendra
This was selected as the best answer
Lukesh KarmoreLukesh Karmore
Hey nagendra ,
An object having more records is parent and which has less is child .right or wrong ???
Clear plzz..
And why u use API name University__c ??
We use API name of custom object in SOQL right.. 
Clear the doubt one by one.. Thank you..
​​​