请教 2 个 sql 语句。 (100分)

I

iCANK

Unregistered / Unconfirmed
GUEST, unregistred user!
表 s
s# sname age sex
001 张三 22 M
002 李四 22 F
003 乔峰 30 M
004 段誉 25 M
005 虚竹 23 M
006 王语焉 80 F
007 王五 3 M

表 sc
s# c# grade
001 01 90
001 02 86
001 03 89
001 04 99
002 01 98
002 03 100
003 02 68
003 04 81
004 01 75
005 02 74

表 c
c# cname teacher
01 语文 金庸
02 数学 华罗庚
03 英语 李阳
04 计算机 比尔

高程数据库知识的一个题目。现在有两个问题:

一。
选出所学课程包含学生某某所学课程的学生姓名。
比如,选出包含学生 李四 所学课程的学生姓名,结果就应该是 张三 和 李四。而我的 sql 语句(只选出了学号):
select s# from sc
where c# in
(
select c# from sc
where s# in
(
select s# from s
where sname='李四'
)
)
group by s#
总是把 段誉 也选出来了,实际上他只学了一门课。很明显不对。

这个 sql 语句应该怎么写啊?没有比较集合的函数啊。

问题二。
选出平均成绩最高的学生用下面的 sql 语句:
select * from s
where s# in
(
select s# from sc
group by s#
having avg(grade) >= all --①
(
select avg(grade) from sc
group by s#
)
)

我把 ① 处的 having avg(grade) >= all 改为 having avg(grade) > all 或者 having avg(grade) = all
都不能得到正确结果。为什么,解释一下 all() 。
另外,这个题目怎么用 max() 来做?
谢谢!
 
1:select cname from c
where c# in (select c# from sc
where s# in (select s# from s
where sname='李四'))
 
1、select s# from sc sc1
where not exists
(select * from sc sc2
where sc2.s#="李四" and
not exists ( select * from sc sc3 where sc3.s#=sc1.s# and
sc3.c#=sc2.c#))
 
select cname.c,sname.s from c ,s
where c#.c in (select c#.sc from sc
where s#.sc in (select s#.s from s
where sname.s='李四'))
 
jesse.zhou:
你这还没有完啊。这样选出来的是 语文 英语 ,是李四修的二门课。我的要求是选出包含
和李四修的课程的人。在这里,张三也修了语文和英语。这样,最后的结果应该是 张三 李四。

dz2050:
看不懂,sc1 sc2 sc3是什么意思啊?

weihao:
你这个是什么意思?也看不懂。:(但我修改成这样:
select c.cname,s.sname from c ,s
where c.c# in (select sc.c# from sc
where sc.s# in (select s.s# from s
where s.sname='李四'))
运行结果是错误的。

也许大家都没有看懂我的问题。
我的要求是:选出 包含 学生 某某 的选修课程 的学生的名字。
例如:在这个数据库中,选出 包含 学生 李四 的选修课程(李四选修了 语文和英语 二门课)
的学生的名字。在这里,张三修了 四门课(语文 数学 英语 计算机)包含了 李四 所选修的
课程。所以,检索的结果应该是 张三 李四。

大家帮忙看看,这个 SQL 语句我头都想破了。:(
 
Select S.S#,Sname
From (Select S#,count(S#) as tmp
From sc
Where c# in (Select c#
From s,sc
Where s.s#=sc.s# and Sname='李四')
Group by S#) AAA,S
Where AAA.S#=S.S# and
tmp=(Select count(C#) From S,SC Where S.S#=SC.S# and Sname='李四')
 
选出 包含 学生 李四 的选修课程的学生的名字。
dz2050的意思是说,没有李四的任意一门课是这些学生不学的,用not exists
但是还要加distinct,否则可能会重复
select distinct s# from sc sc1
where not exists
(select * from sc sc2
where sc2.s#="李四" and
not exists ( select * from sc sc3 where sc3.s#=sc1.s# and
sc3.c#=sc2.c#))

 
选出平均成绩最高的学生
1、
Select top 1 S.S#,Sname,AAA.tmp
From (Select S#,avg(grade) as tmp From sc group by s#) AAA,S
Where AAA.S#=S.S#
Order by tmp Desc

2、
Select S.S#,Sname,AAA.tmp
From (Select S#,avg(grade) as tmp
From sc Group by s#) AAA,S
Where AAA.S#=S.S# and
AAA.tmp=(select Max(tmp)
From (Select avg(grade) as tmp
From sc Group by s#) BBB)
 
1、
select sname from s
where s# in
(select a.s# from sc a,(select a.c# from sc a,s b where sname='李四'and a.s#=b.s#) b
where a.c#=b.c#
group by a.s#
having count(a.s#)=(select count(*) from (select a.c# from sc a,s b where sname='李四'and a.s#=b.s#)))
 
select s.sname from s,c,sc
where s.s#=sc.s# and (select c.c# from c,s,sc
where c.c#=sc.c# and sc.s#=s.s# and s.sname='李四')
in sc.c#
这个有什么问题请与我发e_mail:hylhgz@21cn.com 请注明错误提示
 
2、
select b.sname from
(select s#,avg(grade) as Max_g from sc
group by s#) a,s b
where a.max_g =(
select max(Max_g) from(
select avg(grade) as Max_g from sc
group by s#))
and a.s#=b.s#
 
同意:
QuickSilver

 
1、
select s.* ,sc.*,c.* from s,sc,c where s.s=sc.s and sc.c=c.c如果要对某人
进行选择那就再加个 and s.name='XX';

2、select T.s,max(T.GR) from (select s.s,avg(sc.grade) as GR
from sc,s where s.s=sc.s group by s.s) as T
 
QuickSilver 解决的不错
 
大家很帮忙
 
同意QuickSilver
 
顶部