SQL 经典练习题 + 答案

CREATE SCHEMA exercise;
 
USE exercise;
 
CREATE TABLE Student
(
    stu_id             VARCHAR(3) NOT NULL, 
    stu_name         VARCHAR(4) NOT NULL,
    stu_sex         VARCHAR(2) NOT NULL, 
        stu_birthday            DATETIME NOT NULL,
    stu_class         VARCHAR(5)
);
 
CREATE TABLE Course
(
    cou_id         VARCHAR(5) NOT NULL, 
    cou_name     VARCHAR(10) NOT NULL, 
    tea_id         VARCHAR(10) NOT NULL
);
 
CREATE TABLE Score 
(
    stu_id         VARCHAR(3) NOT NULL, 
    cou_id         VARCHAR(5) NOT NULL, 
    sco_degree     NUMERIC(10, 1) NOT NULL
);
 
CREATE TABLE Teacher
(
    tea_id          VARCHAR(3) NOT NULL,
    tea_name        VARCHAR(4) NOT NULL,
    tea_sex         VARCHAR(2) NOT NULL,
    tea_birthday    DATETIME NOT NULL,
    tea_prof        VARCHAR(6), 
    tea_depart      VARCHAR(10) NOT NULL
);
 
INSERT INTO Student (stu_id, stu_name, stu_sex, stu_birthday, stu_class)
VALUES (108, '曾华', '男', '1977-09-01', 95033);
INSERT INTO Student (stu_id, stu_name, stu_sex, stu_birthday, stu_class)
VALUES (105, '匡明', '男', '1975-10-02', 95031);
INSERT INTO Student (stu_id, stu_name, stu_sex, stu_birthday, stu_class)
VALUES (107, '王丽', '女', '1976-01-23', 95033);
INSERT INTO Student (stu_id, stu_name, stu_sex, stu_birthday, stu_class)
VALUES (101, '李军', '男', '1976-02-20', 95033);
INSERT INTO Student (stu_id, stu_name, stu_sex, stu_birthday, stu_class)
VALUES (109, '王芳', '女', '1975-02-10', 95031);
INSERT INTO Student (stu_id, stu_name, stu_sex, stu_birthday, stu_class)
VALUES (103, '陆君', '男', '1974-06-03', 95031);
 
INSERT INTO Course (cou_id, cou_name, tea_id)
VALUES ('3-105', '计算机导论', 825);
INSERT INTO Course (cou_id, cou_name, tea_id)
VALUES ('3-245', '操作系统', 804);
INSERT INTO Course (cou_id, cou_name, tea_id)
VALUES ('6-166', '数据电路', 856);
INSERT INTO Course (cou_id, cou_name, tea_id)
VALUES ('9-888', '高等数学', 100);
 
INSERT INTO Score (stu_id, cou_id, sco_degree)
VALUES (103, '3-245', 86); 
INSERT INTO Score (stu_id, cou_id, sco_degree)
VALUES (105, '3-245', 75);
INSERT INTO Score (stu_id, cou_id, sco_degree)
VALUES (109, '3-245', 68);
INSERT INTO Score (stu_id, cou_id, sco_degree)
VALUES (103, '3-105', 92);
INSERT INTO Score (stu_id, cou_id, sco_degree)
VALUES (105, '3-105', 88);
INSERT INTO Score (stu_id, cou_id, sco_degree)
VALUES (109, '3-105', 76);
INSERT INTO Score (stu_id, cou_id, sco_degree)
VALUES (101, '3-105', 64);
INSERT INTO Score (stu_id, cou_id, sco_degree)
VALUES (107, '3-105', 91);
INSERT INTO Score (stu_id, cou_id, sco_degree)
VALUES (108, '3-105', 78);
INSERT INTO Score (stu_id, cou_id, sco_degree)
VALUES (101, '6-166', 85);
INSERT INTO Score (stu_id, cou_id, sco_degree)
VALUES (107, '6-166', 79);
INSERT INTO Score (stu_id, cou_id, sco_degree)
VALUES (108, '6-166', 81);
 
INSERT INTO Teacher (tea_id, tea_name, tea_sex, tea_birthday, tea_prof, tea_depart)
VALUES (804, '李诚', '男', '1958-12-02', '副教授', '计算机系'); 
INSERT INTO Teacher (tea_id, tea_name, tea_sex, tea_birthday, tea_prof, tea_depart)
VALUES (856, '张旭', '男', '1969-03-12', '讲师', '电子工程系');
INSERT INTO Teacher (tea_id, tea_name, tea_sex, tea_birthday, tea_prof, tea_depart)
VALUES (825, '王萍', '女', '1972-05-05', '助教', '计算机系');
INSERT INTO Teacher (tea_id, tea_name, tea_sex, tea_birthday, tea_prof, tea_depart)
VALUES (831, '刘冰', '女', '1977-08-14', '助教', '电子工程系');

Student表如下:

Course表:

Score表:

Teacher表:

题目由易到难,题目及答案如下:
 
USE exercise;
 
-- 1、 查询student表中的所有记录的stu_name、stu_sex和stu_class列。
SELECT student.stu_name, student.stu_sex, student.stu_class
FROM student;
 
-- 2、 查询教师所有的单位即不重复的tea_depart列。
SELECT DISTINCT teacher.tea_depart
FROM teacher; 
 
-- 3、 查询student表的所有记录。
SELECT * 
FROM student;
 
-- 4、 查询score表中成绩在60到80之间的所有记录。
SELECT *
FROM score
WHERE sco_degree BETWEEN 60 AND 80;
 
-- 5、 查询score表中成绩为85,86或88的记录。
-- 解法1
SELECT *
FROM score
WHERE sco_degree IN (85, 86, 88);
-- 解法2
SELECT *
FROM score
WHERE sco_degree = 85 OR sco_degree = 86 OR sco_degree = 88;
 
-- 6、 查询student表中“95031”班或性别为“女”的同学记录。
SELECT * 
FROM student
WHERE stu_class = '95031' 
    OR stu_sex = '女';
 
-- 7、 以stu_class降序查询student表的所有记录。
SELECT * FROM student
ORDER BY student.stu_class DESC;
 
-- 8、 以stu_id升序、sco_degree降序查询score表的所有记录。
SELECT * 
FROM score
ORDER BY stu_id ASC, sco_degree DESC;
 
-- 9、 查询“95031”班的学生人数。
SELECT COUNT(*) AS class_95031_num 
FROM student
WHERE stu_class = '95031';
 
-- 10、查询score表中的最高分的学生学号和课程号。
-- 解法1
SELECT stu_id, cou_id
FROM score
WHERE sco_degree = (SELECT MAX(sco_degree)
                    FROM score);
-- 解法2
SELECT stu_id, cou_id
FROM score
ORDER BY sco_degree DESC LIMIT 1;
                        
-- 11、查询‘3-105’号课程的平均分。
SELECT AVG(sco_degree) AS avg_degree
FROM score
WHERE cou_id = '3-105';
 
-- 12、查询score表中至少有5名学生选修的并以3开头的课程的平均分数。
SELECT AVG(sco_degree) AS degree_avg, cou_id
FROM score
WHERE cou_id LIKE '3%'
GROUP BY cou_id
HAVING COUNT(*) >= 5;
 
-- 13、查询最低分大于70,最高分小于90的stu_id列。
SELECT stu_id -- 或者 SELECT DISTINCT stu_id
FROM score
GROUP BY stu_id
HAVING MIN(sco_degree) > 70 
    AND MAX(sco_degree) < 90;
    
-- 14、查询所有学生的stu_name、cou_id和sco_egree列。
SELECT student.stu_name, score.cou_id, score.sco_degree
FROM student INNER JOIN 
score ON student.stu_id = score.stu_id;
 
-- 15、查询所有学生的stu_id、cou_name和sco_degree列。
SELECT score.stu_id, course.cou_name, score.sco_degree
FROM score INNER JOIN 
course ON score.cou_id = course.cou_id;
 
-- 16、查询所有学生的stu_name、cou_name和sco_degree列。
SELECT student.stu_name, course.cou_name, score.sco_degree
FROM student, course, score
WHERE student.stu_id = score.stu_id 
    AND course.cou_id = score.cou_id; 
 
-- 17、查询“95033”班所选课程的平均分。
SELECT student.stu_class, AVG(score.sco_degree) AS degree_avg
FROM student INNER JOIN 
score ON student.stu_id = score.stu_id
WHERE student.stu_class = '95031';
 
-- 18、假设使用如下命令建立了一个grade表, 现查询所有同学的stu_name、cou_name和gra_rank列。
/*-- 去除注释进行创建
CREATE TABLE grade
(
    gra_low   NUMERIC(3, 0),
    gra_upp   NUMERIC(3),
    gra_rank  CHAR(1)
 );
 
INSERT INTO grade VALUES(90, 100, 'A');
INSERT INTO grade VALUES(80, 89, 'B');
INSERT INTO grade VALUES(70, 79, 'C');
INSERT INTO grade VALUES(60, 69, 'D');
INSERT INTO grade VALUES(0, 59, 'E');
COMMIT;
*/
SELECT student.stu_name, course.cou_name, grade.gra_rank
FROM score, student, course, grade
WHERE student.stu_id = score.stu_id
    AND score.cou_id = course.cou_id
    AND (score.sco_degree BETWEEN grade.gra_low AND grade.gra_upp);
   
-- 19、查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录
-- 解法1
SELECT *
FROM score
WHERE score.cou_id = '3-105'
    AND score.sco_degree > (SELECT sco_degree
                FROM score
                WHERE score.cou_id = '3-105' AND score.stu_id = '109');
-- 解法2
SELECT s1.*
FROM score AS s1 INNER JOIN 
score AS s2 ON s1.cou_id = s2.cou_id
WHERE s1.cou_id = '3-105'
    AND s1.sco_degree > s2.sco_degree
    AND s2.cou_id = '3-105' AND s2.stu_id = '109';
 
-- 20、查询score中选学一门以上课程的同学中分数为非最高分成绩的记录。
SELECT score.*
FROM score
WHERE score.sco_degree < (SELECT MAX(score.sco_degree) FROM score)
GROUP BY score.stu_id
HAVING COUNT(*) > 1;
 
-- 21、查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。
-- 解法1
SELECT s1.*
FROM score AS s1 INNER JOIN 
score AS s2 ON s1.cou_id = s2.cou_id
WHERE s1.cou_id = '3-105'
    AND s1.sco_degree > s2.sco_degree
    AND s2.cou_id = '3-105' AND s2.stu_id = '109';
-- 解法2
SELECT *
FROM score
WHERE score.sco_degree > (SELECT score.sco_degree
                          FROM score
                          WHERE score.stu_id = '109' AND score.cou_id = '3-105');
 
-- 22、查询和学号为‘108’的同学同年出生的所有学生的stu_id、stu_name和stu_birthday列。
-- 解法1
SELECT s1.stu_id, s1.stu_name, s1.stu_birthday
FROM student AS s1 INNER JOIN 
student AS s2 ON s1.stu_id = s2.stu_id
WHERE YEAR(s1.stu_birthday) = YEAR(s2.stu_birthday) AND s2.stu_id = '108';
 
-- 解法2
SELECT student.stu_id, student.stu_name, student.stu_birthday
FROM student
WHERE YEAR(student.stu_birthday) = (SELECT YEAR(student.stu_birthday)
                                    FROM student
                                    WHERE student.stu_id = '108');
                            
-- 23、查询“张旭“教师任课的学生成绩。   
SELECT score.sco_degree, score.stu_id
FROM score, course, teacher
WHERE score.cou_id = course.cou_id 
    AND course.tea_id = teacher.tea_id
    AND teacher.tea_name = '张旭';
 
-- 24、查询选修某课程的同学人数多于5人的教师姓名。
SELECT teacher.tea_name
FROM teacher, course, score
WHERE teacher.tea_id = course.tea_id
    AND course.cou_id = score.cou_id
GROUP BY teacher.tea_name
HAVING COUNT(*) > 5;
 
-- 25、查询95033班和95031班全体学生的记录。
SELECT *
FROM student 
WHERE stu_class IN ('95031', '95033');
 
-- 26、查询存在有85分以上成绩的课程cou_id。
SELECT score.cou_id
FROM score
GROUP BY score.cou_id
HAVING MAX(sco_degree) > 85;
 
-- 27、查询出“计算机系“教师所教课程的成绩表。
SELECT student.stu_name, score.sco_degree, course.cou_name, teacher.tea_name
FROM score, teacher, course, student
WHERE student.stu_id = score.stu_id
    AND score.cou_id = course.cou_id
    AND course.tea_id = teacher.tea_id
    AND teacher.tea_depart = '计算机系';
 
-- 28、查询“计算机系”与“电子工程系“不同tea_prof的教师的tea_name和tea_prof。
SELECT tea_name, tea_prof
FROM teacher
WHERE tea_depart = '计算机系'
    AND tea_prof NOT IN (SELECT tea_prof 
                         FROM teacher
                         WHERE tea_depart = '电子工程系');
 
