I am not quite sure what "pass mark is given 33
" means.
I assume that it is 33% of Fullmark
,
for example if Fullmark
=75 then the pass score is 33% of 75 = 24,75 points,
which means that a student must gain at least 24,75 points to pass the exam.
In this case a simple check with an antijoin can help:
WHERE NOT EXISTS(
SELECT null FROM exam e
WHERE e.id = r.id
AND e.score/e.Fullmark < 0.33
);
the above means: "give me only these records for which does not exist any exam with pas mark < 33%"
A full query is shown below, and a demo here --> http://www.sqlfiddle.com/#!2/50ef8/2
SELECT id, Names, TOTALSCORE, Rank
FROM
(
SELECT t.*,
IF(@p = TOTALSCORE, @n, @n := @n + 1) AS Rank,
@p := TOTALSCORE
FROM(
SELECT id, Names,SUM(score) TOTALSCORE
FROM exam, (SELECT @n := 0, @p := 0) n
GROUP BY id
ORDER BY TOTALSCORE DESC
) t
) r
WHERE NOT EXISTS(
SELECT null FROM exam e
WHERE e.id = r.id
AND e.score/e.Fullmark < 0.33
);
-- EDIT --
The above query calculates incorrect rank values, here is improved version:
SELECT id, Names, TOTALSCORE, Rank
FROM
(
SELECT t.*,
IF(@p = TOTALSCORE, @n, @n := @n + 1) AS Rank,
@p := TOTALSCORE
FROM(
SELECT id, Names,SUM(score) TOTALSCORE
FROM exam e1, (SELECT @n := 0, @p := 0) n
WHERE NOT EXISTS(
SELECT null FROM exam e2
WHERE e1.id = e2.id
AND e2.score/e2.Fullmark < 0.33
)
GROUP BY id
ORDER BY TOTALSCORE DESC
) t
) r
;
and demo: --> http://www.sqlfiddle.com/#!2/50ef8/4