MySQL学生表、老师表、课程表和成绩表查询语句,全部亲测

建表可自行百度,重点为查询;

一、数据准备:

1.student学生表数据:

MySQL学生表、老师表、课程表和成绩表查询语句,全部亲测_第1张图片

2.teacher老师表数据:

3.course课程表数据:

4.score成绩表数据:

MySQL学生表、老师表、课程表和成绩表查询语句,全部亲测_第2张图片

二、数据查询

1.查询平均成绩大于60分的同学的学生编号和学生姓名和平均成绩:

语句:select st.StuId,st.StuName as st_name,avg(sc.StuScore) from score as sc,student as st where st.StuId = sc.StuId group by st.StuId having avg(sc.StuScore) > 60;

结果:MySQL学生表、老师表、课程表和成绩表查询语句,全部亲测_第3张图片

2.查询所有学生的学号、姓名、选课数、总成绩

语句:select st.StuId,st.StuName,count(sc.StuId),sum(sc.Stuscore) from student as st,score as sc  where st.StuId = sc.StuId group by sc.StuId;

结果:MySQL学生表、老师表、课程表和成绩表查询语句,全部亲测_第4张图片

3.查询姓“张”的老师的个数

语句:select count(TeaId) from Teacher where TeaName like '张%';

结果:

4.查询学过“张三”老师所教的所有课的同学的学号、姓名

语句:select Student.StuId,Student.StuName from student where StuId in (select score.StuId from score,course,teacher where score.CourseId = course.CourseId and course.TeaId = teacher.TeaId and Teacher.TeaName = '张三');

结果:MySQL学生表、老师表、课程表和成绩表查询语句,全部亲测_第5张图片

5.查询没学过"张三"老师授课的学生的学号、姓名

语句:select Student.StuId,Student.StuName from student where StuId not in (select score.StuId from score,course,teacher where score.CourseId = course.CourseId and course.TeaId = teacher.TeaId and Teacher.TeaName = '张三');

结果:

6.查询学过编号为“0001”的课程并且也学过编号为“0002”的课程的学生的学号、姓名

语句:select Student.StuId,Student.StuName from Student,Score where Student.StuId = Score.StuId and Score.CourseId = '0001' and exists (select * from score as sc where sc.StuId = Score.StuId and sc.CourseId = '0002');

结果:MySQL学生表、老师表、课程表和成绩表查询语句,全部亲测_第6张图片

7.查询课程编号为“0002”的总成绩

语句:select sum(StuScore) from score where CourseId = '0002';

结果:

8.查询所有课程成绩小于等于60分的学生的学号、姓名

语句:select Student.StuId,Student.StuName from student where Student.StuId not in (select Student.StuId from student,score where Student.StuId = Score.StuId and StuScore > 60);

结果:MySQL学生表、老师表、课程表和成绩表查询语句,全部亲测_第7张图片

9.查询没有学全所有课的学生的学号、姓名

语句:select Student.StuId,Student.StuName from student,score where Student.StuId = Score.StuId group by Student.StuId,Student.StuName having count(courseId) < (select count(courseId) from course);

结果:MySQL学生表、老师表、课程表和成绩表查询语句,全部亲测_第8张图片

10.查询至少有一门课与学号为“0001”的学生所学课程相同的学生的学号和姓名

语句:select Student.StuId,Student.StuName from student,score where Student.StuId = Score.StuId and CourseId in (select courseId from score where StuId = '0001');

结果:MySQL学生表、老师表、课程表和成绩表查询语句,全部亲测_第9张图片

11.查询和“0001”号同学所学课程完全相同的其他同学的学号

语句:select StuId from score where courseId in (select courseId from score where StuId = '0001') group by StuId having count(*) = (select count(*) from score where StuId = '0001');

结果:

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

语句:select CourseId as 课程ID,max(StuScore) as 最高分,min(StuScore) as 最低分 from score group by  courseId;

结果:MySQL学生表、老师表、课程表和成绩表查询语句,全部亲测_第10张图片

13.按各科及格率的百分数从高到低排列,以如下形式显示:课程号课程名平均成绩及格百分数

语句:select Course.CourseId as 课程号,Course.CourseName as 课程名,avg(Score.StuScore) as 平均成绩,100 * sum(case when ifnull(Score.StuScore,0)>=60 then 1 else 0 end)/count(*) as 及格百分数 from course,score where Score.CourseId = Course.CourseId group by Score.CourseId order by 及格百分数 desc;

结果:MySQL学生表、老师表、课程表和成绩表查询语句,全部亲测_第11张图片

14.查询不同老师所教不同课程平均分从高到低显示

语句:select Teacher.teaId as 教师ID,Teacher.teaName as 教师姓名,Course.courseId as 课程ID,avg(Score.StuScore) as 平均 成绩 from score,teacher,course where score.courseId = Course.courseId and Course.teaId = Teacher.teaId group by Score.courseId order by avg(Score.StuScore) desc;

结果:MySQL学生表、老师表、课程表和成绩表查询语句,全部亲测_第12张图片

15.使用分段[100-85],[85-70],[70-60],[<60]来统计各科成绩,分别统计各分数段人数:课程ID和课程名称

语句:select Score.courseId as 课程ID,courseName as 课程名称,sum(case when stuScore between 85 and 100 then 1 else 0 end) as '[100-85]',sum(case when stuScore between 70 and 85 then 1 else 0 end) as '[85-70]',sum(case when stuScore between 60 and 70 then 1 else 0 end) as '[70-60]',sum(case when stuScore < 60 then 1 else 0 end) as '[<60]' from Score,Course where Score.courseId = Course.courseId group by Score.courseId;

结果:MySQL学生表、老师表、课程表和成绩表查询语句,全部亲测_第13张图片

16.查询学生平均成绩及其名次

语句:select 1+(select count(distinct 平均成绩) from (select stuId,avg(stuScore) as 平均成绩 from Score group by stuId) as T1 where 平均成绩 > T2.平均成绩) as 名次,stuId as 学生学号,平均成绩 from (select stuId,avg(stuScore) 平均成绩 from Score group by stuId) as T2 order by 平均成绩 desc;

结果:MySQL学生表、老师表、课程表和成绩表查询语句,全部亲测_第14张图片

17.查询每门课程被选修的学生数

语句:select Course.courseName,count(Score.courseId) from Score,Course where Score.courseId = Course.courseId group by Score.courseId;

结果:MySQL学生表、老师表、课程表和成绩表查询语句,全部亲测_第15张图片

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

语句:select Student.stuId,Student.stuName from Student,Score where Score.stuId = Student.stuId group by Score.stuId having count(Score.courseId) = 1;

结果:

19.查询男生、女生人数

语句:select count(stuSex) from student where stuSex = '男';select count(stuSex) from student where stuSex = '女';

结果:

20.查询姓“李”的学生名单

语句:select stuName from student where stuName like '李%';

结果:

21.查询同名同姓学生名单并统计同名人数

语句:select stuName,count(*) from student group by stuName having count(*) > 1;

结果:

22.查询平均成绩大于85的所有学生的学号、姓名和平均成绩

语句:select Student.stuId,Student.stuName,avg(Score.stuScore) from Student,Score where Student.stuId = Score.stuId group by Student.stuId having avg(Score.stuScore) > 85;

结果:

23.查询每门课程的平均成绩,结果按平均成绩升序排序,平均成绩相同时,按课程号降序排列

语句:select courseId,avg(stuScore) from score group by courseId order by avg(stuScore),courseId desc;

结果:MySQL学生表、老师表、课程表和成绩表查询语句,全部亲测_第16张图片

24.查询课程名称为“语文”且分数低于60的学生姓名和分数

语句:select Student.stuName,Score.stuScore from Student,Course,Score where Course.courseName = '语文' and Course.courseId = Score.courseId and Score.stuId = Student.stuId and Score.stuScore < 60;

结果:

25.查询所有学生的选课情况

语句:select Score.stuId,Score.courseId,Student.stuName,Course.courseName from Score,Student,Course where Score.stuId = Student.stuId and Score.courseId = Course.courseId;

结果:MySQL学生表、老师表、课程表和成绩表查询语句,全部亲测_第17张图片

26.查询任何一门课程成绩在70分以上的学生学号、姓名、课程号和分数

语句:select distinct Student.stuId,Student.stuName,Score.courseId,Score.stuScore from Student,Score where Score.stuScore>=70 and Score.stuId = Student.stuId;

结果:MySQL学生表、老师表、课程表和成绩表查询语句,全部亲测_第18张图片

27.查询不及格的课程并按课程号从大到小排列

语句:select distinct courseId from Score where stuScore<60 order by courseId desc;

结果:

28.查询课程编号为0003且课程成绩在80分以上的学生的学号和姓名

语句:select Student.stuId,stuName from Student,Score where Student.stuId = Score.stuId and courseId = '0003' and stuScore >80;

结果:

29.查询选修“张三”老师所授课程的学生中成绩最高的学生姓名及其成绩

语句:select Student.stuName,Score.stuScore from Student,Score,Course,Teacher where Teacher.teaName = '张三' and Teacher.teaId = Course.teaId and Course.courseId = Score.courseId and Score.stuId = Student.stuId and Score.stuScore = (select max(stuScore) from score where courseId = Course.courseId);

结果:

30.查询各个课程及相应的选修人数

语句:select courseId,count(*) from score group by courseId;

结果:MySQL学生表、老师表、课程表和成绩表查询语句,全部亲测_第19张图片

31.查询有2门不同课程成绩相同的学生的学号、学生成绩

解决思路: 将score进行裂变a,b项后,进行成绩相同但是学号不同的并列项进行判断,得出结果

语句:select  distinct a.stuId,b.stuScore from Score a,Score b where a.stuScore = b.stuScore and a.courseId<>b.courseId;

结果:MySQL学生表、老师表、课程表和成绩表查询语句,全部亲测_第20张图片

32.统计每门课程的学生选修人数,要求输出课程号和选修人数,查询结果按人数降序排序,若人数相同,按课程号降序排序

语句:select courseId,count(*) from score group by courseId order by count(*) desc,courseId desc;

结果:MySQL学生表、老师表、课程表和成绩表查询语句,全部亲测_第21张图片

33.查询至少选修两门课程的学生学号

语句: select stuId from Score group by stuId having count(courseId) >=2;

结果:

34.查询全部学生都选修的课程的课程号和课程名

语句:select courseId,courseName from Course where courseId in (select courseId from score group by courseId);

结果:MySQL学生表、老师表、课程表和成绩表查询语句,全部亲测_第22张图片

35.查询没学过“张三”老师讲授的任一门课程的学生姓名

语句:select stuName from Student where stuId not in(select stuId from Course,Teacher,Score where Teacher.teaName = '张三' and Teacher.teaId = Course.teaId and Course.courseId = Score.courseId);

结果:

36.查询两门以上不及格课程的同学的学号及其平均成绩

语句:select stuId,avg(ifnull(stuScore,0)) from score where stuId in (select stuId from score where stuScore <60 group by stuId having count(*) >2) group by stuId;

结果:

37.检索课程编号为“0003”且分数小于60的学生学号,结果按分数降序排列

语句:select stuId from score where courseId = '0003' and stuScore < 60 order by stuScore desc;

结果:

38.查询"0001"课程比"0002"课程成绩高的学生的信息及课程分数

语句:select A.stuId from (select stuId,stuScore from Score where courseId = '0001')A,(select stuId,stuScore from Score where courseId = '0002')B where A.stuScore > B.stuScore and A.stuId = B.stuId;

结果:

你可能感兴趣的:(Mysql数据库)