数据表

  1. # 学生表
  2. create table t_student
  3. (
  4. stu_id varchar(10),
  5. stu_name varchar(10),
  6. stu_age datetime,
  7. stu_sex varchar(10)
  8. );
  9. # 课程表
  10. create table t_course
  11. (
  12. c_id varchar(10),
  13. c_name varchar(10),
  14. c_teaid varchar(10)
  15. );
  16. # 教师表
  17. create table t_teacher
  18. (
  19. tea_id varchar(10),
  20. tea_name varchar(10)
  21. );
  22. # 成绩表
  23. create table t_score
  24. (
  25. s_stuid varchar(10),
  26. s_cid varchar(10),
  27. s_score decimal(18,1)
  28. );

测试数据

-- 向t_student表插入数据
insert into t_student(stu_id,stu_name,stu_age,stu_sex) values('01','赵雷','1990-01-01' ,'男');
insert into t_student(stu_id,stu_name,stu_age,stu_sex) values('02' , '钱电' , '1990-12-21' , '男');
insert into t_student(stu_id,stu_name,stu_age,stu_sex) values('03' , '孙风' , '1990-12-20' , '男');
insert into t_student(stu_id,stu_name,stu_age,stu_sex) values('04' , '李云' , '1990-12-06' , '男');
insert into t_student(stu_id,stu_name,stu_age,stu_sex) values('05' , '周梅' , '1991-12-01' , '女');
insert into t_student(stu_id,stu_name,stu_age,stu_sex) values('06' , '吴兰' , '1992-01-01' , '女');
insert into t_student(stu_id,stu_name,stu_age,stu_sex) values('07' , '郑竹' , '1989-01-01' , '女');
insert into t_student(stu_id,stu_name,stu_age,stu_sex) values('09' , '张三' , '2017-12-20' , '女');
insert into t_student(stu_id,stu_name,stu_age,stu_sex) values('10' , '李四' , '2017-12-25' , '女');
insert into t_student(stu_id,stu_name,stu_age,stu_sex) values('11' , '李四' , '2012-06-06' , '女');
insert into t_student(stu_id,stu_name,stu_age,stu_sex) values('12' , '赵六' , '2013-06-13' , '女');
insert into t_student(stu_id,stu_name,stu_age,stu_sex) values('13' , '孙七' , '2014-06-01' , '女');

-- 向t_course表插入数据
insert into course(c_id , c_name , c_teaid) values('01' , '语文' , '02');
insert into course(c_id , c_name , c_teaid) values('02' , '数学' , '01');
insert into course(c_id , c_name , c_teaid) values('03' , '英语' , '03');

-- 向t_teacher表插入数据
insert into teacher(tea_id , tea_name) values('01' , '张三');
insert into teacher(tea_id , tea_name) values('02' , '李四');
insert into teacher(tea_id , tea_name) values('03' , '王五');

-- 向t_score表插入数据
insert into score(s_stuid,s_cid,s_score) values('01' , '01' , 80);
insert into score(s_stuid,s_cid,s_score) values('01' , '02' , 90);
insert into score(s_stuid,s_cid,s_score) values('01' , '03' , 99);
insert into score(s_stuid,s_cid,s_score) values('02' , '01' , 70);
insert into score(s_stuid,s_cid,s_score) values('02' , '02' , 60);
insert into score(s_stuid,s_cid,s_score) values('02' , '03' , 80);
insert into score(s_stuid,s_cid,s_score) values('03' , '01' , 80);
insert into score(s_stuid,s_cid,s_score) values('03' , '02' , 80);
insert into score(s_stuid,s_cid,s_score) values('03' , '03' , 80);
insert into score(s_stuid,s_cid,s_score) values('04' , '01' , 50);
insert into score(s_stuid,s_cid,s_score) values('04' , '02' , 30);
insert into score(s_stuid,s_cid,s_score) values('04' , '03' , 20);
insert into score(s_stuid,s_cid,s_score) values('05' , '01' , 76);
insert into score(s_stuid,s_cid,s_score) values('05' , '02' , 87);
insert into score(s_stuid,s_cid,s_score) values('06' , '01' , 31);
insert into score(s_stuid,s_cid,s_score) values('06' , '03' , 34);
insert into score(s_stuid,s_cid,s_score) values('07' , '02' , 89);
insert into score(s_stuid,s_cid,s_score) values('07' , '03' , 98);

关系图

image.png

SQL题总

题1

查询同名学生名单,并统计同名人数,找到同名的名字并统计个数

select stu_name ,count(*) from t_student group by stu_name having count(*)>1

having 其实和where一样都是过虑的作用,只是顺序不同,在有分组的时候(group by ),where 是先过虑再分组计算,having是先分组计算再过虑。

题2

查询列出同名的全部学生的信息

select stu_id,stu_name,stu_age,stu_sex from t_student 
where stu_name in(
  select stu_name from t_student group by stu_name having count(*)>1
)

题3

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

select sc.s_cid ,c.c_name , AVG(sc.s_score) as average
from t_score as sc ,t_course as c 
where sc.s_cid =c.c_id
group by sc.s_cid
order by average desc , s_cid asc;

题4

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

select stu.stu_id ,stu.stu_name ,AVG(sc.s_score) as average
from t_student as stu ,t_score as sc
where stu.stu_id = sc.s_stuid
group by sc.s_stuid
having average >85;

题5

查询课程名称为”数学”,且分数低于 60 的学生姓名和分数

select stu.stu_name ,sc.s_score
from t_student as stu,t_score as sc,t_course as c
where stu.stu_id =sc.s_stuid and c.c_id =sc.s_cid
and c.c_name ="数学" and sc.s_score <60;

题6

查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数

select stu.stu_name ,c.c_name ,sc.s_score
from t_student as stu ,t_score as sc, t_score as c
where stu.stu_id=sc.s_stuid and sc.s_cid = c.c_id
and sc.s_score >70;

题7

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

select stu.stu_id ,stu.stu_name 
from t_student as stu and t_score as sc
where stu.stu_id = sc.s_stuid
and sc.s_cid = "01"and sc.s_score >= 80;

题8

查询选修”张三”老师所授课程的学生中,成绩最高的学生信息及其成绩

select stu.*,sc.s_score,sc.s_cid
from  t_student as stu, t_teacher as tea, t_course as c,t_score as sc 
where stu.stu_id =sc.s_stuid and sc.s_cid =c.c_id and c.c_teaid=tea.tea_id
and tea.tea_name="张三" having max(sc.s_score);

题9

查询选修”张三”老师所授课程的学生中,成绩最高的学生信息及其成绩

select stu.*, sc.s_score, sc.s_cid
from  t_student as stu, t_teacher as tea, t_course as c,t_score as sc 
where stu.stu_id =sc.s_stuid and sc.s_cid =c.c_id and c.c_teaid=tea.tea_id
and tea.tea_name = "张三"
      and sc.s_score =(
          select max(t_score.s_score) 
          from t_score,t_student, t_teacher, t_course
          where t_teacher.tea_id = t_course.c_teaid
          and t_score.s_stuid = t_student.stu_id
          and t_score.s_cid = t_course.c_id
          and t_teacher.tea_name = "张三"
      );

题8和题9本来就一样,非得弄的这么复杂,无语子了。

题10

查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩

select sc1.s_stuid ,sc1.s_cid ,sc1.s_score
from t_score as sc1 inner join t_score as sc2
on sc1.s_stuid =sc2.s_stuid
and sc1.s_cid != sc2.s_cid and sc1.s_score =sc2.s_score
group by s_cid,s_stuid;

题11

查询每门功成绩最好的前两名

select sc1.s_stuid, sc1.s_cid, sc1.s_score
from t_score as sc1 left join t_score as sc2 
on sc1.s_cid = sc2.s_cid and sc1.s_score < sc2.s_score
group by sc1.s_cid, sc1.s_stuid
having count(sc2.s_cid) < 2
order by sc1.s_cid;

left join和inner join的区别在于返回不同、数量不同、记录属性不同。inner join:inner join只返回两个表中联结字段相等的行;left join:left join返回包括左表中的所有记录和右表中联结字段相等的记录。

题12

查询选修了全部课程的学生信息

select t_student.*
from t_score ,t_student
where t_score.s_stuid = t_student.stu_id
group by t_score.s_stuid
having count(*) =(select distinct count(*)from t_course)

distinct 是去重的意思

题13

查询本周过生日的学生

select * 
from t_student
where WEEKOFYEAR(t_student.stu_age) = WEEKOFYEAR (curdate());

weekofyear函数是计算出当前日期所在周数,和YEARWEEK(‘日期’,1)的周数一致,但YEARWEEK(‘日期’,1)在小于10的时候,不带0

题14

查询下月过生日的学生

-- DATE_ADD(CURDATE(),INTERVAL 1 month)  #在当前时间的基础上取下个月的时间
select *
from t_student 
where MONTH(t_student.stu_age) = MONTH(DATE_ADD(curdate(),interval 1 month));

题15

查询出只选修两门课程的学生学号和姓名

select stu.stu_id ,stu.stu_name
from t_score as sc, t_student as stu
where stu.stu_id = sc.s_stuid
group by sc.s_stuid
having count(sc.s_stuid)=2;

题16

查询”01”课程比”02”课程成绩高的学生的信息及课程分数

题17

查询学过编号为”02”但没有学过编号为”01”课程的学生信息和相关课程成绩

题18

查询同时选修了”01”课程和”02”课程的学生信息及相关课程成绩

题19

查询选修了”01”课程但可能没有选修”02”课程的学生信息及相关课程成绩(不存在时显示为0)

题20

查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩 (成绩保留2位小数)

题21

查询所有同学的学生编号、学生姓名、选课总数、所有课程的成绩总和 (没有选课的学生显示为0)

题22

查询学过「张三」老师授课的同学的信息

题23

查询没有学全所有课程的同学的信息

题24

查询至少有一门课与学号为”01”的同学所学相同的同学的信息 (不含学号为”01”的学生)

题25

查询和学号”01”同学学习的课程完全相同的其他同学的信息

题26

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

题27

查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 (成绩保留两位小数)

题28

检索”01”课程分数小于 60,按分数降序排列的学生信息

题29

按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 (平均成绩保留两位小数)

题30

查询各科成绩最高分、最低分、平均分、选修人数、及格率 (及格率以百分比格式显示)

题31

按各科成绩进行排序,并显示排名, 成绩重复时不保留名次空缺

题33

查询学生的总成绩,并按照总成绩依次进行排名(不保留名次空缺)

题34

统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比

题35

查询各科成绩前三名的记录

题36

查询每个同学的学号、姓名、选课总数、总成绩

题37

查询编号是2的课程比编号是1的课程最高成绩高的学生姓名和成绩