1.
Code:
-- I defined the field Marks as Integer, that's the reason for the CAST...
SELECT StudentName, SubjectName, ISNULL(CAST(Marks AS VARCHAR(6)),'Absent') AS Marks
FROM tbStudent INNER JOIN tbMarks
ON tbStudent.StudentId = tbMarks.StudentId
INNER JOIN tbSubject ON tbMarks.SubjectId = tbSubject.SubjectId
2.
Code:
SELECT SubjectName, SUM(Passed) AS Passed, SUM(Failed) AS Failed
FROM tbSubject INNER JOIN (
/*
This query (/derived table) get's the maximum mark achieved by a student in a given subject
and introduces the fields Passed and Failed...
*/
SELECT
StudentId,
SubjectId,
CASE
WHEN ISNULL(MAX(Marks), 0) >= 50 THEN 1
ELSE 0
END
AS Passed,
CASE
WHEN ISNULL(MAX(Marks), 0) < 50 THEN 1
ELSE 0
END
AS Failed
FROM tbMarks
GROUP BY StudentId, SubjectId
) AS tbDerived
ON tbSubject.SubjectId = tbDerived.SubjectId
GROUP BY tbSubject.SubjectName