I have a piece of code in my Apex:

List<sObject> assessmentToQuestions = [
            SELECT Id, (SELECT Id FROM zced__Assessment_Questions__r) questions
            FROM zced__Assessment__c
            WHERE zced__Interaction__c = :interactionId

        Set<String> questionsIds = new Set<String>();
        for(sObject assessment: assessmentToQuestions) {
            for(sObject question: assessment.questions) {

The code above does not work because it does not allow me to use alias for the list of Id over here:

(SELECT Id FROM zced__Assessment_Questions__r) questions

I saw the application of an alias over here and tried to do the same, but got an error. So, my question here is how can I use an alias in the case?

Oh (this question in case if it is an x-y problem) how can I approach the problem of iterating the list in another way? I need to be able to iterate over all the Id extracted over here:

(SELECT Id FROM zced__Assessment_Questions__r)
  • You may find my answer on converting sql to soql to be helpful (in addition to the answer you've already gotten here)
    – Derek F
    Jul 13 '18 at 15:21

Alias does not work with parent-to-child queries; however, you can access the query result set of zced__Assessment_Questions__r as follows:

for (zced__Assessment__c zced__Assessment : [
        SELECT Id, (SELECT Id FROM zced__Assessment_Questions__r)
        FROM zced__Assessment__c
        WHERE zced__Interaction__c = :interactionId
]) {
  • 1
    You're quite right but the phrase "aggregation query" might be confusing here (this isn't a SOQL aggregate query, where aliasing is supported; it's a non-aggregate parent-child query).
    – David Reed
    Jul 13 '18 at 15:17
  • Rather than explaining where Aliasing is not supported, explain where it is supported.
    – gNerb
    Jul 13 '18 at 16:08

Not the answer you're looking for? Browse other questions tagged or ask your own question.