如果数据表如下

Name cgpa dept
a     4.0   x
b     3.9   x
c     3.8   x
d     3.8   y
e     4.0   y
f     3.9   z
g     3.6   z

选择cgpa高于同一系所有其他学生的学生姓名和cgpa:
select NAME, CGPA
from STD_INFO T1
where CGPA > all
(
select CGPA
from STD_INFO T2
where lower(T2.DEPT)=lower(T1.DEPT)
and T2.STD_NO != T1.STD_NO
);

我需要了解完整的模拟…..请帮助我…..当我们比较上面相同的表命名(t1,t2)时,它叫什么??

最佳答案

您想找到每个max(CGPA)DEPTinner join数据集到您的STD_INFO表:

select s1.NAME,s1.CGPA
from STD_INFO as s1
  inner join (select DEPT,max(CGPA) as CGPA
              from STD_INFO
              group by DEPT) as s2
    on s2.DEPT = s1.DEPT
    and s2.CGPA = s1.CGPA;

SQL FIDDLE

10-01 23:23