mysql - What's wrong with this query using averages? -


courses (cid, cname, description)

professors (pid, pname)

teaching (tid, cid, pid, year, semester, department)

students (sid, lname, fname, b_date, department)

enrolled_stud (sid, tid, enrollment_date, grade, grade_date)

the question: query students names have highest average:

select a.sname,a.avg (select s1.sname,avg(e.grade)as avg       students s1       natural join enrolledstudents e       group s1.sid       ) a.avg=(select max(a.avg)) 

you can see results in sqlfiddle, , can see doing wrong, , dont what! seem correct me...

since it's study, i'll post hint first. think clause - a current row. of course a.avg equals max(a.avg).

update

try following:

select s1.sname, avg(e.grade) avg students s1 natural join enrolledstudents e group s1.sid having avg = (select avg(e.grade) avg    enrolledstudents e   group e.sid order avg desc limit 1); 

Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c# - Copy ObservableCollection to another ObservableCollection -

All overlapping substrings matching a java regex -