Student (stud_id) (stud_name) Subject (sub_id) (sub_name) (sub_man) StudentInSubject (stud_id) (sub_id) (grade)Now we can write a query that uses a cross join to create every possible combination of students and subjects, but only for the mandatory subjects. We'll use a nested subquery to fetch the grade from the StudentInSubject table.
SELECT stud_id, stud_name, sub_id, sub_name, (SELECT grade FROM StudentInSubject WHERE stud_id = student.stud_id AND sub_id = subject.sub_id) FROM student CROSS JOIN subject WHERE subject.sub_man = 1Then you can create another query that joins the three tables together for the non-mandatory subjects and UNION it to the above query.