s(s#,sn,sd,sa)学号 学员姓名 所属单位 年龄
c(c#,cn) 课程编号 课程名称
sc(s#,c#,g) 学号 所选修的课程编号 学习成绩
查询选修课程名称为‘计算机’的学员学号和姓名
查询选修课程编号为‘2’的学员姓名 和所属单位
use li
go
select sn,sd from s,sc
where s.s#=sc.s# and sc.c#=002
查询不选修课程编号为‘2’的学员姓名和所属单位
use li
go
select sn,sd ,s# from s
where s# not in
(
select s# from sc
where c#=002
)
查询选修全部课程的姓名和单位
use li
go
select sn,sd,s# from s
where s# in(
select s# from sc
group by s#
having count(c#)=(select count(c#) from c))
查询选修了课程的学院人数
use li
go
select count(distinct(s#)) from sc
查询选修课程超过2门的学号和单位
use li
go
select sn,sd,s# from s
where s# in(
select s# from sc
group by s#
having count(c#)>=2)
s(sno,sname) 学号 姓名
c(cno,cname,cteacher) 课程号 课程名 任课老师
sc(sno,cno,scgrade)scgrade为 成绩
找出没有选修过‘李’老师讲课的所有学生姓名
use li
go
select sname,sno from s
where sno not in
(
select sno from sc,c
where sc.cno=c.cno and c.cteacher='li'
)
查询 有2门及以上不及格课程的学生姓名和其平均成绩
use li
go
select s.sno,sname,avg(scgrade) from s,sc
where sc.sno=s.sno and sc.sno in
(select sno from sc
where scgrade<60
group by sno
having count(*)>=2)
group by s.sno,sname
查询学过 1 和 2 号课程的所有学生姓名
use li
go
select sname from s
where s.sno in
(select sno from sc
group by sno
having sum(case cno when 1 then 1 when 2 then 1 else 0 end ) = 2
)
同一题
use li
go
select sname from s
where sno in
(
select sc.Sno from sc where sc.cno = 1 and sc.Sno in (select sc.Sno from sc where sc.cno = 2)
)
查询 1号课成绩比 学号为 2号的同学的该门课成绩高的所有学生的学号
use li
go
select sno from sc
where cno=1 and scgrade>(select scgrade from sc where sno=2 and cno=1)
查询 1号课成绩比2号课成绩高的所有学生的学号及其 1 2 号课成绩
use li
go
select sc1.sno,sc1.scgrade,sc2.scgrade from sc sc1,sc sc2
where sc1.cno=1 and sc1.sno=sc2.sno and sc2.cno=2 and sc1.scgrade>sc2.scgrade