ID | Subject | Grade
12113 BM A
12113 BI A
12113 MM A
12113 SJ A
12113 GE B
12113 SV A
12113 PJ A
12112 BM A
12112 BI A
12112 MM A
12112 SJ A
12112 GE A
12112 SV A
12112 PJ A
Hie all, above are the sample table of showing the studentID with multiple subject takens. I couldnt figure out how to use mysql query to check if the same studentID has score A for all subjects.
was trying to use HAVING MAX(Grade) = 'A'
but doesnt help. any advice?
select ID,
case when count(ID) = sum(case when Grade = 'A' then 1 else 0 end) then 'YES' else 'NO' end as IsAllAGrade
from yourTable
group by ID
This will return all students that have A
in all subjects:
select distinct id from t t1
where not exists (
select * from t t2
where t1.id = t2.id and grade != 'A')
If you want to check for a particular student just add a where
clause. By the way... can a student have more than one result for a given subject? (Twice the same subject but with different grade)
You can check the fiddle here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With