+ Start a Discussion
Bhupathi YadavBhupathi Yadav 

How to write soql queryto get grant parent records using child Objects

Hi Bhupathi Yadav,  
  • Basic Examples of SOQL RELATIONSHIPS:
  • Child > Parent (Standard Object)
Account.NumberOfEmployees,Account.CleanStatus from Contact
  • Child >Parent(Custom Object)
Selectid,COLLEGE__r.Name,COLLEGE__r.Contact__c,COLLEGE__r.Count__c,COLLEGE__r.Highest_Marks__c,COLLEGE__r.Address__cfrom Studnt__c
  • Parent >Child(Standard object)
select Name, Industry, (select AssistantName, Email from contacts)from ACCOUNT
  • Parent >Child (Custom Object)
select id,Name,(select Studnt__c.name__c from Studnts__r) from College__C
I hope it will be helpful.

Best Regards
Roshni RahulRoshni Rahul

Please check the link below :

Hope it helps,

Ajinkya DhasAjinkya Dhas
Hi Bhupathi,

Please follow the below explanation and examples to solve your problem ☁️⚡️

1. Child To Parent Relationship :

Consider Contact (Child) and Account (Parent) relationship.

In the child to parent relationship, relationship name will be a parent object which means a foreign key is an Account object.
#1 Example :
SELECT Contact.FirstName, Contact.Account.Name From Contact

#2 Example : Write a query to fetch list of Contact and Account names from contact object where Account.Industry = 'Media'
SELECT Id, Name, Account.Name FROM Contact WHERE Account.Industry='Media'
NOTE: When we use relationship name to the custom objects in the SOQL query. We should append ObjectName__r to the relationship name.

For Example :
List<child__c> ch = [SELECT Id, Name, parent__r.FirstName, parent__r.LastName__c from child__c WHERE age__c < 25];
NOTE: When you are writing a query from child to parent relationship always a relation will be master-detail field name (or) lookup field name.

For Example: 
write a query to fetch a list of the transaction along with Account type and customer names whose transaction type is a deposit. (NOTE: Master-Detail field in the transaction is customer details__c).
List<transaction__c> tr = [SELECT Id, CustomerDetails__r.CName__c, customerDetails__r.AccountType__c, Name FROM Transaction__c];

So these are the ways you can use the child to parent relationship query efficiently.

For More Information Please Follow Below Link To Know More :

Wishing You Happy Learning ☁️⚡️
Please Let Me Know If This Helps 

Thank You,
Ajinkya Dhas