• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

SQL Server练习题2_sqlserver

sqlserver 搞代码 3年前 (2018-06-16) 78次浏览 已收录 0个评论

题目2

问题描述:
已知关系模式:
S (SNO,SNAME)                       学生关系。SNO 为学号,SNAME 为姓名
C (CNO,CNAME,CTEACHER)  课程关系。CNO 为课程号,CNAME 为课程名,CTEACHER 为任课教师
SC(SNO,CNO,SCGRADE)        选课关系。SCGRADE 为成绩

要求实现如下5个处理:
  1. 找出没有选修过“李明”老师讲授课程的所有学生姓名

http://www.gaodaima.com/36145.htmlSQL Server练习题2_sqlserver

  2. 列出有二门以上(含两门)不及格课程的学生姓名及其平均成绩
  3. 列出既学过“1”号课程,又学过“2”号课程的所有学生姓名
  4. 列出“1”号课成绩比“2”号同学该门课成绩高的所有学生的学号
  5. 列出“1”号课成绩比“2”号课成绩高的所有学生的学号及其“1”号课和“2”号课的成绩

1. 找出没有选修过“李明”老师讲授课程的所有学生姓名
–实现代码:
SELECT SNAME FROM S
WHERE NOT EXISTS(
    SELECT * FROM SC,C
    WHERE SC.CNO=C.CNO
         AND C.CTEACHER=’李明’
          AND SC.SNO=S.SNO)

2. 列出有二门以上(含两门)不及格课程的学生姓名及其平均成绩
–实现代码:
SELECT S.SNO,S.SNAME,AVG_SCGRADE=AVG(SC.SCGRADE)
FROM S,SC,(
    SELECT SNO
    FROM SC
    WHERE SCGRADE<60
    GROUP BY SNO
    HAVING COUNT(DISTINCT CNO)>=2
)A WHERE S.SNO=A.SNO AND SC.SNO=A.SNO
GROUP BY S.SNO,S.SNAME

3. 列出既学过“1”号课程,又学过“2”号课程的所有学生姓名
–实现代码:
SELECT S.SNO,S.SNAME
FROM S,(
    SELECT SC.SNO
    FROM SC,C
    WHERE SC.CNO=C.CNO
        AND C.CNAME IN(‘1′,’2’)
    GROUP BY SNO
    HAVING COUNT(DISTINCT CNO)=2
)SC WHERE S.SNO=SC.SNO

4. 列出“1”号课成绩比“2”号同学该门课成绩高的所有学生的学号
–实现代码:
SELECT S.SNO,S.SNAME
FROM S,SC SC1,SC SC2
    WHERE SC1.CNO=’1′
        AND SC2.SNO=’2′
        AND SC1.CNO=S.CNO
        AND SC1.SCGRADE>SC2.SCGRADE

5. 列出“1”号课成绩比“2”号课成绩高的所有学生的学号及其“1”号课和“2”号课的成绩
–实现代码:
SELECT SC1.SNO,[1号课成绩]=SC1.SCGRADE,[2号课成绩]=SC2.SCGRADE
FROM SC SC1,SC SC2
WHERE SC1.CNO=’1′
        AND SC2.CNO=’2′
        AND SC1.SNO=SC2.SNO
        AND SC1.SCGRADE>SC2.SCGRADE

Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=384993

[点击此处收藏本文]   发表于 2005年05月31日 17:31:00

 十年等待 发表于2005-06-06 12:04 PM  IP: 61.186.252.*
你好:

1. 找出没有选修过“李明”老师讲授课程的所有学生姓名

SELECT SNAME FROM S
WHERE NOT EXISTS(
SELECT * FROM SC,C <=这里是否要加上S
WHERE SC.CNO=C.CNO
AND CNAME=’李明’ <=应该是CTEACHER = ‘李明’ 吧
AND SC.SNO=S.SNO)

 
 十年等待 发表于2005-06-06 12:47 PM  IP: 61.186.252.*
