SOQL Relationships : In our previous Salesforce Tutorial we have learned about  SOQL Functions. In this Salesforce Training Tutorial we are going to learn about SOQL Relationships.

SOQL Relationships between objects are mainly used to query the records from one or more objects in a single SOQL statement in

Different SOQL Relationships.

  1. SOQL Relationships between Standard objects.
  2. SOQL Relationships between custom objects.
  3. Many-to-one relationships: (n:1)

SOQL Relationships between Standard objects.

Parent-to-Child Relationship.

  1. When a relationship is from parent-to-child then SOQL statement must have sub Query in it’s Statement.
  2. For standard objects SOQL relationships name is equal to plural of the Child Object name.

SOQL Relationships

  • SELECT name, ( SELECT Lastname FROM CONTACTS ) FROM Account.

SOQL RelationshipsChild-to-Parent Relationship.

  1. When a SOQL Relationship is from Child-to-Parent then the SOQL statement will not have Sub Query.
  2. SOQL Relationships name is equal to name of the parent object.
  • SELECT contact.firstname, FROM Contact.

SOQL Relationships

Many-to-one relationships: (n:1)

Child-to-Parent relationship

These relationships are specified in SELECT, FROM, WHERE clauses using dot(.) operator.

  • SELECT id, Name, FROM Contact WHERE Account.Industry = ‘ Media’.

Parent-to-Child Relationship

  • SELECT name (SELECT lastname FROM Contacts) FROM Account.

SOQL Relationships between custom objects.

Parent-to-Child relationship.

  • SELECT Lastname__c, ( SELECT Lastname__c FROM Daughters__r ) FROM Mother__c.

Child-to-Parent relationship.

  • SELECT Id, Firstname__c, Mother__r.firstname__c FROM Daughter__c WHERE Mother__r.Lastname__c LIKE ‘c%’.