I have a Asset Table with Field Name like id and name. I have a child relationship of the following Test_Result__c.System__c with field like resultTest1

Select Name, id , (Select something from the child) from Asset

How do you form a proper query to get for each Asset Name, id, the child resultTest1 ?

2 Answers 2


You need to give child relationship name in the subquery. Child relationship name can be found from the lookup in the child to Asset object. So your query will be like

Select Name, id , (Select Id,Name from ChildRelationshipName) from Asset
  • 1
    @RickyRicardo Hi Ricky, since this answered your question you should be sure to mark it as the accepted answer so that it's clear to others in the future. Welcome to SFSE!
    – Matt Lacey
    Commented May 14, 2015 at 20:43

The documentation here has a detailed explanation.

To be able to traverse these relationships for standard objects, a relationship name is given to each relationship. The form of the name is different, depending on the direction of the relationship:

For child-to-parent relationships, the relationship name to the parent is the name of the foreign key, and there is a relationshipName property that holds the reference to the parent object. For example, the Model child object has a child-to-parent relationship to the Widget object, so the value of relationshipName in Contact is Widget__r. These relationships are traversed by specifying the parent using dot notation in the query, for example:

SELECT Model__c.Name, Model__c.Widget__r.Name from Model__c

This query returns the names of all the Models in the organization, and for each Model, the Widget name associated with (parent of) that Model.

For parent-to-child relationships, the parent object has a name for the child relationship that is unique to the parent, the plural of the child object name. For example, Widget has a child relationship to Models and has a relationshipName for them called Models. These relationships can be traversed only in the SELECT clause, using a nested SOQL query. For example:

SELECT Widget__c.Name, (SELECT Model__c.Name FROM Widget__c.Models__r) FROM Widget__c

This query returns all Widgets, and for each Widget, the name of each Model associated with (the child of) that Widget.

  • I have been reading this from the last couple of hours from the SQL Server perspective I could not get it until Victor answer it. Commented May 14, 2015 at 17:39

You must log in to answer this question.

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