I have following tables:
Schema
tbl_semester_empid {id, semester_name varchar(50), start_date}
tbl_batch_empid {id, semester_id,batch_name, session_room}
tbl_associate_empid {id, associate_name, batch_id, contact, joining_date, induction_result, stream_result int}
The problem query is: Display semester wise associate count.
I have wrote 3 queries:
--basic query
SELECT s.ID "Semester Id", COUNT(*) "Associate Count" FROM TBL_SEMESTER_593932 s, TBL_ASSOCIATE_593932 a, TBL_BATCH_593932 b
WHERE s.ID=b.SEMESTER_ID AND a.BATCH_ID=b.ID
GROUP BY s.ID;
--used NVL function
SELECT s.ID "Semester Id", NVL(COUNT(*),0) "Associate Count" FROM TBL_SEMESTER_593932 s, TBL_ASSOCIATE_593932 a, TBL_BATCH_593932 b
WHERE s.ID=b.SEMESTER_ID AND a.BATCH_ID=b.ID
GROUP BY s.ID;
-- used LEFT OUTER JOIN and JOIN
SELECT s.ID "Semester Id", NVL(COUNT(*),0) "Associate Count"
FROM TBL_SEMESTER_593932 s LEFT OUTER JOIN TBL_BATCH_593932 b ON s.ID=b.SEMESTER_ID
JOIN TBL_ASSOCIATE_593932 a ON a.BATCH_ID=b.ID
GROUP BY s.ID;
All of them display following output (it shows records corresponding to only those semesters which have at least 1 student in it, if theis is no student in that semester, no information for that semester is displayed):
I want the semesters with no associates in them as 0 in Associates Count column. So I tried the same with two LEFT OUTE JOINs:
SELECT s.ID "Semester Id", NVL(COUNT(*),0) "Associate Count"
FROM TBL_SEMESTER_593932 s LEFT OUTER JOIN TBL_BATCH_593932 b ON s.ID=b.SEMESTER_ID
LEFT OUTER JOIN TBL_ASSOCIATE_593932 a ON a.BATCH_ID=b.ID
GROUP BY s.ID;
However the result is weird: