Relationship queries are similar to SQL joins. However, you cannot perform arbitrary SQL joins. The relationship queries in SOQL must traverse a valid relationship path as defined in the rest of this section.
Master Parent-to-Child and Child-to-Parent Queries with Visual Learning
Progress: 0% Complete
SOQL (Salesforce Object Query Language) is used to search your Salesforce data. Understanding relationships is key to powerful queries!
Parent-to-Child queries use subqueries to fetch related child records. Perfect for getting all contacts for an account!
Child-to-Parent queries use dot notation to access parent object fields. Navigate up the relationship hierarchy!
Combine both techniques for powerful multi-level queries that traverse multiple relationships!
Lookup relationships are more flexible – child records can exist without parents.
Master-Detail relationships are tighter – detail records cannot exist without master records.
The most powerful queries combine both parent-to-child AND child-to-parent techniques!
Create a query that shows accounts with their opportunities and the primary contact for each opportunity.
Test your SOQL relationships knowledge with this comprehensive quiz! Track your progress and get personalized feedback.
Ready-to-use SOQL queries for common business scenarios. Click any query to copy it!
Syntax: (SELECT fields FROM ChildObject)
Use Case: Get all related child records
Example:
SELECT Name, (SELECT Name FROM Contacts) FROM Account
Syntax: ParentObject.Field
Use Case: Access parent object fields
Example:
SELECT Name, Account.Name FROM Contact
SELECT (SELECT Name FROM Accounts) FROM UserSELECT Owner.Name FROM Account