MySQL练习50题

习题来源于网络,SQL语句是自己的练习答案,部分参考了网络上的答案。

花了一晚上的时间做完,个人认为其中的难点有:分组提取前几名的数据、给数据进行排序

相关资料参考:

sql语句练习50题(Mysql版)

50道SQL练习题及答案与详细分析

 

表结构

–1.学生表
Student(s_id,s_name,s_birth,s_sex) –学生编号,学生姓名, 出生年月,学生性别
–2.课程表
Course(c_id,c_name,t_id) – –课程编号, 课程名称, 教师编号
–3.教师表
Teacher(t_id,t_name) –教师编号,教师姓名
–4.成绩表
Score(s_id,c_id,s_score) –学生编号,课程编号,分数

测试数据

--建表
--学生表
CREATE TABLE `Student`(
    `s_id` VARCHAR(20),
    `s_name` VARCHAR(20) NOT NULL DEFAULT '',
    `s_birth` VARCHAR(20) NOT NULL DEFAULT '',
    `s_sex` VARCHAR(10) NOT NULL DEFAULT '',
    PRIMARY KEY(`s_id`)
);
--课程表
CREATE TABLE `Course`(
    `c_id`  VARCHAR(20),
    `c_name` VARCHAR(20) NOT NULL DEFAULT '',
    `t_id` VARCHAR(20) NOT NULL,
    PRIMARY KEY(`c_id`)
);
--教师表
CREATE TABLE `Teacher`(
    `t_id` VARCHAR(20),
    `t_name` VARCHAR(20) NOT NULL DEFAULT '',
    PRIMARY KEY(`t_id`)
);
--成绩表
CREATE TABLE `Score`(
    `s_id` VARCHAR(20),
    `c_id`  VARCHAR(20),
    `s_score` INT(3),
    PRIMARY KEY(`s_id`,`c_id`)
);
--插入学生表测试数据
insert into Student values('01' , '赵雷' , '1990-01-01' , '男');
insert into Student values('02' , '钱电' , '1990-12-21' , '男');
insert into Student values('03' , '孙风' , '1990-05-20' , '男');
insert into Student values('04' , '李云' , '1990-08-06' , '男');
insert into Student values('05' , '周梅' , '1991-12-01' , '女');
insert into Student values('06' , '吴兰' , '1992-03-01' , '女');
insert into Student values('07' , '郑竹' , '1989-07-01' , '女');
insert into Student values('08' , '王菊' , '1990-01-20' , '女');
--课程表测试数据
insert into Course values('01' , '语文' , '02');
insert into Course values('02' , '数学' , '01');
insert into Course values('03' , '英语' , '03');

--教师表测试数据
insert into Teacher values('01' , '张三');
insert into Teacher values('02' , '李四');
insert into Teacher values('03' , '王五');

--成绩表测试数据
insert into Score values('01' , '01' , 80);
insert into Score values('01' , '02' , 90);
insert into Score values('01' , '03' , 99);
insert into Score values('02' , '01' , 70);
insert into Score values('02' , '02' , 60);
insert into Score values('02' , '03' , 80);
insert into Score values('03' , '01' , 80);
insert into Score values('03' , '02' , 80);
insert into Score values('03' , '03' , 80);
insert into Score values('04' , '01' , 50);
insert into Score values('04' , '02' , 30);
insert into Score values('04' , '03' , 20);
insert into Score values('05' , '01' , 76);
insert into Score values('05' , '02' , 87);
insert into Score values('06' , '01' , 31);
insert into Score values('06' , '03' , 34);
insert into Score values('07' , '02' , 89);
insert into Score values('07' , '03' , 98);

练习题及个人答案

-- 1. 查询" 01 "课程比" 02 "课程成绩高的学生的信息及课程分数
-- 步骤:先查找01课程比02课程分数高的学生信息,再根据学生s_id同Student表连接
select * 
from Student
right join (
	select a.s_id, a.s_score as score01, b.s_score as score02
	from Score as a, Score as b
	where a.s_id=b.s_id
	and a.c_id='01'
	and b.c_id='02'
	and a.s_score>b.s_score
) as c
on Student.s_id=c.s_id

-- 1.1 查询同时存在" 01 "课程和" 02 "课程的情况
select * 
from Score as a, Score as b
where a.c_id='01' 
and b.c_id='02'
and a.s_id=b.s_id
-- 1.2 查询存在" 01 "课程但可能不存在" 02 "课程的情况(不存在时显示为 null )
-- 步骤:左连接 left join
-- 1.3 查询不存在" 01 "课程但存在" 02 "课程的情况

