title: count not null columns out of specific list of columns and then compare in where clause for each row
body: <p>Table structure:</p>
  
  <p><a href="http://imagebin.org/index.php?mode=image&amp;id=238883" rel="nofollow noreferrer">Table Structure http://imagebin.org/index.php?mode=image&amp;id=238883</a></p>
  
  <p>I want to fetch data from both the tables which satisfy some of the conditions like</p>
  
  <pre><code>    WHERE batch='2009', sex='male',course='B.Tech', branch='cs', xth_percent&gt;60, 
       x2percent&gt;60, gradpercent&gt;60 and (if ranktype='other' ) then 
       no._of_not_null_semester&gt;number elseifranktype='Leet') then 
       no._of_not_null_semester&gt;number-2
  </code></pre>
  
  <p>sem 1-8 contains percentage for 8 semesters, and I want to filter results for each student if they have cleared 3 semesters or 4 semester i.e. not null values out of 8 values</p>
  
  <pre><code> no._of_not_null_semester 
  </code></pre>
  
  <p>needs to be calculated, it is not a part of database, need help with that as well. </p>
  
  <p>Required Query</p>
  
  <pre><code>             SELECT * FROM student_test 
                        INNER JOIN master_test ON student_test.id=master_test.id 
                        WHERE sex='male' and batch='2009' and course='B.Tech' 
                        and xthpercent&gt;60 and x2percent&gt;60 and 
                        WHEN ranktype='Leet' THEN 
                                SUM(CASE WHEN sem1 IS NOT NULL THEN 1 ELSE 0
                                    WHEN sem2 IS NOT NULL THEN 1  ELSE 0
                                    WHEN sem3 IS NOT NULL THEN 1 ELSE 0
                                    WHEN sem4 IS NOT NULL THEN 1  ELSE 0
                                    WHEN sem5 IS NOT NULL THEN 1  ELSE 0) &gt;2
                        ELSE 
                               SUM(CASE WHEN sem1 IS NOT NULL THEN 1 ELSE 0
                                   WHEN sem2 S NOT NULL THEN 1  ELSE 0
                                    WHEN sem3 IS NOT NULL THEN 1 ELSE 0
                                    WHEN sem4 IS NOT NULL THEN 1  ELSE 0
                                    WHEN sem5 IS NOT NULL THEN 1  ELSE 0) &gt;4
  </code></pre>