Grandparent - Parent - Child relationships in SOQL query

10:30 AM

I had to write a SOQL query to fetch values form multiple objects related together. I thought it could be done easily using Nested SOQL queries, but it was not possible. Nested queries could be only 1 Level deep. So, the below query doesn't work.

List<Grandparent__c> parentnodes = [Select Name, 
(Select Name,(Select Name from Grandparent__c.Parent__c.Child__r) 
From Grandparent__c.Parent__r) from Grandparent__c];

Whereas, this one works

List<parent__c> parentnodes = [Select Name, 
(Select Name From parent__c.Child__r) from parent__c