Salesforce Platform Developer I (legacy) — Question 157
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 all its trainers?
Answer options
- A. SELECT ID FROM Trainer__c WHERE Gym__r.Name = 'Viridian City Gym'
- B. SELECT Id, (SELECT Id FROM Trainers__c) FROM Gym__c WHERE Name = 'Viridian City Gym'
- C. SELECT Id, (SELECT Id FROM Trainer__c) FROM Gym__c WHERE Name = 'Viridian City Gym'
- D. SELECT Id, (SELECT Id FROM Trainers__r) FROM Gym__c WHERE Name = 'Viridian City Gym'
Correct answer: D
Explanation
The correct answer is D because it uses a subquery to retrieve all Trainer__c records associated with the Gym__c that has the name 'Viridian City Gym'. Option A is incorrect as it only retrieves Trainer__c records but not the Gym__c record. Options B and C are incorrect because they either reference the wrong relationship name or do not gather the trainers correctly.