function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
DritterDritter 

SOQL across child records with WHERE statment

I'm trying to find accounts with a student school that has a school type of "med_us." However, when I add the WHERE statement to the inner select it times out. See below:

SELECT id, Educational_Background__c FROM Account WHERE ID IN (SELECT Account__c FROM Student_School__c WHERE school_type__c = 'med_us') AND Educational_Background__c = 'IMG'AND createddate = TODAY

If I remove the WHERE statement it runs fine, but it's not giving me what I'm looking for:

SELECT id, Educational_Background__c FROM Account WHERE ID IN (SELECT Account__c FROM Student_School__c ) AND Educational_Background__c = 'IMG'AND createddate = TODAY

When searching for records across child objects are we allowed to use WHERE statements in the inner select? 

I've also tried this:

SELECT id, Educational_Background__c FROM Account WHERE ID IN (SELECT Account__c FROM Student_School__c) AND Educational_Background__c = 'IMG'AND createddate = TODAY AND school_type__c.student_school__r ='med_us'
Best Answer chosen by Dritter
JeffreyStevensJeffreyStevens
Someone else may correct me - but I've never used a SOQL in the where clause.

If that's the case - then I think You'll need to goto Student_School__c as your main table.  Maybe something like this?

SELECT id,Account.Educational_Background__c, StudentIDOrOtherFields
FROM Student_School__c
WHERE School_type__c = 'med_us'
    AND Account__r.Educational_Background__c = 'IMG'
    AND Account__r.CreatedDAte = TODAY

 

All Answers

JeffreyStevensJeffreyStevens
Someone else may correct me - but I've never used a SOQL in the where clause.

If that's the case - then I think You'll need to goto Student_School__c as your main table.  Maybe something like this?

SELECT id,Account.Educational_Background__c, StudentIDOrOtherFields
FROM Student_School__c
WHERE School_type__c = 'med_us'
    AND Account__r.Educational_Background__c = 'IMG'
    AND Account__r.CreatedDAte = TODAY

 
This was selected as the best answer
DritterDritter
Thank you