最大平均gpa sql

时间:2013-10-09 02:59:11

标签: sql max average

好吧,这是我需要回答的问题:哪个部门/专业的平均GPA最高?

我对SQL很新,实际上我现在只做了3个星期。我有平均GPA最高的答案,但我想知道如何让它显示主要和gpa,而不仅仅是gpa。

    select max (avg_gpa)
    from (select major, avg(gpa) AS avg_gpa
    from lab5_student
    group by major);

感谢您对此有任何见解!

专利

2 个答案:

答案 0 :(得分:1)

将主要列添加到select语句中,如下所示:

  select major, max (avg_gpa)
    from (select major, avg(gpa) AS avg_gpa
    from lab5_student
    group by major);

这应该有效。

答案 1 :(得分:0)

Select y.* from
(
 SELECT MAX(avg_gpa) as max_avg_gpa
 FROM (
       SELECT major,AVG(gpa) AS avg_gpa
       FROM lab5_student
       GROUP BY major
      )v
 )x
 inner join

(
       SELECT major,AVG(gpa) AS avg_gpa
       FROM lab5_student
       GROUP BY major
)y
on x.max_avg_gpa=y.avg_gpa

<强> SQL FIDDLE DEMO