求各位高手帮帮忙 求答案

数据库表的结构
学生姓名
课程名称
考试成绩
张三
英语
98
张三
数学
57
李四
数学
60
李四
英语
82
王五
英语
59
王五
数学
90

1、通过查询语句,查出数学成绩低于英语成绩的学生信息,字段要求显示学生姓名、数学成绩、英语成绩。
2、通过查询语句,查出每个学生课程成绩是否合格,(0-59分为不及格,60-75分为及格,76-91分为良好,91-100分为优秀),字段要求显示学生姓名、数学成绩、英语成绩。

3、通过查询语句,查出英语、数学最高成绩的学生姓名和考试成绩,英语、数学最低成绩的学生姓名和考试成绩。

假设你这个源表的表名为bd(表名随便起的),
字段:学生姓名为xm,课程名称为kc,考试成绩为cj。

1 :
select xm 学生姓名,
sum(decode(kc, '数学', cj, 0)) 数学成绩,
sum(decode(kc, '英语', cj, 0)) 英语成绩
from bd
where xm in (select b.xm
from (select xm, cj from bd where kc = '数学') b,
(select xm, cj from bd where kc = '英语') c
where b.xm = c.xm
and b.cj < c.cj)
group by xm;

2 :
select xm 学生姓名,
(case
when sx <= 59 then
'不及格'
when sx between 60 and 75 then
'及格'
when sx between 76 and 90 then
'良好'
when sx between 91 and 100 then
'优秀'
end) 数学成绩,
(case
when yy <= 59 then
'不及格'
when yy between 60 and 75 then
'及格'
when yy between 76 and 90 then
'良好'
when yy between 91 and 100 then
'优秀'
end) 英语成绩
from (select xm,
sum(decode(kc, '数学', cj, 0)) sx,
sum(decode(kc, '英语', cj, 0)) yy
from bd
group by xm);

3 :
a.英语最高成绩:
select xm, yy
from (select xm,
sum(decode(kc, '数学', cj, 0)) sx,
sum(decode(kc, '英语', cj, 0)) yy
from bd
group by xm)
where yy in (select max(yy)
from (select xm,
sum(decode(kc, '数学', cj, 0)) sx,
sum(decode(kc, '英语', cj, 0)) yy
from bd
group by xm));

b.英语最低成绩:
select xm, yy
from (select xm,
sum(decode(kc, '数学', cj, 0)) sx,
sum(decode(kc, '英语', cj, 0)) yy
from bd
group by xm)
where yy in (select min(yy)
from (select xm,
sum(decode(kc, '数学', cj, 0)) sx,
sum(decode(kc, '英语', cj, 0)) yy
from bd
group by xm));

c.数学最高成绩:
select xm, sx
from (select xm,
sum(decode(kc, '数学', cj, 0)) sx,
sum(decode(kc, '英语', cj, 0)) yy
from bd
group by xm)
where sx in (select max(sx)
from (select xm,
sum(decode(kc, '数学', cj, 0)) sx,
sum(decode(kc, '英语', cj, 0)) yy
from bd
group by xm));

d.数学最低成绩:
select xm, sx
from (select xm,
sum(decode(kc, '数学', cj, 0)) sx,
sum(decode(kc, '英语', cj, 0)) yy
from bd
group by xm)
where sx in (select min(sx)
from (select xm,
sum(decode(kc, '数学', cj, 0)) sx,
sum(decode(kc, '英语', cj, 0)) yy
from bd
group by xm));

以上语句全部在oracle数据库中实验成功了。你别告诉我你的数据库是微软的SQLSERVER。希望对你有帮助。当然了,有问题的话继续追问我。
温馨提示:答案为网友推荐,仅供参考
相似回答