Intermediate 25 min read

Parent Relationship Queries

By the end of this lesson, you'll be able to:

  • Query fields on a parent record using dot notation in SOQL
  • Traverse more than one relationship level in a single query
  • Explain why this avoids a separate query for the parent record

Prerequisites: "Sorting and Limiting Results"

Querying a parent's fields directly

List<Contact> contacts = [
    SELECT Id, LastName, Account.Name, Account.Industry
    FROM Contact
];

for (Contact con : contacts) {
    System.debug(con.LastName + ' works at ' + con.Account.Name);
}

This is Module 18's "Relationships in Apex" lesson from the query side: Account.Name and Account.Industry in the SELECT clause pull the parent Account's fields into the same query result — no second query needed to fetch them separately.

Traversing multiple levels

List<Contact> contacts = [
    SELECT Id, LastName, Account.Owner.Name
    FROM Contact
];

Account.Owner.Name reaches two levels up: from Contact to its parent Account, and from that Account to its owning User's Name. Salesforce supports traversing up to 5 relationship levels deep in a single SOQL query this way.

Why this beats a separate query

// DON'T DO THIS — a query inside a loop
for (Contact con : contacts) {
    Account acc = [SELECT Name FROM Account WHERE Id = :con.AccountId]; // a query per contact!
}

This is exactly the SOQL-in-a-loop governor limit problem revisited from Module 18. A single query with Account.Name in the SELECT clause retrieves everything needed up front, entirely avoiding the per-record query that would otherwise blow past governor limits at scale.

Exercise

Write a query on Opportunity selecting Id, Name, and the parent Account's Name and Industry.

Show hint

SELECT Id, Name, Account.Name, Account.Industry FROM Opportunity

APEX

Parent Relationship Queries Quiz

1. How many relationship levels can a single SOQL query traverse upward?

Log in to submit the quiz and save your score.

My Notes

Log in to keep private notes on this lesson.

Questions about this lesson

No questions yet — be the first to ask.

Log in to ask a question about this lesson.

Summary

A parent relationship query reaches through a lookup or master-detail relationship to pull parent fields in the very same query — this is the SOQL side of Module 18's relationship-traversal lesson.