I have two tables in my project. Students table and Lessons table. They are connected through a foreign key (Id in Students table and StudentId in Lessons table). I want to return the highest grade for each lesson with the name of the student. I know I should implement aggregate function. It works fine for returning the highest grade of all the lessons, but I have no idea how to return the highest grade for a specific lesson (Math for example).
select s.Name, l.Grade
from Students s
inner join Lessons l on s.Id = l.StudentId
where l.Grade = (select MAX(Grade) from Lessons)
public class StudentClass
{
public int Id { get; set; }
public string Name { get; set; }
public List<Lesson> Lessons { get; set; }
}
public class Lesson
public int Id { get; set; }
public string Name { get; set; }
public int Grade { get; set; }
public int StudentId { get; set; }
[ForeignKey("StudentId")]
public StudentClass Student { get; set; }
}
select s.Name, l.Name as Lesson, MAX(l.Grade) from ... GROUP BY s.Name,l.Name.