title: Mysql Sql statement to Get Position of Student Ranked in order of marks scored
body: <p>I need an sql that will give the position of the student ranked in order of marks scored in a specific examtyp e.g. CAT! only.the sql below gives the position of the student but does not distinguish the examtyp.it ranks without considering the examtyp.</p>
  
  <pre><code>res_id admNo stream examtyp termId  marks   grade   points  year
     1    2129    0   CAT1      1     525       C      62     2013
     2    4093    0   CAT1      1     569       B+     69     2013
     3    2129    0   CAT2      1     550       B+     67     2013
     4    4093    0   CAT2      1     556       B+     68     2013
     6    2129    0   FINAL     1     559       B+     68     2013
     7    2129    0   AVERAGE   1     545       B      66     2013
     7    4093    0   FINAL     1     581       B+     70     2013
     8    4093    0   AVERAGE   1     569       B+     69     2013
  
  
  
  
  $sql = "SELECT 1 + (SELECT count(*) FROM $table a 
          WHERE a.total_marks &gt; b.total_marks ) AS rank 
          FROM $table b WHERE admNo=? AND examCategory=? AND termId=? AND year=?  
          ORDER BY rank LIMIT 1";
  $res = $this-&gt;db-&gt;query($sql, array($admNo, $examCategory, $term, $year));
  </code></pre>