列出“1”号课成绩比“2”号同学该门课成绩高的所有学生的学号
SELECT S.SNO,S.SNAME
FROM S,(
SELECT SC1.SNO
FROM SC SC1,C C1,SC SC2,C C2
WHERE SC1.CNO=C1.CNO AND C1.NAME=’1′
AND SC2.CNO=C2.CNO AND C2.NAME=’2′
<=这里好像牛头不对马嘴吧??
<=是AND SC2.CNO=C2.CNO AND SC2.SNO=’2’才对吧
AND SC1.SCGRADE>SC2.SCGRADE
)SC WHERE S.SNO=SC.SNO

而且“1”号课,就我的理解是course的ID,也就是CNO,怎么会是CNAME,而且这里你还写成了C1.NAME,这种态度要不得阿

我是新手,一般都是上网查资料的,还好以前学了一点点,不然对于那些一点都不会的人,不是被楼主害死了,要么不说,要么就要有认真的态度,直言所至,请楼主谅解

 ghb 发表于2005-11-14 12:04 PM  IP: 61.236.10.*
/*
问题描述:
已知关系模式:
S (SNO,SNAME) 学生关系。SNO 为学号,SNAME 为姓名
C (CNO,CNAME,CTEACHER) 课程关系。CNO 为课程号,CNAME 为课程名,CTEACHER 为任课教师
SC(SNO,CNO,SCGRADE) 选课关系。SCGRADE 为成绩

要求实现如下5个处理:
1. 找出没有选修过“李明”老师讲授课程的所有学生姓名
2. 列出有二门以上(含两门)不及格课程的学生姓名及其平均成绩
3. 列出既学过“1”号课程,又学过“2”号课程的所有学生姓名
4. 列出“1”号课成绩比“2”号同学该门课成绩高的所有学生的学号
5. 列出“1”号课成绩比“2”号课成绩高的所有学生的学号及其“1”号课和“2”号课的成绩
*/

–create table s(sno varchar(10),sname varchar(20))
–create table c(cno varchar(10),cname varchar(20),cteacher varchar(20))
–create table sc(sno varchar(10),cno varchar(20),scgrade integer)

insert into s
select ‘1’,’ghb’
union all select ‘2’,’tw’
union all select ‘3’,’wkp’

insert into c
select ‘1’,’语文’,’李明’
union all select ‘2’,’数学’,’王了’
union all select ‘3’,’英语’,’其它’

insert into sc
select ‘1’,’2′,50
union all select ‘1’,’3′,52
union all select ‘2’,’1′,80
union all select ‘2’,’2′,90
union all select ‘2’,’3′,59
union all select ‘3’,’1′,100
union all select ‘3’,’2′,59
union all select ‘3’,’3′,70

–delete from sc where sno = ‘1’ and cno = ‘1’
select *
from s
select *
from c
select *
from sc
— 1. 找出没有选修过“李明”老师讲授课程的所有学生姓名
select sname
from s
where s.sno not in (select sc.sno from sc,c where sc.cno = c.cno and c.cteacher = ‘李明’)

SELECT SNAME FROM S
WHERE NOT EXISTS(
SELECT * FROM SC,C
WHERE SC.CNO=C.CNO
AND C.CTEACHER=’李明’
AND SC.SNO=S.SNO)

— 2. 列出有二门以上(含两门)不及格课程的学生姓名及其平均成绩
select s.sname,avg(sc.scgrade) as avgsc
from s,sc
where s.sno = sc.sno and s.sno in (select sno from sc where scgrade < 60 group by sno having count(sno) >= 2)
group by s.sname

select s.SNAME,avg(sc.SCGRADE) from S,sc where sc.SCGRADE<60 and s.sno=sc.sno group by s.SNAME having count(sc.SCGRADE)>=2

SELECT S.SNO,S.SNAME,AVG_SCGRADE=AVG(SC.SCGRADE)
FROM S,SC,(
SELECT SNO
FROM SC
WHERE SCGRADE<60
GROUP BY SNO
HAVING COUNT(DISTINCT CNO)>=2
)A WHERE S.SNO=A.SNO AND SC.SNO=A.SNO
GROUP BY S.SNO,S.SNAME

