您的位置:首页 > 数据库 > Oracle

Oracle开发之SQL语句案例—学生成绩统计

2009-10-21 14:15 465 查看
学生信息表:

create table LJ_STU
(
STID NUMBER(10),
STNAME VARCHAR2(100)
);

1 Mary
2 Lily
3 Jack
4 Lucy
5 Sun
6 Roy
7 Jane
8 Mike
9 Anny
10 Davi
11 DR
12 Robin

课程表

create table LJ_COURSE
(
CID NUMBER(10),
CNAME VARCHAR2(100)
);

1 English
2 Chinese
3 Math
4 Chemistry
5 Physics
6 Geography
7 Creature
8 History

学生成绩表

create table LJ_ST_SCORE
(
SID NUMBER(10),
CID NUMBER(10),
SCORE NUMBER(10)
);

SID CID SCORE
1 1 90
1 2 78
1 3 99
1 4 85
1 5 71
1 6 45
1 7 60
1 8 56
2 1 40
2 2 56
2 3 59
2 4 55
2 5 71
2 6 85
2 7 60
2 8 96
3 1 57
3 2 79
3 3 59
3 4 55
3 5 71
3 6 85
3 7 60
3 8 96
4 1 91
4 2 79
4 3 59
4 4 55
4 5 71
4 6 85
4 7 60
4 8 96
5 1 91
5 2 80
5 3 100
5 4 55
5 5 71
5 6 85
5 7 100
5 8 96
6 1 91
6 2 79
6 3 69
6 4 55
6 5 71
6 6 85
6 7 60
6 8 96
7 1 91
7 2 79
7 3 69
7 4 55
7 5 71
7 6 85
7 7 60
7 8 96
8 1 91
8 2 79
8 3 69
8 4 55
8 5 71
8 6 85
8 7 60
8 8 96
9 1 91
9 2 79
9 3 69
9 4 55
9 5 71
9 6 85
9 7 60
9 8 96
10 1 91
10 2 79
10 3 69
10 4 55
10 5 71
10 6 85
10 7 60
10 8 96
11 1 91
11 2 79
11 3 69
11 4 55
11 5 71
11 6 85
11 7 60
11 8 96
12 1 91
12 2 34
12 3 20
12 4 50
12 5 40
12 6 20

12 7 60
12 8 40

1、查询出有两名或两名以上功课不及格(小于60分)的学生,按如下格式展示:

学生ID 学生名称 课程名称 分数

select b.stid, b.stname, c.cname, d.score
from (select s.sid, count(*)
from lj_st_score s
where s.score < 60
group by s.sid
having count(*) >= 2) a,
lj_stu b,
lj_course c,
lj_st_score d
where a.sid = d.sid
and b.stid = d.sid
and c.cid = d.cid
and a.sid = b.stid
and d.score < 60;

2、查询出每门课程成绩前3名的学生,按如下格式显示:

课程ID 课程名称 第一名学生 第二名学生 第三名学生

select c.cname as 课程名,
max(decode(a.rankno, 1, s.stname)) as 第一名,
max(decode(a.rankno, 2, s.stname)) as 第二名,
max(decode(a.rankno, 3, s.stname)) as 第三名
from (select ss.sid,
ss.cid,
ss.score,
row_number() over(partition by ss.cid order by ss.score desc) rankno
from lj_st_score ss) a,
lj_stu s,
lj_course c
where s.stid = a.sid
and c.cid = a.cid
and a.rankno <= 3
group by c.cname

3、查询出“数学”成绩在:优、良、中、差各个等级的学生人数,按如下格式展示:

等级 学生人数
select a.grade, count(*)
from (select (case
when sc.score < 60 then
'差(小于60分)'
when sc.score >= 60 and sc.score <= 70 then
'中(60~70)'
when sc.score > 70 and sc.score <= 80 then
'良好(70~80)'
when sc.score > 80 then
'优秀(80以上)'
end) grade
from lj_course c, lj_st_score sc
where sc.cid = c.cid
and c.cname = 'Math') a
group by a.grade
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: