# 创建数据库
DROP DATABASE IF EXISTS test1;
CREATE DATABASE test1;
USE test1
# 创建学生表并插入数据
CREATE TABLE student(
s_id int,
s_name varchar(8),
s_birth date,
s_sex varchar(4),
PRIMARY KEY(s_id)
);
INSERT INTO student VALUES
(1,'赵雷','1990-01-01','男'),
(2,'钱电','1990-12-21','男'),
(3,'孙风','1990-05-20','男'),
(4,'李云','1990-08-06','男'),
(5,'周梅','1991-12-01','女'),
(6,'吴兰','1992-03-01','女'),
(7,'郑竹','1989-07-01','女'),
(8,'王菊','1990-01-20','女');
# 创建课程表并插入数据
CREATE TABLE course(
c_id int,
c_name varchar(8),
t_id int,
PRIMARY KEY(c_id)
);
INSERT INTO course VALUES
(1,'语文',2),
(2,'数学',1),
(3,'英语',3);
# 创建教师表并插入数据
CREATE TABLE teacher(
t_id int,
t_name varchar(8),
PRIMARY KEY(t_id)
);
INSERT INTO teacher VALUES
(1,'张三'),
(2,'李四'),
(3,'王五');
# 创建成绩表并插入数据
CREATE TABLE score (
s_id int,
c_id int,
s_score int
);
INSERT INTO score VALUES
(1,1,80),
(1,2,90),
(1,3,99),
(2,1,70),
(2,2,60),
(2,3,65),
(3,1,80),
(3,2,80),
(3,3,80),
(4,1,50),
(4,2,30),
(4,3,40),
(5,1,76),
(5,2,87),
(6,1,31),
(6,3,34),
(7,2,89),
(7,3,98);
# 设置联合主键
ALTER TABLE score ADD PRIMARY KEY(s_id,c_id);
1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数
2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数
3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩
4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩(包括有成绩的和无成绩的)
5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
6、查询"李"姓老师的数量
7、查询学过"张三"老师授课的同学的信息
8、查询没学过"张三"老师授课的同学的信息
9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息
10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息
11、查询没有学全所有课程的同学的信息
12、查询至少有一门课与学号为"01"的同学所学相同的同学的信息
13、查询和"01"号的同学学习的课程完全相同的其他同学的信息
14、查询没学过"张三"老师讲授的任一门课程的学生姓名
15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
16、检索"01"课程分数小于60,按分数降序排列的学生信息
17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
18、查询各科成绩最高分、最低分和平均分,以如下形式显示:
19、按各科成绩进行排序,并显示排名
20、查询学生的总成绩并进行排名
21、查询不同老师所教不同课程平均分从高到低显示
22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩
23、统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]及所占百分比
24、查询学生平均成绩及其名次
25、查询各科成绩前三名的记录
26、查询每门课程被选修的学生数
27、查询出只有两门课程的全部学生的学号和姓名
28、查询男生、女生人数
29、查询名字中含有"风"字的学生信息
30、查询同名同性学生名单,并统计同名人数
31、查询1990年出生的学生名单
32、查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列
33、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩
34、查询课程名称为"数学",且分数低于60的学生姓名和分数
35、查询所有学生的课程及分数情况
36、查询任何一门课程成绩在70分以上的学生姓名、课程名称和分数
37、查询课程不及格的学生
38、查询课程编号为01且课程成绩在80分以上的学生的学号和姓名
39、求每门课程的学生人数
40、查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩
41、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩
42、查询每门课程成绩最好的前三名
43、统计每门课程的学生选修人数(超过5人的课程才统计)
44、检索至少选修两门课程的学生学号
45、查询选修了全部课程的学生信息
46、查询各学生的年龄(周岁)
47、查询本周过生日的学生
48、查询下周过生日的学生
49、查询本月过生日的学生
50、查询12月份过生日的学生
三、答案(仅供参考,有问题欢迎交流)
# 1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数
SELECT stu.*, s.score1, s.score2
FROM student stu
JOIN (
SELECT s1.s_id id, s1.s_score score1, s2.s_score score2
FROM (SELECT * FROM score WHERE c_id = 1) s1
JOIN (SELECT * FROM score WHERE c_id = 2) s2 ON s1.s_id = s2.s_id
WHERE s1.s_score > s2.s_score
) s ON stu.s_id = s.id
# 2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数
SELECT stu.*, s.score1, s.score2
FROM student stu
JOIN (
SELECT s1.s_id id, s1.s_score score1, s2.s_score score2
FROM (SELECT * FROM score WHERE c_id = 1) s1
JOIN (SELECT * FROM score WHERE c_id = 2) s2 ON s1.s_id = s2.s_id
WHERE s1.s_score < s2.s_score
) s ON stu.s_id = s.id
# 3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩
SELECT stu.s_id, stu.s_name, IFNULL(ROUND(AVG(s.s_score),2),0) avg_score
FROM student stu
LEFT JOIN score s ON stu.s_id = s.s_id
GROUP BY s.s_id
HAVING avg_score >= 60
# 4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩(包括有成绩的和无成绩的)
SELECT stu.s_id, stu.s_name, IFNULL(ROUND(AVG(s.s_score),2),0) avg_score
FROM student stu
LEFT JOIN score s ON stu.s_id = s.s_id
GROUP BY s.s_id
HAVING avg_score < 60
# 5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
SELECT stu.s_id, stu.s_name, COUNT(s.s_id) num_score, SUM(s.s_score) sum_score
FROM student stu
LEFT JOIN score s ON stu.s_id = s.s_id
GROUP BY s.s_id
# 6、查询"李"姓老师的数量
SELECT COUNT(*) AS cnt_name_li
FROM teacher
WHERE t_name LIKE '李%';
# 7、查询学过"张三"老师授课的同学的信息
SELECT stu.*
FROM student stu
WHERE stu.s_id IN(
SELECT s.s_id
FROM score s
WHERE s.c_id = (
SELECT c.c_id
FROM teacher t
JOIN course c ON t.t_id = c.t_id
WHERE t.t_name = '张三'
)
)
# 8、查询没学过"张三"老师授课的同学的信息
SELECT stu.*
FROM student stu
WHERE stu.s_id NOT IN(
SELECT s.s_id
FROM score s
WHERE s.c_id = (
SELECT c.c_id
FROM teacher t
JOIN course c ON t.t_id = c.t_id
WHERE t.t_name = '张三'
)
)
# 9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息
SELECT stu.*
FROM student stu
WHERE stu.s_id IN(
SELECT s.s_id
FROM score s
WHERE s.c_id IN (1, 2)
GROUP BY s.s_id
HAVING COUNT(*) = 2
)
# 10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息
SELECT stu.*
FROM student stu
WHERE stu.s_id IN(
SELECT s.s_id
FROM score s
WHERE s.c_id = 1 AND s.s_id NOT IN(
SELECT s_id
FROM score
WHERE c_id = 2
)
)
# 11、查询没有学全所有课程的同学的信息
SELECT stu.*
FROM student stu
WHERE stu.s_id NOT IN(
SELECT s.s_id
FROM score s
WHERE s.c_id IN (1, 2, 3)
GROUP BY s.s_id
HAVING COUNT(*) = 3
)
# 12、查询至少有一门课与学号为"01"的同学所学相同的同学的信息
SELECT stu.*
FROM student stu
WHERE stu.s_id IN(
SELECT s.s_id
FROM score s
WHERE s.c_id IN (
SELECT c_id
FROM score
WHERE s_id = 1
)
GROUP BY s.s_id
HAVING COUNT(*) > 0
)
# 13、查询和"01"号的同学学习的课程完全相同的其他同学的信息
SELECT stu.*
FROM student stu
WHERE stu.s_id IN(
SELECT s.s_id
FROM score s
WHERE s.s_id != 1 AND s.c_id IN (
SELECT c_id
FROM score
WHERE s_id = 1
)
GROUP BY s.s_id
HAVING COUNT(*) = 3
)
# 14、查询没学过"张三"老师讲授的任一门课程的学生姓名
SELECT stu.s_name
FROM student stu
WHERE stu.s_id NOT IN(
SELECT s.s_id
FROM score s
WHERE s.c_id IN (
SELECT c.c_id
FROM teacher t
JOIN course c ON t.t_id = c.t_id
WHERE t.t_name = '张三'
)
)
# 15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
SELECT stu.s_id, stu.s_name, ROUND(AVG(s.s_score),2) avg_score
FROM student stu
JOIN score s ON stu.s_id = s.s_id
WHERE s.s_score < 60
GROUP BY stu.s_id
HAVING COUNT(*) > 1
# 16、检索"01"课程分数小于60,按分数降序排列的学生信息
SELECT stu.*, s.s_score score
FROM student stu
JOIN score s ON stu.s_id = s.s_id
WHERE s.c_id = 1 AND s.s_score < 60
ORDER BY s.s_score DESC
# 17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
SELECT stu.s_id, stu.s_name,
MAX(CASE WHEN c.c_name = '语文' THEN s.s_score ELSE 0 END) '语文',
MAX(CASE WHEN c.c_name = '数学' THEN s.s_score ELSE 0 END) '数学',
MAX(CASE WHEN c.c_name = '英语' THEN s.s_score ELSE 0 END) '英语',
IFNULL(ROUND(AVG(s.s_score),2),0) '平均成绩'
FROM student stu
LEFT JOIN
score s ON stu.s_id = s.s_id
LEFT JOIN
course c ON s.c_id = c.c_id
GROUP BY stu.s_id
# 18、查询各科成绩最高分、最低分和平均分,以如下形式显示:
SELECT c.c_id, c.c_name,
MAX(expr) '最高分',
'最低分',
'平均分'
FROM score s
JOIN course c ON s.c_id = c.c_id
GROUP BY c.c_id
# 19、按各科成绩进行排序,并显示排名
--
set @rank = 0, @c = null;
SELECT @rank := if(@c = t.c_id, @rank+1, 1) ranking, @c := t.c_id, t.c_id, t.c_name, t.s_id, t.s_name, t.s_score
FROM (
SELECT c.c_id, c.c_name, stu.s_id, stu.s_name, s.s_score
FROM score s
JOIN student stu ON s.s_id = stu.s_id
JOIN course c ON s.c_id = c.c_id
) t
ORDER BY t.c_id, t.s_score DESC
SELECT ROW_NUMBER() OVER (PARTITION BY c.c_id ORDER BY c.c_id, s.s_score DESC) ranking, c.c_id, c.c_name, stu.s_id, stu.s_name, s.s_score
FROM score s
JOIN student stu ON s.s_id = stu.s_id
JOIN course c ON s.c_id = c.c_id
# 20、查询学生的总成绩并进行排名
SELECT ROW_NUMBER() OVER (ORDER BY t.sum_score DESC) ranking, t.s_id, t.s_name, t.sum_score
FROM (
SELECT stu.s_id, stu.s_name, SUM(s.s_score) sum_score
FROM score s
JOIN student stu ON s.s_id = stu.s_id
GROUP BY s.s_id
) t
# 21、查询不同老师所教不同课程平均分从高到低显示
SELECT t.t_id, t.t_name, c.c_id, c.c_name, ROUND(AVG(s.s_score), 2) avg_score
FROM score s
JOIN course c ON s.c_id = c.c_id
JOIN teacher t ON c.t_id = t.t_id
GROUP BY t.t_id, c.c_id
ORDER BY avg_score DESC
# 22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩
SELECT *
FROM score
# 23、统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]及所占百分比
SELECT c.c_id '课程编号', c.c_name '课程名称',
SUM(CASE WHEN s.s_score <= 100 and s.s_score > 85 THEN 1 ELSE 0 END) '[100-85]',
CONCAT(ROUND(SUM(CASE WHEN s.s_score <= 100 and s.s_score > 85 THEN 1 ELSE 0 END)/COUNT(s_score)*100,2),'%') '百分比',
SUM(CASE WHEN s.s_score <= 85 and s.s_score > 70 THEN 1 ELSE 0 END) '[85-70]',
CONCAT(ROUND(SUM(CASE WHEN s.s_score <= 85 and s.s_score > 70 THEN 1 ELSE 0 END)/COUNT(s_score)*100,2),'%') '百分比',
SUM(CASE WHEN s.s_score <= 70 and s.s_score > 60 THEN 1 ELSE 0 END)'[70-60]',
CONCAT(ROUND(SUM(CASE WHEN s.s_score <= 70 and s.s_score > 60 THEN 1 ELSE 0 END)/COUNT(s_score)*100,2),'%') '百分比',
SUM(CASE WHEN s.s_score <= 60 and s.s_score >= 0 THEN 1 ELSE 0 END) '[0-60]',
CONCAT(ROUND(SUM(CASE WHEN s.s_score <= 60 and s.s_score > 0 THEN 1 ELSE 0 END)/COUNT(s_score)*100,2),'%') '百分比'
FROM score s
JOIN course c ON c.c_id = s.c_id
GROUP BY c.c_id
# 24、查询学生平均成绩及其名次
-- RANK() 并列跳跃排名
-- ROW_NUMBER() 连续排名
-- rank为MySQL的保留字,要使用保留字的时候,要用反引号将其引起来
SELECT ROW_NUMBER() OVER (ORDER BY s.avg_score DESC) AS ranking, stu.s_id, stu.s_name, s.avg_score
FROM (
SELECT s_id , AVG(s_score) avg_score
FROM score
GROUP BY s_id
) s
JOIN student stu ON s.s_id = stu.s_id
# 25、查询各科成绩前三名的记录
SELECT c.c_id, c.c_name, stu.s_id, stu.s_name, s.s_score
FROM (
SELECT *
FROM score s1
WHERE (
SELECT COUNT(*)
FROM score s2
WHERE s1.c_id = s2.c_id and s1.s_score < s2.s_score
) < 3
) s
JOIN course c ON s.c_id = c.c_id
JOIN student stu ON s.s_id = stu.s_id
ORDER BY c.c_id, s.s_score
# 26、查询每门课程被选修的学生数
SELECT c.c_id, c.c_name, s.stu_num
FROM course c
JOIN (
SELECT *, COUNT(*) stu_num
FROM score
GROUP BY c_id
) s
ON c.c_id = s.c_id
# 27、查询出只有两门课程的全部学生的学号和姓名
SELECT s_id, s_name
FROM student
WHERE s_id in (
SELECT s_id
FROM score
GROUP BY s_id
HAVING COUNT(*) = 2
)
# 28、查询男生、女生人数
SELECT s_sex, COUNT(*) sex_num
FROM student
GROUP BY s_sex
# 29、查询名字中含有"风"字的学生信息
-- "%" 百分号通配符: 表示任何字符出现任意次数 (可以是0次)
-- "_" 下划线通配符:表示只能匹配单个字符,不能多也不能少,就是一个字符
SELECT *
FROM student
WHERE s_name LIKE '%风%'
# 30、查询同名同性学生名单,并统计同名人数
SELECT *, COUNT(*) con_name_num
FROM student
GROUP BY s_name, s_sex
HAVING con_name_num > 1
# 31、查询1990年出生的学生名单
SELECT *
FROM student
WHERE DATE_FORMAT(s_birth,'%Y') = '1990'
# 32、查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列
SELECT c.c_id, c.c_name, s.avg_score
FROM course c
JOIN (
SELECT c_id, s_score, ROUND(AVG(s_score), 2) avg_score
FROM score
GROUP BY c_id
) s
ON c.c_id = s.c_id
ORDER BY avg_score DESC, c_id ASC
# 33、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩
-- 保留两位小数 round(avg(s_score), 2)
SELECT stu.s_id, stu.s_name, s.avg_score
FROM student stu
JOIN (
SELECT s_id, ROUND(AVG(s_score), 2) avg_score
FROM score
GROUP BY s_id
HAVING avg_score > 85
) s
ON stu.s_id = s.s_id
# 34、查询课程名称为"数学",且分数低于60的学生姓名和分数
SELECT stu.s_name, s.s_score
FROM student stu
JOIN (
SELECT s_id, s_score
FROM score
WHERE s_score < 60 and c_id = (
SELECT c_id
FROM course
WHERE c_name = '数学'
)
) s
ON stu.s_id = s.s_id
# 35、查询所有学生的课程及分数情况
SELECT stu.s_id, stu.s_name, c.c_name,
MAX(CASE WHEN c.c_name = '语文' THEN s.s_score ELSE 0 END) '语文',
MAX(CASE WHEN c.c_name = '数学' THEN s.s_score ELSE 0 END) '数学',
MAX(CASE WHEN c.c_name = '英语' THEN s.s_score ELSE 0 END) '英语'
FROM student stu
LEFT JOIN
score s ON stu.s_id = s.s_id
LEFT JOIN
course c ON s.c_id = c.c_id
GROUP BY stu.s_id
# 36、查询任何一门课程成绩在70分以上的学生姓名、课程名称和分数
SELECT stu.s_name, c.c_name, s.s_score
FROM score s
JOIN student stu ON s.s_id = stu.s_id
JOIN course c ON s.c_id = c.c_id
WHERE s.s_score > 70
# 37、查询课程不及格的学生
SELECT s.s_id, stu.s_name, s.c_id, c.c_name, s.s_score
FROM (
SELECT *
FROM score
WHERE s_score < 60
) s
JOIN student stu ON s.s_id = stu.s_id
JOIN course c ON s.c_id = c.c_id
# 38、查询课程编号为01且课程成绩在80分以上的学生的学号和姓名
SELECT s_id, s_name
FROM student
WHERE s_id IN (
SELECT s_id
FROM score
WHERE c_id = 1 AND s_score >= 80
)
# 39、求每门课程的学生人数
SELECT course.c_id, course.c_name, s_num.num
FROM course
JOIN (
SELECT c_id, COUNT(*) num
FROM score
GROUP BY c_id
) s_num
ON course.c_id = s_num.c_id
# 40、查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩
SELECT stu.*, s.s_score
FROM student stu
JOIN (
SELECT s_id, s_score
FROM score
WHERE c_id = (
SELECT c_id
FROM course
WHERE t_id = (
SELECT t_id
FROM teacher
WHERE t_name = '张三'
)
)
ORDER BY s_score DESC
LIMIT 1
) s
ON stu.s_id = s.s_id
# 41、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩
SELECT s_id, c_id, s_score
FROM score
WHERE s_score = (
SELECT s_score
FROM score
GROUP BY s_score
HAVING COUNT(*) > 1
)
# 42、查询每门课程成绩最好的前三名
-- 查看SQL_MODE
select @@GLOBAL.sql_mode;
-- 修改SQL_MODE
SELECT t1.c_id, c.c_name, t1.s_id, s.s_name, t1.s_score
FROM (
SELECT c_id, s_id, s_score
FROM score
WHERE (
SELECT COUNT(*)
FROM score s1
WHERE score.c_id = s1.c_id and score.s_score < s1.s_score
) < 3
) t1
JOIN course c ON t1.c_id = c.c_id
JOIN student s ON t1.s_id = s.s_id
ORDER BY t1.c_id, t1.s_score DESC
# 43、统计每门课程的学生选修人数(超过5人的课程才统计)
SELECT c_id, COUNT(*) as cselect_stu
FROM score
GROUP BY c_id
HAVING COUNT(*) > 5;
# 44、检索至少选修两门课程的学生学号
SELECT s_id
FROM score
GROUP BY s_id
HAVING COUNT(*) > 1;
# 45、查询选修了全部课程的学生信息
SELECT *
FROM student
WHERE s_id IN (
SELECT s_id
FROM score
GROUP BY s_id
HAVING COUNT(*) = (
SELECT COUNT(*) FROM course
)
)
# 46、查询各学生的年龄(周岁)
SELECT *, IF(DATE_FORMAT(s_birth,'%m-%d') <= DATE_FORMAT(NOW(),'%m-%d'),YEAR(NOW()) - YEAR(s_birth) - 1,YEAR(NOW()) - YEAR(s_birth)) as s_age
FROM student;
# 47、查询本周过生日的学生
SELECT *
FROM student
WHERE WEEK(NOW()) = WEEK(CONCAT(YEAR(NOW()),"-",DATE_FORMAT(s_birth,'%m-%d')));
# 48、查询下周过生日的学生
-- 这种情况会存在问题
SELECT *
FROM student
WHERE WEEK(s_birth) = WEEK(DATE_FORMAT(NOW(),'%Y%m%d')) + 1;
-- 正解:
SELECT *
FROM student
WHERE WEEK(NOW()) + 1 = WEEK(CONCAT(YEAR(NOW()),"-",DATE_FORMAT(s_birth,'%m-%d')));
# 49、查询本月过生日的学生
SELECT *
FROM student
WHERE DATE_FORMAT(s_birth,'%m') = DATE_FORMAT(NOW(), '%m');
# 50、查询12月份过生日的学生
SELECT *
FROM student
WHERE DATE_FORMAT(s_birth,'%m') = 12;