Understanding Relationship Names

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 Contact child object has a
child-to-parent relationship to the Account object, so the value of relationshipName in Contact is Account. These
relationships are traversed by specifying the parent using dot notation in the query, for
example:
SELECT Contact.FirstName, Contact.Account.Name from Contact
This query returns the first names of all the contacts in the organization, and for each contact, the account name associated with (parent of) that contact.
- 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, Account has
child relationships to Assets, Cases, and Contacts among other objects, and has a relationshipName for each, Assets, Cases, and Contacts.These relationships can be traversed only in the SELECT clause, using a nested SOQL query. For example:
SELECT Account.Name, (SELECT Contact.FirstName, Contact.LastName FROM Account.Contacts) FROM Account
This query returns all accounts, and for each account, the first and last name of each contact associated with (the child of) that account.
Relationship names are different for custom objects, though the SELECT syntax is the same. See Identifying Parent and Child Relationships for more information.