MySQL 0 memory solution, very easy to understand, Window function
with last_one as (
select
    exam_id,
    min(score)over(partition by exam_id) as lowest
from exam
group by 1
),
    
rk as (
select 
    student_id,
    score,
    exam_id,
    rank()over(partition by exam_id order by score desc) as score_rk
from exam
),

non_quiet as (
select
    student_id
    from rk inner join last_one on rk.exam_id = last_one.exam_id
    where rk.score = last_one.lowest
    or rk.score_rk = 1
)

select distinct
student.student_id,
student.student_name
from student
inner join exam on student.student_id = exam.student_id
where student.student_id not in (
select * from non_quiet
)
order by student.student_id
Comments (0)