-- 29、查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的cou_id、stu_id和sco_degree,并按sco_degree从高到低次序排序。
SELECT score.cou_id, score.stu_id, score.sco_degree
FROM score
WHERE cou_id = '3-105'
    AND sco_degree > ANY(SELECT sco_degree
                         FROM score
                         WHERE cou_id = '3-245')
                         ORDER BY score.sco_degree DESC;
 
-- 30、查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的cou_id、stu_id和sco_degree。
SELECT cou_id, stu_id, sco_degree
FROM score
WHERE cou_id = '3-105'
    AND sco_degree > ALL(SELECT sco_degree
                         FROM score
                         WHERE cou_id = '3-245')
                         ORDER BY sco_degree DESC;
                         
-- 31、查询所有教师和同学的name、sex和birthday。
SELECT tea_name AS name, tea_sex AS sex,  tea_birthday AS birthday
FROM teacher
UNION 
SELECT stu_name, stu_sex, stu_birthday
FROM student;
 
-- 32、查询所有“女”教师和“女”同学的name、sex和birthday。
SELECT tea_name AS name, tea_sex AS sex,  tea_birthday AS birthday
FROM teacher
WHERE tea_sex = '女'
UNION 
SELECT stu_name, stu_sex, stu_birthday
FROM student
WHERE stu_sex = '女';
 
-- 33**、查询成绩比该课程平均成绩低的同学的成绩表。
SELECT cou_id, sco_degree, stu_id
FROM score AS A
WHERE sco_degree < (SELECT AVG(sco_degree)
                    FROM score AS B
                    WHERE A.cou_id = B.cou_id);
 
-- 34、查询所有任课教师的tea_name和tea_depart。
SELECT tea_name, tea_depart
FROM teacher AS T
WHERE T.tea_id IN (SELECT C.tea_id
                   FROM course AS C);
 
-- 35、查询所有未讲课的教师的tea_name和tad_depart。
SELECT tea_name, tea_depart
FROM teacher AS T
WHERE T.tea_id NOT IN (SELECT C.tea_id
                       FROM course AS C);
                        
-- 36、查询至少有2名男生的班号。
SELECT stu_class
FROM student
WHERE stu_sex = '男'
GROUP BY stu_class
HAVING COUNT(*) >= 2;
 
-- 37、查询Student表中不姓“王”的同学记录。
SELECT *
FROM student
WHERE stu_name NOT LIKE '王%';
 
-- 38、查询Student表中每个学生的姓名和年龄。
SELECT stu_name, YEAR(NOW()) - YEAR(stu_birthday) AS stu_age
FROM student;
 
-- 39、查询Student表中最大和最小的stu_birthday日期值
SELECT MAX(stu_birthday) AS max_birthday, MIN(stu_birthday) AS min_birthday
FROM student;
 
-- 40、以班号和年龄从大到小的顺序查询Student表中的全部记录。
SELECT student.*, YEAR(NOW()) - YEAR(stu_birthday) AS stu_age
FROM student
ORDER BY stu_class DESC, stu_age DESC;
 
-- 41、查询“男”教师及其所上的课程。 
SELECT course.*
FROM course INNER JOIN
teacher ON teacher.tea_id = course.tea_id
WHERE teacher.tea_sex = '男';
 
-- 42、查询最高分同学的stu_id、cou_id和sco_degree列。
SELECT S1.*
FROM score AS S1
WHERE S1.sco_degree = (SELECT MAX(S2.sco_degree)
               FROM score AS S2);
                       
-- 43、查询和“李军”同性别的所有同学的stu_name.
SELECT S1.stu_name
FROM student AS S1
WHERE S1.stu_sex = (SELECT S2.stu_sex
                    FROM student AS S2
                    WHERE s2.stu_name = '李军');
                    
-- 44、查询和“李军”同性别并同班的同学stu_name.
SELECT S1.stu_name
FROM student AS S1
WHERE S1.stu_sex = (SELECT S2.stu_sex
                    FROM student AS S2
                    WHERE s2.stu_name = '李军')
                    AND S1.stu_class = (SELECT S3.stu_class
                    FROM student AS S3
                    WHERE S3.stu_name = '李军');
                                        
-- 45、查询所有选修“计算机导论”课程的“男”同学的成绩表
SELECT score.*
FROM score, student, course
WHERE score.stu_id = student.stu_id
    AND score.cou_id = course.cou_id
    AND course.cou_name = '计算机导论'
    AND student.stu_sex = '男';
原文地址:https://www.cnblogs.com/qingmuchuanqi48/p/14280245.html