当前位置:   article > 正文

MYSQL查询_数据库查询所有同学的学号,姓名,选课数,总成绩

数据库查询所有同学的学号,姓名,选课数,总成绩

现在存在如下表关系

班级表:class

cid

caption

1

三年二班

2

一年三班

3

三年一班

学生表:student

Sid

Sname

Gender

Class_id

1

钢蛋

1

2

铁锤

1

3

山炮

2

教师表:teacher

tid

tname

1

叶平

2

李大圣

3

叶问

课程表:course

Cid

Cname

Teacher_id

1

生物

1

2

体育

1

3

物理

2

成绩表:score

Sid

Student_id

Corse_id

number

1

1

1

60

2

1

2

59

3

2

2

100

1、自行创建测试数据

2、查询“生物”课程比“体育”课程成绩高的所有学生的学号。ps:针对的是自己的生物成绩比物理成绩高,再把符合条件的学生的学号查出来;

select sc1.Student_id 学号 , sc1.Corse_id 生物, sc1.number,sc2.Corse_id 体育,sc2.number from score sc1 left join score sc2 on sc1.Student_id=sc2.Student_id where sc1.Corse_id=(select cid from course where cname='生物') and sc2.Corse_id=(select cid from course where cname='体育') having sc1.number>sc2.number;

3、查询平均成绩大于60分的同学的学号和平均成绩; 

select Student_id, avg(number) from score group by Student_id;

4、查询所有同学的学号、姓名、选课数、总成绩;

select s.sid,s.sname,count(sc.corse_id) 选课数,sum(sc.number) 总成绩 from student s, score sc where s.sid=sc.Student_id group by Student_id;

5、查询姓“李”的老师的个数;

select count(*) from teacher where tname like'%李%';

6、查询没学过“叶平”老师课的同学的学号、姓名;

select sid,sname from student where sid not in (select distinct Student_id from score where Corse_id in (select cid from course where tid=(select tid from teacher where tname='叶平'))); 

 7、查询学过“001”并且也学过编号“002”课程的同学的学号、姓名;

select sid,sname from student where sid in (select distinct  sc1.Student_id from score sc1, score sc2 where sc1.Corse_id=1 and sc2.Corse_id=2);

8、查询学过“叶平”老师所教的所有课的同学的学号、姓名;

select sid,sname from student where sid in (select distinct Student_id from score where Corse_id in (select cid from course where tid=(select tid from teacher where tname='叶平')));

9、查询课程编号“002”的成绩比课程编号“001”课程低的所有同学的学号、姓名;

select sid,sname from student where sid in (select sc1.Student_id  from score sc1, score sc2 where sc1.Student_id=sc2.Student_id and sc1.Corse_id=1 and sc2.Corse_id=2 and sc1.number>sc2.number);

10、查询有课程成绩小于60分的同学的学号、姓名;

select sid,sname from student where sid in (select Student_id from score where number<60);

11、查询没有学全所有课的同学的学号、姓名;

select sid,sname from student where sid not in (select Student_id from score group by Student_id having count(Corse_id)=3);

12、查询至少有一门课与学号为“001”的同学所学相同的同学的学号和姓名;

select sid, sname from student where sid in (select distinct Student_id from score where Corse_id in (select Corse_id from score where Student_id=1))

13、查询至少学过学号为“001”同学所选课程中任意一门课的其他同学学号和姓名;

select sid, sname from student where sid in (select distinct Student_id from score where Corse_id in (select Corse_id from score where Student_id=1) having Student_id<>1);

14、查询和“002”号的同学学习的课程完全相同的其他同学学号和姓名;

select sid,sname from student where sid in (select s.Student_id from (select sc1.Student_id from score sc1 join score sc2 on sc1.Corse_id=sc2.Corse_id where sc1.Student_id<>2 and sc2.Student_id=2) s  group by s.Student_id having count(*)=(select count(*) from score where Student_id=2));

 

15、删除学习“叶问”老师课的SC表记录;

delete from score where Corse_id=(select cid from course where tid=(select tid from teacher where tname='叶问'));

删除前:

删除后:

16、向SC表中插入一些记录,这些记录要求符合以下条件:①没有上过编号“002”课程的同学学号;②插入“002”号课程的平均成绩; 

insert into score (Student_id,Corse_id,number) select sid, 2, (select avg(number) from score where Corse_id=2) from student where sid not in (select Student_id from score where Corse_id=2);

17、按平均成绩从低到高显示所有学生的“生物”、“体育”、“物理”三门的课程成绩,按如下形式显示: 学生ID,生物,体育,物理,有效课程数,有效平均分;

select  sc1.Student_id,sc1.number 生物,sc2.number 体育,sc3.number 物理,(sc1.number+sc2.number+sc3.number)/3 平均成绩  from score sc1, score sc2, score sc3 where sc1.Student_id=sc2.Student_id and sc2.Student_id =sc3.Student_id and sc1.Corse_id=1 and sc2.Corse_id=2 and sc3.Corse_id=3 group by sc1. Student_id order by 平均成绩 asc;

18、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分;

select Corse_id,max(number),min(number) from score group by Corse_id;

19、按各科平均成绩从低到高和及格率的百分数从高到低顺序;

select Corse_id,avg(number) 平均成绩, 100*sum(case when (number)>=60 then 1 else 0 end)/count(*) 及格率  from score group by Corse_id order by 平均成绩 asc, 及格率 desc;

 20、查询出只选修了一门课程的全部学生的学号和姓名; 

select sid,sname from student where sid in (select Student_id from score group by Student_id having count(*)=1);

所有操作完成后的表:

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/439283
推荐阅读
相关标签
  

闽ICP备14008679号