Which SOQL query will get the record for the Viridian City Gym and all it’s trainers?
A custom object Trainer_ < has a lookup field to another custom object cym__ c.
Which SOQL query will get the record for the Viridian City Gym and all it’s trainers?
A . SELECT Id; (SELECT Id FROM Trainers_r) FROM Gym_ _c WHERE Name = ‘Viridian City Gym’
B . SELECT Id, (SELECT Id FROM Trainer_c) FROM Gym__c WHERE Name = ‘Viridian City Gym’
C . SELECT ID FROM Trainer ¢ WHERE Gym__r.Name = ‘Viridian City Gym’
D . SELECT Id, (SELECT Id FROM Trainers_c) FROM Gym_c WHERE Name – ‘Viridian City Gym’’
Answer: A
Explanation:
Given that Trainer__c has a lookup field to Gym__c, to retrieve the record for the ‘Viridian City Gym’ and all its trainers, the correct SOQL query is:
Option A:
SELECT Id, (SELECT Id FROM Trainers__r) FROM Gym__c WHERE Name = ‘Viridian City Gym’ Parent-to-Child Query (Subquery):
To retrieve child records (trainers) from the parent record (gym), you perform a subquery using the child relationship name.
Child Relationship Name:
For a lookup relationship, the child relationship name can be found in the relationship settings. By default, if the lookup field on Trainer__c is Gym__c, the child relationship name is Trainers__r. Query Breakdown:
SELECT Id, (SELECT Id FROM Trainers__r) FROM Gym__c WHERE Name = ‘Viridian City Gym’ Selects the Gym record and all related Trainers.
Why Other Options Are Incorrect:
Option B: (SELECT Id FROM Trainer__c)
Trainer__c is an object, not a relationship name, so the subquery is incorrect. Option C: SELECT ID FROM Trainer__c WHERE Gym__r.Name = ‘Viridian City Gym’ This query retrieves Trainer records, not the Gym record and its trainers as specified. Option D: (SELECT Id FROM Trainers__c)
Trainers__c is incorrect; the child relationship name should end with __r in subqueries.
Reference: SOQL Relationships and Subqueries:
"To retrieve related child records, use a subquery in the SELECT clause."
― Salesforce SOQL and SOSL
Reference: Relationship Queries Conclusion:
Option A correctly retrieves the Gym record and all its related Trainer records using a parent-to-child subquery.
Latest DEX-450 Dumps Valid Version with 456 Q&As
Latest And Valid Q&A | Instant Download | Once Fail, Full Refund