mysql学生表经典案例50题

一、建库、建表

create database alibaba;

use alibaba;
– 建表
create table student(s_id varchar(10),s_name varchar(10),s_age date,s_sex varchar(10)) engine=InnoDB default charset utf8;
insert into student(s_id,s_name,s_age,s_sex)
values('01' , '赵雷', '1990-01-01' , '男'),
('02' , '钱电' , '1990-12-21' , '男')
,('03' , '孙风' , '1990-05-20' , '男')
,('04' , '李云' , '1990-08-06' , '男')
,('05' , '周梅' , '1991-12-01' , '女')
,('06' , '吴兰' , '1992-03-01' , '女')
,('07' , '郑竹' , '1989-07-01' , '女')
,('08' , '王菊' , '1990-01-20' , '女');
select * from student;

create table course(c_id varchar(10),c_name varchar(10),t_id varchar(10)) engine=InnoDB default charset utf8;
insert into course values('01' , '语文' , '02'),
('02' , '数学' , '01'),
('03' , '英语' , '03');
select * from course;

create table teacher(t_id varchar(10),t_name varchar(10)) engine=InnoDB default charset utf8;
insert into teacher values('01' , '张三'),('02' , '李四'),('03' , '王五');
select * from teacher;

create table stu_sco(s_id varchar(10),c_id varchar(10),score decimal(18,1)) engine=InnoDB default charset utf8;
insert into stu_sco values ('01' , '01' , 80), ('01' , '02' , 90), ('01' , '03' , 99), ('02' , '01' , 70), ('02' , '02' , 60), ('02' , '03' , 80), ('03' , '01' , 80), ('03' , '02' , 80), ('03' , '03' , 80), ('04' , '01' , 50), ('04' , '02' , 30), ('04' , '03' , 20), ('05' , '01' , 76), ('05' , '02' , 87), ('06' , '01' , 31), ('06' , '03' , 34), ('07' , '02' , 89), ('07' , '03' , 98);
select * from stu_sco;

二、完成如下查询语句

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

select a.*,b.score as sc1,c.score as sc2 from student a join stu_sco b on a.s_id =b.s_id and b.c_id =01 join stu_sco c on a.s_id=c.s_id and c.c_id =02 where b.score>c.score;

2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数

select a.*,b.score as sc1,c.score as sc2 from student a join stu_sco b on a.s_id =b.s_id and b.c_id =01 join stu_sco c on a.s_id=c.s_id and c.c_id =02 where b.score

3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩

SELECT
student.s_id 学生编号,
student.s_name 学生姓名,
avg(score) 平均成绩
FROM
student
JOIN stu_sco ON student.s_id = stu_sco.s_id
GROUP BY
student.s_id,
student.s_name
HAVING
平均成绩 >= 60;

SELECT
student.s_id,
student.s_name,
avg_score
FROM
student
INNER JOIN (
SELECT
s_id,
avg(score) avg_score
FROM
stu_sco
GROUP BY
s_id
) b ON student.s_id = b.s_id
AND b.avg_score >= 60;

4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩

SELECT
student.s_id,
student.s_name,
avg_score
FROM
student
INNER JOIN (
SELECT
s_id,
avg(score) avg_score
FROM
stu_sco
GROUP BY
s_id
) b ON student.s_id = b.s_id
AND b.avg_score <60;

SELECT
student.s_id 学生编号,
student.s_name 学生姓名,
avg(score) 平均成绩
FROM
student
JOIN stu_sco ON student.s_id = stu_sco.s_id
GROUP BY
student.s_id,
student.s_name
HAVING
平均成绩 <60;

5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩

SELECT
student.s_id 学生编号,
student.s_name 学生姓名,
count(c_id) 选课总数,
sum(score) 总成绩
FROM
student
JOIN stu_sco ON student.s_id = stu_sco.s_id
GROUP BY student.s_id;

6、查询"李"姓老师的数量

SELECT
count(*) 李姓老师的数量
FROM
teacher
WHERE t_name LIKE '李%';

7、查询学过"张三"老师授课的同学的信息

SELECT *
FROM
student
WHERE
s_id IN (
SELECT
s_id
FROM
stu_sco
WHERE
c_id IN (
SELECT
c_id
FROM
course
WHERE
c_id IN (
SELECT
t_id
FROM
teacher
WHERE
t_name = '张三' ) ) );

8、查询没学过"张三"老师授课的同学的信息

SELECT *
FROM
student
WHERE
s_id NOT IN (
SELECT
s_id
FROM
stu_sco
WHERE
c_id IN (
SELECT
c_id
FROM
course
WHERE
c_id IN (
SELECT
t_id
FROM
teacher
WHERE
t_name = '张三'
)
)
);

9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息

SELECT
*
FROM
student
WHERE
s_id IN (
SELECT
a.s_id
FROM
(
SELECT
*
FROM
stu_sco
WHERE
c_id = '01'
) a
JOIN (
SELECT
*
FROM
stu_sco
WHERE
c_id = '02'
) b ON a.s_id = b.s_id
);

10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息

SELECT
student.*
FROM
student,
stu_sco sc1
WHERE
student.s_id = sc1.s_id
AND sc1.c_id = '01'
AND student.s_id NOT IN (
SELECT
s_id
FROM
stu_sco sc2
WHERE
sc2.c_id = '02'
);

11、查询没有学全所有课程的学生信息

SELECT
student.*
FROM
student
JOIN stu_sco ON student.s_id = stu_sco.s_id
GROUP BY
student.s_id
HAVING
count(stu_sco.c_id) < (
SELECT
count(DISTINCT c_id)
FROM
course
);

12、查询至少有一门课与学号为"01"的同学所学相同的同学的信息

SELECT
student.*
FROM
student
WHERE
s_id IN (
SELECT DISTINCT
(s_id)
FROM
stu_sco
WHERE
c_id IN (
SELECT
c_id
FROM
stu_sco
WHERE
s_id = '01'
)
AND s_id <> '01'
);

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

SELECT
student.*
FROM
student
WHERE
s_id IN (
SELECT
s_id
FROM
stu_sco
WHERE
s_id <> '01'
GROUP BY
s_id
HAVING
count(1) = (
SELECT
count(c_id)
FROM
stu_sco
WHERE
s_id = '01'
)
);

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

SELECT
*
FROM
student
WHERE
s_id NOT IN (
SELECT
s_id
FROM
stu_sco
WHERE
c_id IN (
SELECT
c_id
FROM
course
LEFT JOIN teacher ON course.t_id = teacher.t_id
WHERE
t_name = '张三'
)
);

15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩

select s_id from stu_sco where score<60 GROUP BY s_id HAVING count(s_id)>1;
SELECT
student.s_id 学号,
student.s_name,
AVG(score)
FROM
student
JOIN stu_sco ON student.s_id = stu_sco.s_id
WHERE
student.s_id IN (
SELECT
s_id
FROM
stu_sco
WHERE
score < 60
GROUP BY
s_id
HAVING
count(s_id) > 1
)
GROUP BY
student.s_id;

select a.s_id,b.s_name,avg(c.score) as '平均成绩' from
(select s_id from stu_sco where score<60 group by s_id having count(s_id)>=2)a
left join student b on a.s_id=b.s_id
left join stu_sco c on b.s_id=c.s_id
group by a.s_id;

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

SELECT
student.*, a.score
FROM
student
JOIN (
SELECT
s_id,
score
FROM
stu_sco
WHERE
c_id = 01
AND score < 60
) a ON student.s_id = a.s_id
ORDER BY
a.score DESC;

17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩

SELECT
stu_sco.*,
平均成绩
FROM
stu_sco
JOIN (
SELECT
s_id,
AVG(score) 平均成绩
FROM
stu_sco
GROUP BY
s_id
) a ON stu_sco.s_id = a.s_id
ORDER BY
平均成绩 DESC,
score DESC;

18、查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率

SELECT
a.c_id 课程ID,
c_name 课程名,
max(score) 最高分,
min(score) 最低分,
avg(score) 平均分,
avg(pass) 及格率,
avg(middle) 中等率,
AVG(good) 优良率,
AVG(excellent) 优秀率
FROM
(SELECT
stu_sco.*, (CASE WHEN score >= 60 THEN 1 ELSE 0 END ) pass,
( CASE WHEN score >= 70 AND score < 79 THEN 1 ELSE 0 END) middle,
( CASE WHEN score >= 80 AND score < 89 THEN 1 ELSE 0 END) good,
( CASE WHEN score >= 90 THEN 1 ELSE 0 END) excellent
FROM
stu_sco) a
JOIN course ON a.c_id = course.c_id
GROUP BY
a.c_id;

19、查询学生的总成绩并进行排名

SELECT
a.*,@rank :=@rank + 1 AS rank
FROM
(
SELECT
s_id,
sum(score) 总成绩
FROM
stu_sco
GROUP BY
s_id
ORDER BY
总成绩 DESC
) a,
(SELECT @rank := 0 AS rank) b;

20、按课程分类,用成绩对学生进行排名,排名升序

select s_id, c_id ,score from stu_sco ORDER BY c_id ASC,score desc;

select d.s_name,d.score,if(@c_name=d.c_name,@rank:=@rank+1,@rank:=1) as rank, @c_name:=d.c_name as c_name
from
(select a.s_name,c.c_name,b.score
from student a left join stu_sco b on a.s_id=b.s_id
left join course c on c.c_id=b.c_id
order by c.c_name desc,b.score desc)d,
(select @c_name:=0,@rank:=0)e;

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

SELECT
t_name 老师,
c_name 课程,
avg(score) 平均分
FROM
teacher
LEFT JOIN course ON course.t_id = teacher.t_id
JOIN stu_sco ON course.c_id = stu_sco.c_id
GROUP BY
teacher.t_id,
stu_sco.c_id
ORDER BY
平均分 DESC;

22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩

select d.*,c.c_id,c.score from (
select a.s_id,a.c_id,a.score from stu_sco a where (select count(b.s_id) from stu_sco b where a.c_id=b.c_id and a.score< b.score)
between 1 and 2 group by a.c_id,a.s_id) c left join
student d on c.s_id=d.s_id ORDER BY c.c_id;

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

SELECT stu_sco.c_id 课程编号,c_name 课程名称
,avg(CASE WHEN(score>=0 and score<=60)THEN 1 else 0 END) '0-60占比',
avg(CASE WHEN(score>=60 and score<=70)THEN 1 else 0 END) '60-70占比',
avg(CASE WHEN(score>=70 and score<=85)THEN 1 else 0 END) '70-85占比',
avg(CASE WHEN(score>=85 )THEN 1 else 0 END) '85-100占比'
from stu_sco LEFT JOIN course on stu_sco.c_id=course.c_id GROUP BY stu_sco.c_id;

SELECT stu_sco.c_id 课程编号,c_name 课程名称
,avg(CASE WHEN(score BETWEEN 0 and 60)THEN 1 else 0 END) '0-60占比',
avg(CASE WHEN(score BETWEEN 60 and 70)THEN 1 else 0 END) '60-70占比',
avg(CASE WHEN(score BETWEEN 70 and 85)THEN 1 else 0 END) '70-85占比',
avg(CASE WHEN(score BETWEEN 85 and 100 )THEN 1 else 0 END) '85-100占比'
from stu_sco LEFT JOIN course on stu_sco.c_id=course.c_id GROUP BY stu_sco.c_id;

select a.c_id 课程编号,b.c_name 课程名称,
sum(case when score between 0 and 60 then 1 else 0 end)/count(a.c_id) as '[0-60]占比',
sum(case when score between 60 and 70 then 1 else 0 end)/count(a.c_id) as '[60-70]占比',
sum(case when score between 70 and 85 then 1 else 0 end)/count(a.c_id) as '[70-85]占比',
sum(case when score between 85 and 100 then 1 else 0 end)/count(a.c_id) as '[85-100]占比'
from stu_sco a left join course b on a.c_id=b.c_id
group by a.c_id;

24、查询每个学生平均成绩及其名次

select s_id 学号,平均成绩,@rank:=@rank+1 as 名次 from (select s_id,avg(score) 平均成绩 from stu_sco GROUP BY s_id ORDER BY 平均成绩 DESC) a,(select @rank:=0)b;

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

SELECT
*
FROM
stu_sco a
WHERE
(
SELECT
count(b.s_id)
FROM
stu_sco b
WHERE
a.c_id = b.c_id
AND a.score < b.score
) < 3
ORDER BY
c_id,
score DESC;

26、查询每门课程被选修的学生数

select c_id 课程ID,count(s_id) 选修人数 from stu_sco GROUP BY c_id;

27、查询出只有两门课程成绩的全部学生的学号和姓名

select s_id,s_name from student where s_id in (select s_id from stu_sco GROUP BY s_id HAVING count(score)=2);

28、查询男生、女生人数

select s_sex 性别,count(*) 人数 from student GROUP BY s_sex;

29、查询名字中含有"风"字的学生信息

select * from student where s_name like '%风%';

30、查询同名同姓学生名单,并统计同名人数

SELECT s_name,count(s_id) 同名人数 FROM student GROUP BY s_name HAVING 同名人数>2;

31、查询1990年出生的学生名单

select s_name from student where year(s_age)='1990';

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

SELECT c_id,AVG(score) 平均成绩 FROM stu_sco GROUP BY c_id ORDER BY 平均成绩 desc,c_id ASC;

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

SELECT stu_sco.s_id,s_name,AVG(score) 平均成绩 from stu_sco LEFT JOIN student on stu_sco.s_id = student.s_id GROUP BY stu_sco.s_id HAVING 平均成绩>=85;

SELECT a.s_id,s_name,平均成绩 from student a join (SELECT s_id,avg(score) 平均成绩 from stu_sco GROUP BY s_id HAVING avg(score)>=85) b on a.s_id=b.s_id;

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

SELECT s_name,score from stu_sco join student on student.s_id = stu_sco.s_id where score <60 and c_id in (SELECT c_id from course where c_name='数学');

35、查询所有学生的课程及分数情况;

SELECT stu_sco.s_id,s_name,
CASE WHEN c_id= (SELECT c_id from course WHERE c_name='语文') THEN score end as '语文',
CASE WHEN c_id= (SELECT c_id from course WHERE c_name='数学') THEN score end as '数学',
CASE WHEN c_id= (SELECT c_id from course WHERE c_name='英语') THEN score end as '英语'
FROM stu_sco LEFT JOIN student on student.s_id = stu_sco.s_id;

SELECT stu_sco.s_id,s_name,
sum(CASE WHEN c_id= (SELECT c_id from course WHERE c_name='语文') THEN score end)as '语文',
sum(CASE WHEN c_id= (SELECT c_id from course WHERE c_name='数学') THEN score end) as '数学',
sum(CASE WHEN c_id= (SELECT c_id from course WHERE c_name='英语') THEN score end) as '英语'
FROM stu_sco LEFT JOIN student on student.s_id = stu_sco.s_id GROUP BY stu_sco.s_id;

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

SELECT s_name,c_name,score from stu_sco LEFT JOIN student on student.s_id=stu_sco.s_id LEFT JOIN course on course.c_id=stu_sco.c_id where score>70;

select c.s_name,b.c_name,a.score from
stu_sco a left join course b on a.c_id=b.c_id
left join student c on a.s_id=c.s_id
where (a.s_id,a.c_id) in (select s_id,c_id from stu_sco where score>70);

37、查询出现过学生考试不及格的课程

SELECT c_id,c_name from course WHERE c_id in (SELECT c_id from stu_sco where score<60);

select distinct(a.c_id),b.c_name from stu_sco a,course b where a.c_id=b.c_id and a.score<60;

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

select s_id,s_name from student where s_id in (select s_id from stu_sco where c_id=01 and score>80);
SELECT student.s_id,s_name from student JOIN stu_sco on stu_sco.s_id=student.s_id where score>80 and c_id=01;

39、求每门课程的学生人数

SELECT c_id,COUNT(s_id) from stu_sco GROUP BY c_id;

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

SELECT student.*,score
FROM stu_sco
JOIN
(SELECT c_id,max(score) 最高分
FROM stu_sco
WHERE c_id in
(SELECT c_id from course
where t_id in
(SELECT t_id from teacher
where t_name ='张三')
GROUP BY c_id )) a
ON stu_sco.c_id=a.c_id and stu_sco.score=a.最高分
JOIN student on stu_sco.s_id=student.s_id;

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

select a.s_id,a.c_id,a.score
from stu_sco a
where (SELECT count(a.s_id)
from stu_sco b
where a.score=b.score and a.c_id<>b.c_id
)>1 ;

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

SELECT a.c_id,a.s_id,a.score from stu_sco a where (SELECT count(a.s_id) from stu_sco b WHERE a.c_id =b.c_id and a.score

select a.c_id,a.s_id,a.score from stu_sco a where
(select count(a.s_id) from stu_sco b where a.c_id=b.c_id and a.score
group by a.c_id,a.s_id;

43、统计每门课程的学生选修人数(超过5人的课程才统计)。

要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
SELECT c_id,count(s_id) 选修人数 from stu_sco GROUP BY c_id HAVING 选修人数>5 ORDER BY 选修人数 DESC,c_id asc;

44、检索至少选修两门课程的学生学号

也就是学生id在考试成绩的记录要大于等于两次
SELECT s_id,count(c_id) 选课数 from stu_sco GROUP BY s_id HAVING 选课数>=2;

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

select student.* from student join (select s_id from stu_sco GROUP BY s_id HAVING count(c_id)=(SELECT COUNT(DISTINCT c_id) from course)) b on student.s_id=b.s_id;

46、查询各学生的年龄

SELECT student.*,year(CURDATE())-year(s_age) as age from student;

47、查询本周过生日的学生

select student.* from student WHERE WEEK(DATE_FORMAT(s_age,CONCAT(DATE_FORMAT(CURDATE(),'%Y'),'-%m-%d')))=WEEK(CURDATE());

48、查询下周过生日的学生

select student.* from student WHERE WEEK(DATE_FORMAT(s_age,CONCAT(DATE_FORMAT(CURDATE(),'%Y'),'-%m-%d')))=WEEK(CURDATE())+1;

49、查询本月过生日的学生

SELECT * from student;
SELECT student.* FROM student WHERE MONTH(DATE_FORMAT(s_age,"%Y-%m-%d"))=month(CURDATE());
SELECT student.* FROM student WHERE DATE_FORMAT(s_age,"%m")=month(CURDATE());

50、查询下月过生日的学生

SELECT student.* FROM student WHERE DATE_FORMAT(s_age,"%m")=month(CURDATE())+1;

你可能感兴趣的:(数据分析,数据库)