title: Mysql subquery return error when more than one record is fetched from db
body: <p>I am using the following code to generate a merit list of students' performance in different subjects to display all the students according to their performances.</p>
  
  <pre><code> $qry = "select * from (
   select (select percentage from exams_db where subjecttype = 'English') as English,
             (select percentage from exams_db where subjecttype = 'Biology') as Biology,
             (select percentage from exams_db where subjecttype = 'Maths') as Maths,
             (select percentage from exams_db where subjecttype = 'Science') as Science,
             (select percentage from exams_db where subjecttype = 'SocialStudies') as SocialStudies,
             (select total_marks from exams_db ORDER BY total_marks DESC LIMIT 1) as total_marks,
             (select exam_date from exams_db ORDER BY id DESC LIMIT 1) as exam_date
  
      from exams_db group by Maths
      union all
      select (select grade from exams_db where subjecttype = 'English') as English,
             (select grade from exams_db where subjecttype = 'Biology' ) as Biology,
             (select grade from exams_db where subjecttype = 'Maths') as Maths,
             (select grade from exams_db where subjecttype = 'Science') as Science,
             (select grade from exams_db where subjecttype = 'SocialStudies') as SocialStudies, 
             (select total_marks from exams_db  ORDER BY total_marks DESC LIMIT 1) as total_marks,
             (select exam_date from exams_db ORDER BY id DESC LIMIT 1) as exam_date
  
      from exams_db group by Maths
       ) t "; 
  </code></pre>
  
  <p>However, i get an error that Subquery returns more than 1 row</p>
  
  <p>However, when i edit the code as below, it works for only one user.</p>
  
  <pre><code>$qry = "select * from (
   select (select percentage from exams_db  where subjecttype = 'English' AND user = '$userid') as English,
             (select percentage from exams_db  where subjecttype = 'Biology' AND user = '$userid') as Biology,
             (select percentage from exams_db  where subjecttype = 'Maths' AND user = '$userid') as Maths,
             (select percentage from exams_db  where subjecttype = 'Science' AND user = '$userid') as Science,
             (select percentage from exams_db  where subjecttype = 'SocialStudies' AND user = '$userid') as SocialStudies,
             (select total_marks from exams_db  WHERE user = '$userid' ORDER BY total_marks DESC LIMIT 1) as total_marks,
             (select exam_date from exams_db  WHERE user = '$userid' ORDER BY id DESC LIMIT 1) as exam_date
  
      from exams_db  group by Maths
      union all
      select (select grade from exams_db  where subjecttype = 'English' AND user = '$userid') as English,
             (select grade from exams_db  where subjecttype = 'Biology' AND user = '$userid') as Biology,
             (select grade from exams_db  where subjecttype = 'Maths' AND user = '$userid') as Maths,
             (select grade from exams_db  where subjecttype = 'Science' AND user = '$userid') as Science,
             (select grade from exams_db  where subjecttype = 'SocialStudies' AND user = '$userid') as SocialStudies, 
             (select total_marks from exams_db WHERE user = '$userid' ORDER BY total_marks DESC LIMIT 1) as total_marks,
             (select exam_date from exams_db  WHERE user = '$userid' ORDER BY id DESC LIMIT 1) as exam_date
  
      from exams_db  group by Maths
       ) t ";
  </code></pre>
  
  <p>But, i want to display all users. So, how can i edit my first code to enable me display all users because as much as the second code is helping me, it is allowing me to display only one user.</p>
  
  <p>My database structure is as follows</p>
  
  <pre><code>user    subjecttype  percentage  grade
  
   109283  English       40%         B
  
   345245  Biology       80%         A
  
   832904  Science       50%         C
  </code></pre>