--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表连接呢?这是因为要保证查出来的人要学了操作系统的人
学生选课查询(4)
最新推荐文章于 2023-12-22 15:36:22 发布