sql老师学生成绩经典模式(50题)-练习 (前11题)
接,比如查没有被某个老师教过,语文比数学高的学生,开始觉得好复杂,想不到怎么连接,慢慢多看,多写几个,找到了一点感觉,不断改正错误,作了1/3习题,坚持


--1、查询"01"课程比"02"课程成绩高的学生,的信息及课程分数
--select * from Student a inner join SC b on(a.Sno=b.Sno)
--学生表和新的b表连接
select * from student s,
(select s1.sno,s2.score from sc s1,sc s2 where s1.sno=s2.sno
and s1.cno='01' and s2.cno='02' and s1.score>s2.score) b
where s.sno=b.sno
-- 一个人的总成绩
-- select sum(score) from SC group by Sno;
-- select * from (Student a inner join SC b on(a.Sno=b.Sno)) where 01.score>02.score;
--select sum(score)from (Student a inner join SC b on(a.Sno=b.Sno))where a.sno=01;
--select sum(score)from (Student a inner join SC b on(a.Sno=b.Sno));
--3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩
--select a.Sno,Sname,score from Student a inner join SC b on(a.Sno=b.Sno)
--select a.Sno,Sname,avg(score) from Student a inner join SC b on(a.Sno=b.Sno) where avg(score)>60;
--select avg(score),sno,sname from (student s1 ,sc s2 where s1.sno=s2.sno)b
--having avg(score)>60;
select * from student s,
(select avg(score),sno from sc group by sno having avg(score)>=60) c
where s.sno=c.sno;
--5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩 (相关子查询)
--select sno,sname,sum(score) from sc s and count(cno) as cou
select s.sname,s.SNO,cou,he from student s,
(select sno,count(cno) as cou,sum(score) as he from sc group by sno ) b
where s.sno=b.sno;
--6、查询"李"姓老师的数量
select count(tname) from teacher where tname like '李%';
--7、查询学过"张三"老师授课的同学的信息
--student s,teacher t,sc c 连3张表,
--c.cno=t.tno and c.sno=s.sno要两个条件
select * from student s,teacher t,sc c
where tname='张三' and c.cno=t.tno and c.sno=s.sno
--8、查询没学过"张三"老师授课的同学的信息 intersect交集 minus差集
---Union,对两个结果集进行并集操作,不包括重复行,同时进行默认规则的排序;
---Union All,对两个结果集进行并集操作,包括重复行,不进行排序;加s.按student列数
select * from student
minus
(select s.* from student s,teacher t,sc c
where tname='张三' and c.cno=t.tno and c.sno=s.sno)
--select s.* from student s,teacher t,sc c
--where tname='李四' and c.cno=t.tno and c.sno=s.sno
--union
--select s.* from student s,teacher t,sc c
--where tname='王五' and c.cno=t.tno and c.sno=s.sno
--李四 王五教的同时,张三可能也在教
--9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息
select * from student s,
(select s1.sno from sc s1,sc s2 where s1.cno='01' and s2.cno='02' and s1.sno=s2.sno) b
where s.sno=b.sno
select * from student s,sc s1,sc s2
where s1.cno='01' and s2.cno='02' and s1.sno=s2.sno and s.sno=s2.sno;
--10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息
select s.* from student s,sc s1
where s1.cno='01' and s.sno=s1.sno
minus
select s.* from student s,sc s1,sc s2
where s1.cno='01' and s2.cno='02' and s1.sno=s2.sno and s.sno=s2.sno;
--11、查询没有学全所有课程的同学的信息
select * from student
minus
select s.* from student s,sc s1,sc s2 ,sc s3
where s1.cno='01' and s2.cno='02' and s3.cno='03' and s1.sno=s2.sno and s.sno=s2.sno and s.sno=s3.sno;

浙公网安备 33010602011771号