SOQL Relationships between objects are mainly used to query the records from one or more objects in a single SOQL statement in salesforce.com.
Different SOQL Relationships.
- SOQL Relationships between Standard objects.
- SOQL Relationships between custom objects.
- Many-to-one relationships: (n:1)
SOQL Relationships between Standard objects.
- When a relationship is from parent-to-child then SOQL statement must have sub Query in it’s Statement.
- For standard objects SOQL relationships name is equal to plural of the Child Object name.
- SELECT name, ( SELECT Lastname FROM CONTACTS ) FROM Account.
- When a SOQL Relationship is from Child-to-Parent then the SOQL statement will not have Sub Query.
- SOQL Relationships name is equal to name of the parent object.
- SELECT contact.firstname, contact.Account.name FROM Contact.
Many-to-one relationships: (n:1)
These relationships are specified in SELECT, FROM, WHERE clauses using dot(.) operator.
- SELECT id, Name, Account.name FROM Contact WHERE Account.Industry = ‘ Media’.
- SELECT name (SELECT lastname FROM Contacts) FROM Account.
SOQL Relationships between custom objects.
- SELECT Lastname__c, ( SELECT Lastname__c FROM Daughters__r ) FROM Mother__c.
- SELECT Id, Firstname__c, Mother__r.firstname__c FROM Daughter__c WHERE Mother__r.Lastname__c LIKE ‘c%’.