-- 2. 查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩
-- 步骤:先找到平均成绩大于60的s_id,再根据s_id和Student表作内连接
-- select s_id, avg(s_score) as avg_score
-- from Score
-- group by s_id
-- having avg(s_score)>60
select a.s_id, a.s_name, b.avg_score
from Student a
join (select s_id, avg(s_score) as avg_score
		from Score
		group by s_id
		having avg(s_score)>60
		) as b
on a.s_id=b.s_id

-- 3. 查询在 SC 表存在成绩的学生信息
-- 步骤:先找出Score表中存在的s_id,再根据s_id从Student表提取信息
select *
from Student
where s_id in (select distinct s_id from Score)

-- 4. 查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null )
-- 步骤:先对Score表使用group by,获得每个学生的选课总数和总成绩,再根据s_id同Student表进行left join(没有成绩的学生显示null)
--------------先分组再join
select a.s_id, a.s_name, b.score_cnt, b.score_sum
from Student a
left join (select s_id, count(s_score) as score_cnt, sum(s_score) as score_sum 
	from Score
	group by s_id
    ) as b
on a.s_id=b.s_id
--------------先join再做分组
select a.s_id, a.s_name, count(b.c_id) as course_sum, sum(b.s_score) as score_sum 
from student a
left join score b
on a.s_id=b.s_id
group by a.s_id, a.s_name 

-- 5. 查询「李」姓老师的数量
-- 步骤:使用like模糊查询
select count(*)
from teacher
where t_name like '李%'

-- 6. 查询学过「张三」老师授课的同学的信息
-- 步骤:先找到张三老师的课程号,再从Score表取得上过课的学生号,再根据学生号取得个人信息
select *
from student
where student.s_id in (
    select distinct s_id
	from score
	where score.c_id in (
        select c_id
		from course, teacher
		where course.t_id=teacher.t_id
		and t_name='张三'
        )
	)
-- 步骤:直接联合四张表,用where进行条件筛选
select *
from course, teacher, score, student
where course.t_id=teacher.t_id
and teacher.t_name='张三'
and score.c_id=course.c_id
and student.s_id=score.s_id

-- 7. 查询没有学全所有课程的同学的信息
-- 步骤:首先对Score表进行group by,得到没有学完课程的学生号(计数<3)
-- 同student表进行右连接
-- ?问题,王菊没有参加课程,这个方法不能查询到
select student.*, b.course_cnt
from student
right join (
	select s_id, count(c_id) as course_cnt
	from score
	group by s_id
	having count(c_id)<3
    ) as b
on student.s_id=b.s_id
-- 步骤:反向筛选。先选出全部参加了课程的同学,再反向找到没有学全所有课程的同学
select * from student
where student.s_id not in (
  select score.s_id from score
  group by score.s_id
  having count(score.c_id)= (select count(c_id) from course)
)

-- 8. 查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息
-- 步骤:首先找到学号01的同学所学课程编号,再找到学过这些课程的同学的信息
select distinct student.*
from score, student
where score.c_id in (select distinct c_id from score where s_id='01')
and score.s_id <> '01'
and student.s_id=score.s_id

-- 9. 查询和" 01 "号的同学学习的课程完全相同的其他同学的信息
-- 步骤:这里没有找到对比课程号的方法,使用课程号计数进行比较
select a.s_id, count(a.c_id), b.*
from score a, student b
where a.s_id=b.s_id
group by a.s_id
having count(a.c_id)=(select count(c_id) from score where s_id='01')

-- 10. 查询没学过"张三"老师讲授的任一门课程的学生姓名
-- 步骤:先查找张三教授的课程信息,得到上过张三课的学生id,再使用not in找到没上学课的学生
select s_name
from student
where s_id not in (
select distinct s_id
	from score, teacher, course
	where teacher.t_name='张三'
	and teacher.t_id=course.t_id
	and score.c_id=course.c_id
    )

-- 11. 查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
-- 步骤:根据score表得到这部分同学的学号和平均成绩,再join student表
select score.s_id, student.s_name, avg(s_score)
from score, student
where s_score<60
and score.s_id=student.s_id
group by score.s_id
having count(score.s_score)>=2

-- 12. 检索" 01 "课程分数小于 60,按分数降序排列的学生信息
-- 步骤:条件查询后order by排序即可
select *
from score, student
where score.s_id=student.s_id
and score.c_id='01'
and score.s_score<60
order by score.s_score desc

-- 13. 按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
-- 步骤:首先对score表group by,得到每个学生的平均成绩;再将新表left join到score表
select  score.*, b.score_avg
from score
left join (
	select s_id, avg(s_score) as score_avg
	from score
	group by s_id
    ) as b
on score.s_id=b.s_id
order by b.score_avg desc, s_id
-- 推荐:计算平均成绩时利用group by
select s_id, 
	(select s_score from score where score.s_id=a.s_id and score.c_id='01') as '语文',
    (select s_score from score where score.s_id=a.s_id and score.c_id='02') as '数学',
    (select s_score from score where score.s_id=a.s_id and score.c_id='03') as '英语',
    avg(s_score) as '均分'
from score a
group by s_id

-- 14. 查询各科成绩最高分、最低分和平均分:
-- 以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率
-- 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90
-- 要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
-- 步骤:使用case when进行计数实现及格率等查询
select a.c_id as '课程id', b.c_name as '课程名', count(a.c_id) as '选修人数',  max(a.s_score) as '最高分',
min(a.s_score) as '最低分', avg(a.s_score) as '平均分',
round(100*sum(case when a.s_score>=60 then 1 else 0 end)/count(a.s_score)) as '及格率',
round(100*sum(case when a.s_score<=70 and a.s_score<80 then 1 else 0 end)/count(a.s_score)) as '中等率',
round(100*sum(case when a.s_score<=80 and a.s_score<90 then 1 else 0 end)/count(a.s_score)) as '优良率',
round(100*sum(case when a.s_score>=90 then 1 else 0 end)/count(a.s_score)) as '优良率'
from score a
left join course b
on a.c_id=b.c_id
group by a.c_id
order by '选修人数' desc, '课程id'

15. 按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺
-- MySQL中没有Rank函数
-- 参考:https://www.jianshu.com/p/bb1b72a1623e

15.1 按各科成绩进行排序,并显示排名, Score 重复时合并名次
16. 查询学生的总成绩,并进行排名,总分重复时保留名次空缺
16.1 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺

-- 17. 统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比
-- case when
select a.c_id, b.c_name,
(sum(case when a.s_score<=100 and s_score>85 then 1 else 0 end)/count(a.s_score)) as '100-85',
(sum(case when a.s_score<=85 and s_score>70 then 1 else 0 end)/count(a.s_score)) as '85-70',
(sum(case when a.s_score<=70 and s_score>60 then 1 else 0 end)/count(a.s_score)) as '70-60',
(sum(case when a.s_score<=60 then 1 else 0 end)/count(a.s_score)) as '60-0'
from score a, course b
where a.c_id=b.c_id
group by a.c_id

-- 18. 查询各科成绩前三名的记录
-- 步骤:分组查询前三名,在where里做限制
-- https://www.cnblogs.com/hxzblog/p/7307537.html
select s.c_id, s.s_score
from score s
where (select count(*) from score where c_id=s.c_id and s.s_score<s_score)<3
group by s.c_id, s.s_score
order by s.c_id, s.s_score

-- 19. 查询每门课程被选修的学生数
select c_id, count(s_id)
from score
group by c_id

-- 20. 查询出只选修两门课程的学生学号和姓名
-- group by分组后使用having限制
select s_id, count(c_id)
from score
group by s_id
having count(c_id)=2

-- 21. 查询男生、女生人数
-- group by分组后count计数
select s_sex, count(s_sex)
from student
group by s_sex

-- 22. 查询名字中含有「风」字的学生信息
-- 使用%进行模糊查询
select *
from student
where s_name like '%风%'

-- 23. 查询同名同性学生名单,并统计同名人数
-- 先查询出同名同性学生的信息,再group by分组后count计数
select a.s_name, a.s_sex, count(*)
from student a, student b
where a.s_name=b.s_name
-- and a.s_id <> b.s_id 注:student表中没有同名同性的学生,所以这行注释,用于显示当前表的结果
and a.s_sex=b.s_sex
group by a.s_name, a.s_sex

-- 24. 查询 1990 年出生的学生名单
-- 使用year函数
select *
from student
where year(s_birth)=1990

-- 25. 查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列
-- order by多列排序
select c_id, avg(s_score) as score_avg
from score
group by c_id
order by score_avg desc, c_id

-- 26. 查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩
-- group by分组后having做筛选
select a.s_id, b.s_name, avg(a.s_score) as '平均成绩'
from score a, student b
where a.s_id=b.s_id
group by a.s_id
having avg(a.s_score)>=85

-- 27. 查询课程名称为「数学」,且分数低于 60 的学生姓名和分数
select a.s_name, b.s_score
from student a, score b, course c
where a.s_id=b.s_id
and c.c_id=b.c_id
and c.c_name='数学'
and b.s_score<60

-- 28. 查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况)
-- 步骤:存在学生没成绩没选课的情况,则应使用student表作为左连接的左表
select *
from student
left join score
on student.s_id=score.s_id

-- 29. 查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数
select b.s_name, c.c_name, a.s_score
from score a, student b, course c 
where a.s_id=b.s_id
and a.c_id=c.c_id
and a.s_score>70

-- 30. 查询不及格的课程
select b.s_name, c.c_name, a.s_score
from score a, student b, course c 
where a.s_id=b.s_id
and a.c_id=c.c_id
and a.s_score<60

-- 31. 查询课程编号为 01 且课程成绩在 80 分以上的学生的学号和姓名
select a.s_id, a.c_id, b.s_name, a.s_score
from score a, student b
where a.s_id=b.s_id
and a.c_id='01'
and a.s_score>=80

-- 32. 求每门课程的学生人数
-- group by分组后count计数
select a.c_id, b.c_name, count(a.c_id)
from score a, course b
where a.c_id=b.c_id
group by a.c_id

-- 33. 成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩
-- 成绩不重复,即成绩最高的只有一个学生,可以排序后使用limit 1
----------使用四个表联合进行查询
select *
from student a, course b, teacher c, score d
where a.s_id=d.s_id
and b.c_id=d.c_id
and b.t_id=c.t_id
and c.t_name='张三'
order by d.s_score desc
limit 1
----------嵌套查询
-- 先从teacher表找到“张三”对应的教师id
-- 再从course表找到“张三”对应的课程id
-- 再从score表找到选修“张三”对应课程的学生分数
-- 根据score表的s_id从student表找到对应的学生信息
select a.s_id, a.s_score, b.*
from score a, student b
where a.s_id=b.s_id
and a.c_id=(select c_id
	from course
	where t_id=(select t_id
		from teacher
		where t_name='张三'))
order by a.s_score desc
limit 1

-- 34. 成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩
-- 成绩有重复,即成绩最高的有多个学生,可以使用where和max进行选择
-- 先找到成绩最高的数值,再对s_score进行匹配
select *
from student a, score b
where a.s_id=b.s_id
and b.s_score = (select max(s_score)
	from course a, teacher b, score c
	where a.t_id=b.t_id
	and a.c_id=c.c_id
	and b.t_name='张三'
	)

-- 35. 查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩
-- score表自连接
select distinct *
from (select a.s_id, a.c_id, a.s_score
from score a, score b
where a.s_score=b.s_score
and a.c_id <> b.c_id) as d

-- 36. 查询每门课程成绩最好的前两名
-- 分组后取前两名,同题18
select a.c_id, a.s_score
from score a
where (select count(*) from score where c_id=a.c_id and s_score>a.s_score)<2
group by a.c_id, a.s_score
order by a.c_id, a.s_score

-- 37. 统计每门课程的学生选修人数(超过 5 人的课程才统计)。
select a.c_id, b.c_name, count(a.c_id) as stu_num
from score a, course b
where a.c_id=b.c_id
group by a.c_id
having stu_num>5

-- 38. 检索至少选修两门课程的学生学号
-- group by分组后having限制
select s_id, count(c_id)
from score
group by s_id
having count(c_id)>=2

-- 39. 查询选修了全部课程的学生信息
-- group by分组后having限制
select a.s_id, count(a.c_id)
from score a, student b
where a.s_id=b.s_id
group by a.s_id
having count(a.c_id)=(select count(*) from course)

-- 40. 查询各学生的年龄,只按年份来算
select s_name, year(s_birth), year(now())-year(s_birth) as '年龄'
from student

-- 41. 按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一
-- 使用时间函数
------ 对month和day共同进行比较,当前月份小于出生月份并且当前日期小于出生日期才减一
select s_birth, 
year(s_birth), year(now())-year(s_birth) as '年龄',
(case when month(now())<month(s_birth)
 and day(now())<day(s_birth) 
 then year(now())-year(s_birth)-1
 else year(now())-year(s_birth)
 end)
from student
------ 使用时间格式进行查看,将月日总体进行比较,8月1日>7月2日
select s_birth, year(now())-year(s_birth),
(DATE_FORMAT(NOW(),'%Y')-DATE_FORMAT(s_birth,'%Y') - 
                (case when DATE_FORMAT(NOW(),'%m%d')>DATE_FORMAT(s_birth,'%m%d') then 0 else 1 end)) as age
from student

-- 42. 查询本周过生日的学生

-- 43. 查询下周过生日的学生

-- 44. 查询本月过生日的学生
select s_birth, month(s_birth) as birth_month
from student
where month(s_birth)=month(now())

-- 45. 查询下月过生日的学生
select s_birth, month(s_birth) as birth_month
from student
where month(s_birth)=month(now())+1

  

猜你喜欢

转载自www.cnblogs.com/xingyucn/p/10428827.html