日期:2014-05-17  浏览次数:20796 次

写SQL语句
S(sno,sname)
C(cno,cname)
SC(sno,cno,grade)

这是三张表,大家应该都知道吧

写出每门课程成绩大于90分的人数的select语句,

我看到答案了,但是思路还是比较混乱,请大牛们帮我理一理思路地给出答案

比如,第一步,想到什么
      第二部,想到什么
      然后。。。。


尽量把思路写出来,非常地感谢啦!!!!

------解决方案--------------------
select c.cname, sum(case when sc.grade>=90 then 1 else 0 end) as upper_90_num
from c join sc on c.cno=sc.cno
group by c.cname;

------解决方案--------------------
逆向法

1.从 sc 表中 找出 存在 课程成绩小于90 分的 sno。

2.从sc 表中剔除 第一步得到的sno,剩下的就是所有成绩大于90分的。

3. count求数量。

sql如下:
select count(distinct sno) cnt
  from sc t
 where not exists (select 1
          from sc t1
         where t1.grade < 90
           and t.sno = t1.sno);