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
simon chaosimon chao 

Is it possible to filter query items so that I only obtain the field with the highest number?

Is is possible to filter out a query list so I only get the highest one? For example, I would like to  query a students name and his exam mark. And I only want to grab the student with the highest exam mark.  (student__c is a child of class__c)
Select (Select Student_Name__c, Exam_Mark__c from Student__r ORDER BY Exam_Mark__c ASC) from class__c
This grabs all the students and it doesn't sort out the exam marks from highest to lowest. I know I can add a limit 1 to grab only the top value, but how can I gaurantee that the first query value that I am grabbing is the highest? 
 
Dushyant SonwarDushyant Sonwar
Hi Simon ,

Did you try using the below query?
Select (Select Student_Name__c, Exam_Mark__c from Student__r ORDER BY Exam_Mark__c DESC Nulls Last limit 1) from class__c

Hope this helps.
Dushyant SonwarDushyant Sonwar
 I am assuming that Exam_Mark__c is a  Number field.
simon chaosimon chao
didn't work, it still shows more than 1 rows when i query and it doesn't show the highest mark
Dushyant SonwarDushyant Sonwar
Hi simon , 

didnt understand what you said.

​There may be more than one classes in the query result but student always be one due to limit.

Are you saying  that you are getting more than one student(child) for a class(parent) for the parent to child query suggested above despite having limit 1 in student(child)?
 
Dushyant SonwarDushyant Sonwar
Are you looking for student having highest marks according to the classes as you have mentioned above in the starting a parent to child soql?