Salesforce Dictionary - Free Salesforce GlossarySalesforce Dictionary
Full Relationship Query entry
How-to guide

Write effective relationship queries

Writing effective SOQL relationship queries combines understanding the data model with knowing the language syntax and the performance implications. The workflow below covers the standard sequence for developing and testing a complex relationship query.

By Dipojjal Chakrabarti · Founder & Editor, Salesforce DictionaryLast updated May 19, 2026

Writing effective SOQL relationship queries combines understanding the data model with knowing the language syntax and the performance implications. The workflow below covers the standard sequence for developing and testing a complex relationship query.

  1. Identify the data the query needs to return

    Document the question the query answers: what records to retrieve, what fields are needed from each, what filters apply. Identify the starting object (often the one with the primary filter), the parent objects to traverse upward (using dot notation), and the child collections to retrieve via subqueries. Sketch the relationship path before writing SOQL. Mistakes at this design step lead to inefficient queries that work but waste resources.

  2. Construct the query with relationship names

    Write the SOQL using the correct relationship names. For standard objects, the relationship name typically matches the object name (Account from Contact). For custom relationships, use the relationship name shown on the Lookup field configuration (typically the field name with __r appended). Add WHERE clauses with the same dot notation for cross-object filtering. Add ORDER BY clauses for the outer query and any subqueries. Test the query in the Developer Console or Workbench against a small dataset first.

  3. Analyze the Query Plan for performance

    For non-trivial queries, run the Query Plan tool in the Developer Console. The plan shows how the optimizer will execute the query, which indexes it will use, and whether any portion of the query will fall back to a table scan. Selective queries (those with WHERE clauses that filter to a small percentage of records using indexed fields) perform well; non-selective queries can produce timeouts on large datasets. Optimize by adding custom indexes, restructuring the query to start with the most selective filter, or breaking very complex queries into smaller pieces.

  4. Use the query in Apex or integration code

    Once the SOQL is correct and efficient, embed it in the appropriate code path: an Apex class for internal logic, an external integration via the REST API or Bulk API, or a Visualforce or Lightning component for UI. For Apex, use bind variables to parameterize the query. For external integrations, use parameterized queries through the REST API to avoid string concatenation. Add Apex tests covering the query results against sample data. Monitor query execution time and limit consumption in production.

Gotchas
  • Custom relationship names use __r, not __c. The Lookup field uses __c; the relationship reference uses __r in SOQL.
  • Child-to-parent traversal is limited to five levels. Queries that need more levels must split into multiple queries.
  • Subqueries cannot nest within other subqueries. Deeper hierarchies require multiple queries or different patterns.
  • Polymorphic relationships require TYPEOF in modern SOQL. Older code may use awkward workarounds.
  • Performance degrades on non-selective queries against very large objects. Always check the Query Plan for production-scale queries.

See the full Relationship Query entry

Relationship Query includes the definition, worked example, deep dive, related terms, and a quiz.