In Field Operators and Boolean Operators, we discussed how fields map to the filter type and how to combine filters with the AND, OR, and NOT functions. The sObject is also added to the filter type for each parent relationship.
Objects can have a child-to-parent relationship or a parent-to-child relationship. For example, the Contact object has a child-to-parent relationship to the Account object. An Account has parent-to-child relationships to Assets, Cases, and Contacts, and other objects.
Child-to-Parent Relationships
Child-to-parent relationships can be many-to-one, just like multiple child contacts can be on a parent account.
For some objects that can have a parent relationship with the same object, you can query the parent relationship using the Parent field. For example, an account can have a parent account and a campaign can have a parent campaign.
Parent-to-child relationships are usually one-to-many, just like a parent account can have many child contacts. To include subqueries for standard objects, specify the plural form of the object name, which is the name of the relationship for the object. For example, by selecting from Account, you can query the children records via the Cases, Contacts, and Opportunities fields.
1SELECT Name,2(3 SELECT LastName4 FROM Contacts5)6FROM Account
Some objects have children relationships to the same object. For example, use the ChildAccounts field to query children accounts on account records. Accounts without children accounts are also returned.
To query object metadata that includes child relationships, pass in the childRelationships field.
You can also traverse the parent-to-child relationship using a reference to a different object. In this case, the CreatedBy field references the Name field on the User object.
The previous query is similar to this SOQL statement.
SOQL statement showing parent-to-child relationship with a reference field
1SELECT Name,2(3 SELECT CreatedBy.Name4 FROM Notes5)6FROM Account
A query and its subqueries can include the where argument. The argument can filter on a supported object in the current scope via parent relationships.
For example, you can query accounts in a given industry and the last name of every contact whose created by first name is ‘Marc’.
Query accounts with where argument in query and subquery
The previous query is similar to this SOQL statement.
SOQL statement showing WHERE clause in query and subquery
1SELECT Name,2(3 SELECT LastName4 FROM Contacts5 WHERE CreatedBy.FirstName = 'Marc')6 FROM Account WHERE Industry = 'Technology'
Lookup Relationships
With a lookup relationship, you can retrieve field information on a child or parent object. For example, you can query accounts and their associated contact names.
The child records are paginated and include cursor and pagination information. By default, the first 10 children records are returned. To specify the number of children records to return, use the first argument on the child object.
Response for query on children records in ascending order with pagination information
The previous query is similar to this SOQL statement.
SOQL statement with ORDER BY clause and reference fields
1SELECT Id, CaseNumber, Account.Id, Account.Name2FROM Case3ORDER BY Account.Name
With a where argument that uses the or boolean operator, records are returned even if the reference field value in a record is null.
For example, if a contact with a specific LastName has a null AccountId field and another contact with a different last name and specific parent account name, the query returns both contacts.
Query contacts that matches a condition with the or operator