— 3. 列出既学过“1”号课程,又学过“2”号课程的所有学生姓名
select tem.sname
from
(
select s.sname
from s
where s.sno in(select sno from sc where cno = ‘1’)
union all
select s.sname
from s
where s.sno in(select sno from sc where cno = ‘2’)
)tem
group by tem.sname
having count(tem.sname) > 1

select s.sname from s,sc where s.sno=sc.sno and sc.cno=1 and sc.sno in (select b.sno from sc b where b.sno=sc.sno and b.cno=2)

select s.sname from s,sc where s.sno=sc.sno and sc.cno=1 and exists (select b.sno from sc b where b.sno=sc.sno and b.cno=2)

SELECT S.SNO,S.SNAME
FROM S,(
SELECT SC.SNO
FROM SC,C
WHERE SC.CNO=C.CNO
AND C.cno IN(‘1′,’2’)
GROUP BY SNO
HAVING COUNT(DISTINCT c.CNO)=2
)SC WHERE S.SNO=SC.SNO

— 4. 列出“1”号课成绩比“2”号同学该门课成绩高的所有学生的学号
select sc.sno
from sc
where cno = ‘1’ and scgrade > (select scgrade from sc where sno = ‘2’ and cno = ‘1’) and sno <> ‘2’

SELECT Sc.SNO FROM SC where sc.cno = ‘1’ and exists (select * from sc b where b.sno = sc.sno and sc.scgrade > b.scgrade and b.sno = ‘2’)

select s.SNO from s,sc where s.sno=sc.sno and sc.cno=1 and sc.SCGRADE>(select b.SCGRADE from sc b where b.sno=sc.sno and b.cno=2)

SELECT S.SNO,S.SNAME
FROM S,SC SC1,SC SC2
WHERE SC1.CNO=’1′
AND SC2.SNO=’2′
AND SC1.CNO=S.CNO
AND SC1.SCGRADE>SC2.SCGRADE

select sc1.sno
from sc sc1,sc sc2
where sc1.cno = ‘1’ and sc1.sno <> ‘2’ and sc2.cno = ‘1’ and sc2.sno = ‘2’ and sc1.scgrade > sc2.scgrade and sc1.cno = sc2.cno

— 5. 列出“1”号课成绩比“2”号课成绩高的所有学生的学号及其“1”号课和“2”号课的成绩
select sc1.sno,sc1.scgrade,sc2.scgrade
from sc sc1,sc sc2
where sc1.sno = sc2.sno and sc1.cno = ‘1’ and sc2.cno = ‘2’ and sc1.scgrade > sc2.scgrade

select a.SNO,a.SCGRADE from sc a where (a.CNO=1 or a.CNO=2)
and a.sno in
(select s.SNO from s,sc where s.sno=sc.sno and sc.cno=1 and sc.SCGRADE>(select b.SCGRADE from sc b where b.sno=sc.sno and b.cno=2))

SELECT SC1.SNO,[1号课成绩]=SC1.SCGRADE,[2号课成绩]=SC2.SCGRADE
FROM SC SC1,SC SC2
WHERE SC1.CNO=’1′
AND SC2.CNO=’2′
AND SC1.SNO=SC2.SNO
AND SC1.SCGRADE>SC2.SCGRADE

drop table s
drop table c
drop table sc

 shenjane 发表于2006-02-07 3:13 PM  IP: 210.22.152.*
第四、第五题都有一些错误
SELECT S.SN,SC1.SCGRADE AS 课程1,SC2.SCGRADE as 课程2 FROM S,SC SC1,SC SC2 WHERE
S.SNO = SC1.SNO AND SC1.CNO=’0001’AND SC2.CNO=’0002′
AND SC1.SNO=SC2.SNO AND SC1.SCGRADE<SC2.SCGRADE

 

欢迎大家阅读《SQL Server练习题2_sqlserver,跪求各位点评,若觉得好的话请收藏本文,by 搞代码


喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址