有一个学生成绩表字段分别是学生姓名,科目名称,成绩
1.使用SQL统计一下两门课程在80分以上的学生名单 2.统计一下每个学生优,良,不及格的科目数,优大于等于80分,良60-79,不合格小于60分。
参考解答
假设表结构如下:
create table stuscore(
name varchar2(20),
subject varchar2(20),
score number
);
两门课程在80分以上的学生名单
select name from stuscore
where score >= 80 group by name having count(*)>=2
每个学生优,良,不及格的科目数
select name,
count(case when score<60 then 1 end) 不合格,
count(case when score>=60 and score <80 then 1 end) 良,
count(case when score>=80 then 1 end) 优
from stuscore group by name;