SQL 經典練習題 + 答案
阿新 • • 發佈:2021-01-15
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 = '男';