学生选课查询(4)

--1. 李老师所教的课程号、课程名称
select cid,cname from c where teacher like '李%';

--2. 年龄大于23岁的女学生的学号和姓名
select sid,sname from s where age>23;

--3. “王小丽”所选修的全部课程名称
select cname from c where cid in (select cid from sc where sid=(select sid from s where sname='王小丽'));
select cname from s,sc,c where s.sid=sc.sid and sc.cid=c.cid and sname='王小丽';

--4. 所有成绩都在80分以上的学生姓名及所在系
--  注意点:是成绩都在80
select sname,department from s where sid in (select sid from sc group by sid having min(grade)>80);

--5. 没有选修“操作系统”课的学生姓名
select sname from s where sid not in (select sid from sc where cid=(select cid from c where cname='操作系统'));

--6. 与“王小丽”同乡的男生姓名及所在系
select sname,department from s where birthplace=(select birthplace from s where sname='王小丽') and sex='男';

--7. 英语成绩比数学成绩好的学生(注意!!!)
select * from s where (select grade from sc join c on sc.cid=c.cid where sc.sid=s.sid and cname='英语')>(select grade from sc join c on sc.cid=c.cid where sc.sid=s.sid and cname='数学');

--8. 选修同一门课程时,女生比男生成绩好的学生名单(注意!!!)
select distinct sname from s join sc x on s.sid=x.sid where sex='女' and grade>all(select grade from s,sc where s.sid=sc.sid and sex='男' and sc.cid=x.cid);

--9. 至少选修两门以上课程的学生姓名、性别
select distinct sname,sex from s where sid in (select sid from sc group by sid having count(cid)>2);

--10. 选修了李老师所讲课程的学生人数;
select count(sid) from sc where cid in (select cid from c where teacher like '李%');

--11. 没有选修李老师所讲课程的学生人数;
select count(sid) from sc where cid not in(select cid from c where teacher like '李%');

--12. “操作系统”课程得最高分的学生姓名、性别、所在系;(注意!!!)
select s.sid,sname,sex,department from s,sc,c where s.sid=sc.sid and sc.cid=c.cid and cname='操作系统' and grade=(select max(grade) from sc where cid=(select cid from c where cname='操作系统'));
--注意:为什么外层查询语句要进行3表连接呢?这是因为要保证查出来的人要学了操作系统的人
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值