I have to join three tables shown below into a single table.
Table: Subjects
Subject_id Subject_Name
--------------------------
1 English
2 Arabic
3 Hindi
Table : Students
Student_id Student_Name Subject_id
------------------------------------------
XXX Veena 1
YYY Ram 1
AAA Raiza 2
DDD David 3
BBB Geeth 3
Table : StudentVsSubject
Subject_id Student_id Status (1 =passed, 0 = failed)
-------------------------------------------------------------
1 XXX 1
2 AAA 0
3 BBB 1
3 DDD 0
And the output should be :
Subject TotalStudents Passed Failed NotAttended
---------------------------------------------------
English 2 1 0 1
Arabic 1 0 1 0
Hindi 2 1 1 0
I'm stuck with by getting TotalStudents only.
SELECT Subjects.Subject_name
,count(Students.Students) AS TotalStudents
FROM Subjects
LEFT JOIN Students ON Subjects.Subject_id = Students.Subject_id
GROUP BY Subjects.Subject_name
caseexpressions to do conditional aggregation!