PDI · Question #49
A custom object Trainer_c has a lookup field to another custom object Gym___c. Which SOQL query will get the record for the Viridian City gym and it's trainers?
The correct answer is A. SELECT Id, (SELECT Id FROM Trainers) FROM Gym_C WHERE Name . Viridian City Gym'. To retrieve a Gym__c record named 'Viridian City Gym' and its associated Trainer__c records, a parent-to-child SOQL subquery using the plural relationship name 'Trainers' is required.
Question
A custom object Trainer_c has a lookup field to another custom object Gym___c. Which SOQL query will get the record for the Viridian City gym and it's trainers?
Options
- ASELECT Id, (SELECT Id FROM Trainers) FROM Gym_C WHERE Name . Viridian City Gym'
- BSELECT Id, (SELECT Id FROM Trainer_c) FROM Gym_c WHERE Name -Viridian City Gym'
- CSELECT ID FROM Trainer_c WHERE Gym__r.Name -Viridian City Gym'
- DSELECT Id, (SELECT Id FROM Trainers) FROM Gym_C WHERE Name -Viridian City Gym'
How the community answered
(38 responses)- A79% (30)
- B5% (2)
- C13% (5)
- D3% (1)
Why each option
To retrieve a `Gym__c` record named 'Viridian City Gym' and its associated `Trainer__c` records, a parent-to-child SOQL subquery using the plural relationship name 'Trainers' is required.
This SOQL query correctly uses a parent-to-child subquery to first find the `Gym__c` record by its `Name` and then, within that query, retrieves all related child `Trainer__c` records by using the plural relationship name of the child object, which is typically 'Trainers' for a lookup from `Trainer__c` to `Gym__c`.
The subquery uses `Trainer_c` (the object's API name) instead of the plural relationship name (e.g., `Trainers`), which is required for parent-to-child SOQL queries.
This is a child-to-parent query that would return only `Trainer__c` records whose associated gym name is 'Viridian City Gym', rather than retrieving the gym record itself and its entire list of trainers.
This query uses an incorrect comparison operator '-' instead of '=' in the `WHERE` clause, making the query syntactically invalid.
Concept tested: SOQL Parent-to-Child Queries
Source: https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_relationships.htm
Topics
Community Discussion
No community discussion yet for this question.