select e.mark, course, g.name, s.name, lastname, surname, p.name, f.name from students s
left join groupa g on s.group_id = g.id
inner join exam_mark e on s.id = e.student_id
inner join peciality p on g.peciality_id = p.id
inner join facult f on p.facult_id = f.id
where e.mark >= 4 and e.mark <> 2
-- Только значимые столбцы
CREATE TABLE exam_mark (ID INT IDENTITY, student_id INT, mark INT)
INSERT exam_mark (student_id, mark )
VALUES (4, 5), (4, 4), (4, 4), (4, 5), (4, 2),
(5, 5), (5, 5), (5, 4), (5, 3), (5, 5),
(6, 2), (6, 2), (6, 3), (6, 2), (6, 2),
(7, 3), (7, 3), (7, 3), (7, 3), (7, 3)
SELECT gd.*
FROM exam_mark AS gd
LEFT JOIN exam_mark AS bd ON bd.student_id = gd.student_id AND bd.mark = 2
-- у кого есть 4, 5 но нет 2
WHERE (gd.mark IN (4, 5)) AND (bd.student_id IS NULL)
select e.mark, course, g.name, s.name, lastname, surname, p.name, f.name from students s
left join groupa g on s.group_id = g.id
inner join exam_mark e on s.id = e.student_id
inner join peciality p on g.peciality_id = p.id
inner join facult f on p.facult_id = f.id
where e.mark >= 4
and not exists (select 1
from exam_mark e1
where e1.student_id = s.id
and e1.mark <> 2)
select e.mark, course, g.name, s.name, lastname, surname, p.name, f.name from students s
left join groupa g on s.group_id = g.id
inner join exam_mark e on s.id = e.student_id
inner join peciality p on g.peciality_id = p.id
inner join facult f on p.facult_id = f.id
where not exists (
select 1 from exam_mark e1
where e1.student_id = s.id and e1.mark = 2)