https://www.youtube.com/watch?v=FkW-wkVsJgQ
CREATE TABLE students ( student_name VARCHAR(50), subject VARCHAR(50), marks INT );
INSERT INTO students (student_name, subject, marks) VALUES
('Alice', 'Math', 65),
('Alice', 'Science', 80),
('Alice', 'English', 78),
('Bob', 'Math', 82),
('Bob', 'Science', 85),
('Bob', 'English', 88),
('Catherine', 'Math', 70),
('Catherine', 'Science', 72),
('Catherine', 'English', 68),
('Daniel', 'Math', 99);
with cte as (
select *
,rank() OVER (partition by student_name order by marks desc) as rnk
from students
)
select student_name,SUM(marks) as marks
from cte
where rnk<=2
group by student_name