1、学生表 Student(SID,Sname,Sage,Ssex) --SID 学生编号,Sname 学生姓名,Sage 年龄,Ssex 学生性别 编号 姓名 年龄 性别 1 赵雷 20 男 2 钱电 20 男 3 孙风 21 男 4 吴兰 18 女 5 孙兰 17 女 ![SQL练习汇总(查询“01“课程比“02“课程成绩高的学生的信息及课程分)_Sage](https://s2.51cto.com/images/blog/202302/16155402_63ede11a1a7a218593.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184) 2.课程表 Course(CID,Cname,TID) --CID --课程编号,Cname 课程名称,TID 教师编号 课程号 课程名称 教师编号 1 语文 2 2 数学 1 3 英语 3 ![SQL练习汇总(查询“01“课程比“02“课程成绩高的学生的信息及课程分)_Sage_02](https://s2.51cto.com/images/blog/202302/16155402_63ede11a2e07f70535.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184) 3.教师表 Teacher(TID,Tname) --TID 教师编号,Tname 教师姓名 教师编号 教师名 1 张三 2 李四 3 王五 ![SQL练习汇总(查询“01“课程比“02“课程成绩高的学生的信息及课程分)_Sage_03](https://s2.51cto.com/images/blog/202302/16155402_63ede11a426d555031.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184) 4.成绩表 SC(SID,CID,score) --SID 学生编号,CID 课程编号,score 分数 学生编号 课程编号 分数 1 1 80 1 2 71 1 3 87 2 1 88 2 2 70 2 3 89 3 1 68 3 2 78 3 3 87 4 1 67 4 2 58 4 3 89 5 1 56 5 2 89 6 3 38 ![SQL练习汇总(查询“01“课程比“02“课程成绩高的学生的信息及课程分)_Sage_04](https://s2.51cto.com/images/blog/202302/16155402_63ede11a5601d61556.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184) 1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数select s.*,s1.score 1score ,s2.score 2score from Student s, SC s1, SC s2 where s.SID = s1.SID and s.SID = s2.SID and s1.CID ='01' and s2.CID = '02' and s1.score > s2.score;![SQL练习汇总(查询“01“课程比“02“课程成绩高的学生的信息及课程分)_Sage_05](https://s2.51cto.com/images/blog/202302/16155402_63ede11a6a64c52603.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184) 2、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩select s.SID,s.sname,avg(c.score) avgscore from Student s,SC c where s.SID = c.SID group by c.SID having avg(c.score) >= 60;![SQL练习汇总(查询“01“课程比“02“课程成绩高的学生的信息及课程分)_Sage_06](https://s2.51cto.com/images/blog/202302/16155402_63ede11a7ef9757979.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184) 3、查询在sc表中不存在成绩的学生信息。select * from Student where SID not in( select distinct(SID) from sc) ;4、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩SELECT s.sid, s.Sname, count(sc.Cid), sum(sc.Score) from student s,sc WHERE s.Sid=sc.Sid group by sc.Sid;![SQL练习汇总(查询“01“课程比“02“课程成绩高的学生的信息及课程分)_Sage_07](https://s2.51cto.com/images/blog/202302/16155402_63ede11aa38b591696.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184) 5、查询"李"姓老师的数量select count(*) 李姓老师数量 from Teacher where tname like '李%';![SQL练习汇总(查询“01“课程比“02“课程成绩高的学生的信息及课程分)_Sage_08](https://s2.51cto.com/images/blog/202302/16155402_63ede11abbf6a39577.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184) 6、查询学过"张三"老师授课的同学的信息select s.* from student s,Teacher t,SC sc,course c where s.SID = sc.SID and sc.CID = c.CID and c.TID = t.TID and t.tname = '张三';![SQL练习汇总(查询“01“课程比“02“课程成绩高的学生的信息及课程分)_Sage_09](https://s2.51cto.com/images/blog/202302/16155402_63ede11ad375c49007.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184) 7、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息select s.* from Student s, SC s1,SC s2 where s.SID = s.SID and s1.SID=s2.SID and s1.CID='01'and s2.CID ='02'![SQL练习汇总(查询“01“课程比“02“课程成绩高的学生的信息及课程分)_Sage_10](https://s2.51cto.com/images/blog/202302/16155402_63ede11ae700d73878.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184) 8、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩select s.SID,s.sname,avg(c.score) from Student s,SC c where s.SID = c.SID and s.SID in (select SID from SC where SC.score < 60 group by SC.SID having count(*) > 1);
|