A developer needs to retrieve Account records along with their associated Contacts in a single query. Which query syntax is correct?
Trap 1: SELECT Id, Name, CONCAT(Contacts) FROM Account
`CONCAT()` is a string function in SOQL used to concatenate field values into a single string, which tempts developers familiar with SQL string aggregation or formula fields. However, it fails because it cannot traverse parent-to-child relationships to retrieve related child records as an object list. To fetch Accounts alongside their associated Contacts in a single query, you must instead use a subquery syntax such as `SELECT Id, Name, (SELECT Id, LastName FROM Contacts) FROM Account`.
Trap 2: SELECT Id, Name, (SELECT Id, LastName FROM Contact) FROM Account
Using the singular relationship name `Contact` fails because Salesforce child-to-parent subqueries in SOQL require the plural relationship name, which defaults to `Contacts`. This syntax is tempting because it resembles standard object-oriented dot-notation or general SQL subqueries where the target table name is singular, making it a logical guess for developers querying child records. It would be correct if the child relationship on the Account object had been custom-named as a singular noun, or when querying a parent field from a child object.
Trap 3: SELECT Id, Name, Contacts__r FROM Account
Child-to-parent relationships in SOQL require traversing standard relationship names or custom relationship names ending in `__r` in the reverse direction, but querying contacts from an account requires a parent-to-child subquery enclosed in parentheses. This syntax is tempting because it resembles a standard child-to-parent traversal, such as retrieving an Account's related Parent Account using a custom relationship field like `Parent__c`. However, that specific dot-notation traversal syntax is exclusively designed for querying parent records from a child context, rather than nesting child records within a parent query.
- A
SELECT Id, Name, CONCAT(Contacts) FROM Account
Why it fails: `CONCAT()` is a string function in SOQL used to concatenate field values into a single string, which tempts developers familiar with SQL string aggregation or formula fields. However, it fails because it cannot traverse parent-to-child relationships to retrieve related child records as an object list. To fetch Accounts alongside their associated Contacts in a single query, you must instead use a subquery syntax such as `SELECT Id, Name, (SELECT Id, LastName FROM Contacts) FROM Account`.
- B
SELECT Id, Name, (SELECT Id, LastName FROM Contact) FROM Account
Why it fails: Using the singular relationship name `Contact` fails because Salesforce child-to-parent subqueries in SOQL require the plural relationship name, which defaults to `Contacts`. This syntax is tempting because it resembles standard object-oriented dot-notation or general SQL subqueries where the target table name is singular, making it a logical guess for developers querying child records. It would be correct if the child relationship on the Account object had been custom-named as a singular noun, or when querying a parent field from a child object.
- C
SELECT Id, Name, Contacts__r FROM Account
Why it fails: Child-to-parent relationships in SOQL require traversing standard relationship names or custom relationship names ending in `__r` in the reverse direction, but querying contacts from an account requires a parent-to-child subquery enclosed in parentheses. This syntax is tempting because it resembles a standard child-to-parent traversal, such as retrieving an Account's related Parent Account using a custom relationship field like `Parent__c`. However, that specific dot-notation traversal syntax is exclusively designed for querying parent records from a child context, rather than nesting child records within a parent query.
- D
SELECT Id, Name, (SELECT Id, LastName FROM Contacts) FROM Account
This syntax correctly satisfies the requirement to retrieve parent Account records and their child Contact records in a single database round trip by leveraging a parent-to-child relationship subquery. Salesforce Object Query Language (SOQL) uses standard relationship naming conventions, where the pluralised child relationship name (`Contacts`) is enclosed in parentheses within the main `SELECT` statement. This relational query structure directly fulfills the condition to fetch associated records together efficiently without executing multiple queries.