I have the following structure:

Contract__c (1) ----- (*) ContractCover__c (1) ---- (*) Address__c

I need to create a query that gets all the addresses that are linked to contract via contractcover.

Select Id, (Select Id FROM ContractCover__r, (Select Id from Contract__r)) From Address__c

This query seems to be incorrect. Can anyone help? Tia.

3 Answers 3


You can only go one level of children down, so consider something like this:

SELECT Id, Address__r.Id, (SELECT Id FROM Contracts__r) 
FROM ContractCover__c 
WHERE Address__r.Id != null

If you need to limit it by a field on Address__c, you can add something like, AND Address__r.XYZ__c = :foo

Also note that the relationship name to Contract is likely Contracts__r not Contract__r


Haven't tried anything like this but in parent to child relationship queries, you need to use plural of child names. Try something like:

Select Id, (Select Id FROM ContractCovers__r, (Select Id from Contracts__r)) From Address__c

Take a look at the docs here:
You can only select children not grandchildren records. You can go the opposite way and query the grandchildren directly and get the parent information as part of the query. There is a similar question here that might help you structure your query. Found another similar question here too.

Understanding Relationship Query Limitations

When designing relationship queries, consider these limitations:

Relationship queries are not the same as SQL joins. You must have a relationship between objects to create a join in SOQL.

No more than 35 child-to-parent relationships can be specified in a query. A custom object allows up to 25 relationships, so you can reference all the child-to-parent relationships for a custom object in one query.

No more than 20 parent-to-child relationships can be specified in a query.

In each specified relationship, no more than five levels can be specified in a child-to-parent relationship. For example, Contact.Account.Owner.FirstName (three levels).

In each specified relationship, only one level of parent-to-child relationship can be specified in a query. For example, if the FROM clause specifies Account, the SELECT clause can only specify the Contact or other objects at that level. It could not specify a child object of Contact.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .