1. 表格
题目涉及到四张表格:
- 学生表(Student)
- 课程表(Course)
- 教师表(Teacher)
- 成绩表(Score)
附表格创建代码:
CREATE TABLE Student
(
s_id VARCHAR(20),
s_name VARCHAR(20) NOT NULL,
s_birth VARCHAR(20) NOT NULL,
s_sex VARCHAR(10) NOT NULL,
PRIMARY KEY(s_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', '女');
CREATE TABLE Course
(
c_id VARCHAR(20),
c_name VARCHAR(20) NOT NULL,
t_id VARCHAR(20) NOT NULL,
PRIMARY KEY(c_id)
);
INSERT INTO Course VALUES('01', '语文', '02');
INSERT INTO Course VALUES('02', '数学', '01');
INSERT INTO Course VALUES('03', '英语', '03');
CREATE TABLE Teacher
(
t_id VARCHAR(20),
t_name VARCHAR(20) NOT NULL DEFAULT '',
PRIMARY KEY(t_id)
);
INSERT INTO Teacher VALUES('01', '张三');
INSERT INTO Teacher VALUES('02', '李四');
INSERT INTO Teacher VALUES('03', '王五');
CREATE TABLE Score
(
s_id VARCHAR(20),
c_id VARCHAR(20),
s_score INT(3),
PRIMARY KEY(s_id, c_id)
);
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);
SELECT * FROM Student;
SELECT * FROM Course;
SELECT * FROM Teacher;
SELECT * FROM Score;
2. 题目
1、查询课程编号为“01”的课程比“02”的课程成绩高的所有学生的学号 2、查询同时存在" 01 “课程和” 02 "课程的情况
3. 题目 + 解答
1、查询课程编号为“01”的课程比“02”的课程成绩高的所有学生的学号
多次将Score表 自连接实现将同一个字段两次使用:
SELECT st.*, m.s_score1, m.s_score2
FROM
(
SELECT sc1.s_id, sc1.s_score s_score1, sc2.s_score s_score2
FROM Score sc1
JOIN Score sc2
ON sc1.s_id = sc2.s_id
AND sc1.c_id = '01'
AND sc2.c_id = '02'
AND sc1.s_score > sc2.s_score
) m
JOIN Student st
ON m.s_id = st.s_id;
2、查询平均成绩大于60分的学生的学号和平均成绩
SELECT s_id, AVG(s_score) avg_score
FROM Score
GROUP BY s_id
HAVING avg_score > 60;
2.1、所有成绩小于60分的学生信息
SELECT st.s_id, st.s_name, st.s_birth, st.s_sex
FROM Student st
JOIN (
SELECT s_id, MIN(s_score) min_score
FROM Score
GROUP BY s_id
HAVING min_score < 60) t
ON st.s_id = t.s_id
2.2、查询平均成绩小于60分的学生的学号和平均成绩,考虑没参加考试的情况
SELECT m.s_id,
AVG(m.score) avg_score
FROM
(
SELECT st.s_id,
IF(sc.s_score IS NULL, 0, sc.s_score) score
FROM Student st
LEFT JOIN Score sc
ON st.s_id = sc.s_id
) m
GROUP BY m.s_id
HAVING avg_score < 60;
另解:
SELECT m.s_id, AVG(IFNULL(m.s_score, 0)) avg_score
FROM
(
SELECT st.s_id, sc.s_score
FROM Student st
LEFT JOIN Score sc
ON st.s_id = sc.s_id
) m
GROUP BY m.s_id
HAVING AVG(IFNULL(m.s_score, 0)) < 60
3、查询所有学生的学号、姓名、选课数、总成绩
SELECT st.s_id, st.s_name, COUNT(sc.c_id), SUM(sc.s_score)
FROM Student st
LEFT JOIN Score sc
ON st.s_id = sc.s_id
GROUP BY st.s_id, st.s_name
4、查询姓“猴”的老师的个数
SELECT COUNT(t_name)
FROM Teacher
WHERE t_name LIKE "猴%"
5、查询没学过“张三”老师课的学生的学号、姓名
SELECT st.s_id, st.s_name
FROM Student st
WHERE s_id NOT IN
(
SELECT sc.s_id
FROM Score sc
JOIN Course c
ON sc.c_id = c.c_id
JOIN Teacher t
ON c.t_id = t.t_id
WHERE t.t_name = "张三"
)
6、查询学过“张三”老师所教的所有课的同学的学号、姓名
SELECT st.s_id, st.s_name
FROM Student st
WHERE st.s_id IN
(
SELECT DISTINCT sc.s_id
FROM
(SELECT c.c_id
FROM Course c
JOIN Teacher t
ON c.t_id = t.t_id
WHERE t.t_name = "张三") s
LEFT JOIN Score sc
ON s.c_id = sc.c_id
WHERE sc.s_id IS NOT NULL
);
7、查询学过编号为“01”的课程并且也学过编号为“02”的课程的学生的学号、姓名(!)
SELECT st.s_id, st.s_name
FROM Student st
JOIN
(
SELECT sc1.*
FROM Score sc1
JOIN Score sc2
ON sc1.s_id = sc2.s_id
WHERE sc1.c_id = '01'
AND sc2.c_id = '02'
) m
ON st.s_id = m.s_id;
7.1、查询学过编号为“01”的课程但没有学过编号为“02”的课程的学生的学号、姓名(!)
SELECT DISTINCT st.s_id, st.s_name
FROM Student st
JOIN
(
SELECT sc1.*
FROM Score sc1
JOIN Score sc2
ON sc1.c_id = '01'
AND sc2.c_id != '02'
) m
ON st.s_id = m.s_id
8、查询课程编号为“02”的总成绩
SELECT SUM(s_score)
FROM Score
GROUP BY c_id
HAVING c_id = '02'
9、查询所有课程成绩小于60分的学生的学号、姓名
SELECT DISTINCT st.s_id, st.s_name
FROM Student st
JOIN
(
SELECT s_id,
MIN(s_score) min_score
FROM Score s
GROUP BY s.s_id
HAVING min_score < 60
) s
ON st.s_id = s.s_id
10、查询没有学全所有课的学生的学号、姓名 (!)
SELECT DISTINCT st.s_id, st.s_name
FROM Student st
JOIN
(
SELECT m.s_id
FROM (
SELECT s_id, COUNT(c_id) cnt
FROM Score
GROUP BY s_id
) m
WHERE m.cnt != (SELECT COUNT(c_id) FROM Course)
) n
ON st.s_id = n.s_id
11、查询至少有一门课与学号为“01”的学生所学课程相同的学生的学号和姓名
|