[quote]
问题描述:
现有3张表:分别是
学生信息表S(sno,sname)
课程信息表C(cno,cname,cteacher)
学生与课程关系表SC(sno,cno,scroce)
问题1:查询出所有未选修过李明老师所教课程的学生姓名.
问题2:查询出有2门以上未及格的学生姓名
问题3:查询出既选修课程"1"也选修了课程"2"的学生姓名
主要考察:group by 和inner join ,having条件筛选,以及count函数的使用
group by 主要是对结果集进行分组,然后采用having搭配使用进行数据的筛选,
[/quote]
drop table S,SC,C
create table S(
sno varchar(15), --学号
sname varchar(15) --学生姓名
)
create table C(
cno varchar(15), --课程编号
cname varchar(15), --课程名称
cteacher varchar(15) --授课教师
)
create table SC(
sno varchar(15), --学号
cno varchar(15), --课程编号
scroce float --分数
)
select * from S;
delete from S;
insert into S(sno,sname)values('s001','jerry');
insert into S(sno,sname)values('s002','tom');
insert into S(sno,sname)values('s003','jason');
delete from C;
select * from C;
insert into C(cno,cname,cteacher)values('c001','1','李明');
insert into C(cno,cname,cteacher)values('c002','2','李明0');
insert into C(cno,cname,cteacher)values('c003','3','李明1');
insert into C(cno,cname,cteacher)values('c004','4','李明2');
delete from SC;
select * from SC;
insert into SC(sno,cno,scroce)values('s001','c001',40);
insert into SC(sno,cno,scroce)values('s001','c002',50);
insert into SC(sno,cno,scroce)values('s002','c001',50);
insert into SC(sno,cno,scroce)values('s002','c003',50);
select
a.sno,a.sname,b.cno
from S as a
inner join SC as b on b.sno=a.sno
where not exists(
select * from SC where sno=b.sno and cno in(select cno from C where cteacher = '李明')
);
--有2门未及格的学生
select
a.sname
from S as a
inner join SC as b on b.sno=a.sno
where b.scroce<60
group by a.sname
having count(sname)>1;
--既选修了课程1也选修了课程2的学生姓名
select
a.sname
from S a
inner join SC b on a.sno=b.sno
inner join C c on c.cno=b.cno
where b.cno in ('c001','c002')
group by a.sname
having count(a.sname)=2;