title: MySql query selecting top highest student scores including a must subject score
body: <p>What I am trying to archive here, is to select only the subjects a student is performing the best at, including a mandatory subject (<code>mustSubjectId</code>) and output the results depending on the grade range total.<br>
  The higher the <code>grade</code> the lower the point <code>add sum(marks)</code> as points. </p>
  
  <p>Example input parameters:</p>
  
  <pre><code>$subjectlimit = '3'; // total subjects a student should pass including mandatory subject to qualify 
  $idexam = '1'; // targeted examination id 
  $mustSubjectId = '201'; // mandatory subjectid
  $studentId = '1'; // a student to get
  </code></pre>
  
  <p>Table examinations</p>
  
  <pre>
  idcandidate | subject_id   | score
  ------------------------------------
  1           |    200       | 30 
  1           |    201       | 60 (note: a must subject)
  1           |    202       | 80 
  1           |    204       | 90 
  1           |    203       | 50 
  1           |    205       | 54 
  1           |    209       | 37 
  8           |    200       | 68 
  2           |    200       | 20 
  1           |    206       | 50 
  </pre>
  
  <p>Table examrules</p>
  
  <pre>
  idexam          | marks       | grade
  ---------------------------------------
  1               |    30       | 9 
  1               |    45       | 8 
  1               |    65       | 7 
  1               |    70       | 6
  1               |    80       | 5 
  1               |    85       | 4 
  1               |    90       | 3 
  1               |    95       | 2 
  1               |    100      | 1 
  </pre>
  
  <p>Output with limit 3</p>
  
  <pre>
  Idcandidate| idexam   | subject_id| grade
  1          |     1    | 204       |    3
  1          |     1    | 202       |    5
  1          |     1    | 201       |    8
  </pre>
  
  <p>Final Output </p>
  
  <pre>
  Idcandidate| idexam  | points
  1          |     1   |    16
  </pre>    
  
  <p>I hope that you can help as I am not good with complex MySql queries</p>
  
  <p><strong>EDIT</strong></p>
  
  <blockquote>
    <p>I was mistaken by JNevill in the comments, the table examinations does
    not have idexam and no relationship with examrules. The selection
    should be something like</p>
    
    <p>Select top scores including mandatory subject score from examinations
    where idcandidate = ‘1’ limit 3 then check the selected scores grade
    in examrules where idexam = ‘1’ , the grades which will be found
    should be calculated as points  sum (grade) as points</p>
  </blockquote>
  
  <p><strong>AIM OF THE QUERY</strong></p>
  
  <blockquote>
    <p>There is a sheet showing the grades of all subjects of a class against
    students, then on a certain row the sheet suppose to show the points
    the student gets on 3 subjects including mandatory subject.  For a
    student to pass examination he/she should pass three subjects
    including mandatory subject.</p>
  </blockquote>
  
  <p><strong>Note</strong> 
  the sql am running is reporting not to recognize select top and I also try on fiddle, fiddle didn’t recognize it too. <code>select top 3 score from examinations where idcandidate = ‘1’</code></p>