創(chuàng)建表
請(qǐng)創(chuàng)建如下表,并創(chuàng)建相關(guān)約束。如圖1
操作表中的內(nèi)容
1、自行創(chuàng)建測(cè)試數(shù)據(jù);
2、查詢學(xué)生總?cè)藬?shù);
3、查詢“生物”課程和“物理”課程成績(jī)都及格的學(xué)生id和姓名;
4、查詢每個(gè)年級(jí)的班級(jí)數(shù),取出班級(jí)數(shù)最多的前三個(gè)年級(jí);
5、查詢平均成績(jī)最高和最低的學(xué)生的id和姓名以及平均成績(jī);
6、查詢每個(gè)年級(jí)的學(xué)生人數(shù);
7、查詢每位學(xué)生的學(xué)號(hào),姓名,選課數(shù),平均成績(jī);
8、查詢學(xué)生編號(hào)為“2”的學(xué)生的姓名、該學(xué)生成績(jī)最高的課程名、成績(jī)最低的課程名及分?jǐn)?shù);
9、查詢姓“李”的老師的個(gè)數(shù)和所帶班級(jí)數(shù);
10、查詢班級(jí)數(shù)小于5的年級(jí)id和年級(jí)名;
11、查詢班級(jí)信息,包括班級(jí)id、班級(jí)名稱(chēng)、年級(jí)、年級(jí)級(jí)別(12為低年級(jí),34為中年級(jí),56為高年級(jí)),示例結(jié)果如下;
12、查詢學(xué)過(guò)“張三”老師2門(mén)課以上的同學(xué)的學(xué)號(hào)、姓名;
13、查詢教授課程超過(guò)2門(mén)的老師的id和姓名;
14、查詢學(xué)過(guò)編號(hào)“1”課程和編號(hào)“2”課程的同學(xué)的學(xué)號(hào)、姓名;
15、查詢沒(méi)有帶過(guò)高年級(jí)的老師id和姓名;
16、查詢學(xué)過(guò)“張三”老師所教的所有課的同學(xué)的學(xué)號(hào)、姓名;
17、查詢帶過(guò)超過(guò)2個(gè)班級(jí)的老師的id和姓名;
18、查詢課程編號(hào)“2”的成績(jī)比課程編號(hào)“1”課程低的所有同學(xué)的學(xué)號(hào)、姓名;
19、查詢所帶班級(jí)數(shù)最多的老師id和姓名;
20、查詢有課程成績(jī)小于60分的同學(xué)的學(xué)號(hào)、姓名;
21、查詢沒(méi)有學(xué)全所有課的同學(xué)的學(xué)號(hào)、姓名;
22、查詢至少有一門(mén)課與學(xué)號(hào)為“1”的同學(xué)所學(xué)相同的同學(xué)的學(xué)號(hào)和姓名;
23、查詢至少學(xué)過(guò)學(xué)號(hào)為“1”同學(xué)所選課程中任意一門(mén)課的其他同學(xué)學(xué)號(hào)和姓名;
24、查詢和“2”號(hào)同學(xué)學(xué)習(xí)的課程完全相同的其他同學(xué)的學(xué)號(hào)和姓名;
25、刪除學(xué)習(xí)“張三”老師課的score表記錄;
26、向score表中插入一些記錄,這些記錄要求符合以下條件:①?zèng)]有上過(guò)編號(hào)“2”課程的同學(xué)學(xué)號(hào);②插入“2”號(hào)課程的平均成績(jī);
27、按平均成績(jī)從低到高顯示所有學(xué)生的“語(yǔ)文”、“數(shù)學(xué)”、“英語(yǔ)”三門(mén)的課程成績(jī),按如下形式顯示: 學(xué)生ID,語(yǔ)文,數(shù)學(xué),英語(yǔ),有效課程數(shù),有效平均分;
28、查詢各科成績(jī)最高和最低的分:以如下形式顯示:課程ID,最高分,最低分;
29、按各科平均成績(jī)從低到高和及格率的百分?jǐn)?shù)從高到低順序;
30、課程平均分從高到低顯示(現(xiàn)實(shí)任課老師);
31、查詢各科成績(jī)前三名的記錄(不考慮成績(jī)并列情況)
32、查詢每門(mén)課程被選修的學(xué)生數(shù);
33、查詢選修了2門(mén)以上課程的全部學(xué)生的學(xué)號(hào)和姓名;
34、查詢男生、女生的人數(shù),按倒序排列;
35、查詢姓“張”的學(xué)生名單;
36、查詢同名同姓學(xué)生名單,并統(tǒng)計(jì)同名人數(shù);
37、查詢每門(mén)課程的平均成績(jī),結(jié)果按平均成績(jī)升序排列,平均成績(jī)相同時(shí),按課程號(hào)降序排列;
38、查詢課程名稱(chēng)為“數(shù)學(xué)”,且分?jǐn)?shù)低于60的學(xué)生姓名和分?jǐn)?shù);
39、查詢課程鏈接描述編號(hào)為“3”且課程成績(jī)?cè)?0分以上的學(xué)生的學(xué)號(hào)和姓名;
40、求選修了課程的學(xué)生人數(shù)
41、查詢選修“王五”老師所授課程的學(xué)生中,成績(jī)最高和最低的學(xué)生姓名及其成績(jī);
42、查詢各個(gè)課程及相應(yīng)的選修人數(shù);
43、查詢不同課程但成績(jī)相同的學(xué)生的學(xué)號(hào)、課程號(hào)、學(xué)生成績(jī);
44、查詢每門(mén)課程成績(jī)最好的前兩名學(xué)生id和姓名;
45、檢索至少選修兩門(mén)課程的學(xué)生學(xué)號(hào);
46、查詢沒(méi)有學(xué)生選修的課程的課程號(hào)和課程名;
47、查詢沒(méi)帶過(guò)任何班級(jí)的老師id和姓名;
48、查詢有兩門(mén)以上課程超過(guò)80分的學(xué)生id及其平均成績(jī);
49、檢索“3”課程分?jǐn)?shù)小于60,按分?jǐn)?shù)降序排列的同學(xué)學(xué)號(hào);
50、刪除編號(hào)為“2”的同學(xué)的“1”課程的成績(jī);
51、查詢同時(shí)選修了物理課和生物課的學(xué)生id和姓名;
可以從github直接下載sql文件
創(chuàng)建表# 自行創(chuàng)建測(cè)試數(shù)據(jù) -- create database school charset utf8; use `school`; # 創(chuàng)建班級(jí)表 DROP TABLE IF EXISTS `class`; create table `class`( `cid` int unsigned not null auto_increment , `caption` varchar(50) not null unique , `grade_id` int unsigned not null , PRIMARY KEY(`cid`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; DESC `class`; # 創(chuàng)建學(xué)生表 DROP TABLE IF EXISTS `student`; CREATE TABLE student( `sid` INT UNSIGNED NOT NULL AUTO_INCREMENT , `sname` VARCHAR(50) NOT NULL , `gender` ENUM("男","女") NOT NULL DEFAULT "男", `class_id` INT UNSIGNED NOT NULL, PRIMARY KEY (`sid`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; DESC `student`; # 創(chuàng)建老師表 DROP TABLE IF EXISTS `teacher`; CREATE TABLE `teacher`( tid INT UNSIGNED NOT NULL AUTO_INCREMENT , tname VARCHAR(50) NOT NULL , PRIMARY KEY (tid) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; DESC `teacher`; # 創(chuàng)建課程表 DROP TABLE IF EXISTS `course`; CREATE TABLE `course`( `cid` INT UNSIGNED NOT NULL AUTO_INCREMENT , `cname` VARCHAR(50) NOT NULL , `teacher_id` INT UNSIGNED NOT NULL, PRIMARY KEY (`cid`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; DESC course; # 創(chuàng)建成績(jī)表 DROP TABLE IF EXISTS `score`; CREATE TABLE `score`( `sid` INT UNSIGNED NOT NULL AUTO_INCREMENT, `student_id` INT UNSIGNED NOT NULL, `course_id` INT UNSIGNED NOT NULL, `score` INT UNSIGNED NOT NULL DEFAULT 0, PRIMARY KEY (`sid`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; DESC `score`; # 創(chuàng)建年紀(jì)表 DROP TABLE IF EXISTS `class_grade`; CREATE TABLE `class_grade`( `gid` INT UNSIGNED NOT NULL AUTO_INCREMENT, `gname` VARCHAR(50) NOT NULL, PRIMARY KEY (`gid`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; DESC `class_grade`; # 班級(jí)班級(jí)任職表 DROP TABLE IF EXISTS `teach2cls`; CREATE TABLE `teach2cls`( `tcid` INT UNSIGNED NOT NULL AUTO_INCREMENT, `tid` INT UNSIGNED NOT NULL, `cid` INT UNSIGNED NOT NULL, PRIMARY KEY (`tcid`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; DESC `class_grade`; -- 插入數(shù)據(jù) -- 插入班級(jí)表 INSERT INTO `class`(`caption`, `grade_id`) VALUES ("一年一班", 1), ("二年一班", 2), ("三年二班", 3), ("一年二班", 1), ("二年二班", 2), ("三年一班", 3), ("四年一班", 4), ("五年一班", 5), ("六年一班", 6); -- 插入學(xué)生表 INSERT INTO `student`(`sname`, `gender`, `class_id`) VALUES ("喬丹", "女", 1), ("艾費(fèi)森","女",1), ("科比","男",2), ("亨利", "男", 2), ("alex", "女", 3), ("jenry", "女", 4), ("tom", "男", 3), ("mike", "男", 5), ("sam", "男", 8), ("den", "男", 9), ("張三", "男", 2), ("張五", "男", 4), ("jenry", "男", 1); -- 插入老師表 INSERT INTO `teacher`(`tname`) VALUES ("張三"), ("李四"), ("王五"), ("賈六"), ("孫七"); -- 插入課程表 INSERT INTO `course`(`cname`, `teacher_id`) VALUES ("生物", 1), ("體育", 1), ("物理", 2), ("語(yǔ)文", 5), ("數(shù)學(xué)", 3), ("英語(yǔ)", 4); -- 插入成績(jī)表 INSERT INTO `score`(`student_id`, `course_id`, `score`) VALUES (1, 1, 60), (1, 2, 59), (2, 2, 99), (2, 3, 80), (2, 4, 50), (3, 1, 100), (3, 6, 89), (4, 5, 40), (6, 3, 70), (10, 1, 80), (7, 4, 20), (11, 6, 71), (11, 5, 100), (13, 5, 80), (1, 3, 70), (2, 1, 90); -- 插入年級(jí)表 INSERT INTO `class_grade`(`gname`) VALUES ("一年級(jí)"), ("二年級(jí)"), ("三年級(jí)"), ("四年級(jí)"), ("五年級(jí)"), ("六年級(jí)"); -- 插入班級(jí)任職表 INSERT INTO `teach2cls`(`tid`, `cid`) VALUES (1, 1), (1, 2), (2, 1), (3, 2), (5, 1), (4, 3), (1, 3), (2, 4), (3, 6), (5, 7), (5, 8), (4, 5);50題
-- 1.查找學(xué)生總?cè)藬?shù) SELECT COUNT(sid) as "student_c" FROM student; -- 2、查詢“生物”課程和“物理”課程成績(jī)都及格的學(xué)生id和姓名; SELECT student.sid, student.sname, course.cname FROM (student INNER JOIN score on student.sid = score.student_id) INNER JOIN course on score.course_id = course.cid WHERE cname = "生物" or cname = "物理" and score >= 60 ORDER BY sid; -- 3、查詢每個(gè)年級(jí)的班級(jí)數(shù),取出班級(jí)數(shù)最多的前三個(gè)年級(jí); SELECT class_grade.gname, COUNT(class.cid) AS "class_c" FROM class LEFT JOIN class_grade ON class.grade_id = class_grade.gid GROUP BY class_grade.gid ORDER BY class_c DESC LIMIT 3; -- 4、查詢平均成績(jī)最高和最低的學(xué)生的id和姓名以及平均成績(jī); SELECT student.sid, student.sname, AVG(score.score) FROM score INNER JOIN student ON score.student_id = student.sid GROUP BY score.student_id HAVING AVG(score.score) IN ( (SELECT AVG(score.score) AS "score_a" FROM student INNER JOIN score ON student.sid = score.student_id GROUP BY student.sid ORDER BY score_a DESC LIMIT 1), (SELECT AVG(score.score) AS "score_a" FROM student INNER JOIN score ON student.sid = score.student_id GROUP BY student.sid ORDER BY score_a LIMIT 1)); -- 5、查詢每個(gè)年級(jí)的學(xué)生人數(shù); SELECT class_grade.gname, COUNT(student.sid) AS "student_c" FROM (class RIGHT JOIN class_grade ON class.grade_id = class_grade.gid) LEFT JOIN student ON class.cid = student.class_id GROUP BY gname; -- 6、查詢每位學(xué)生的學(xué)號(hào),姓名,選課數(shù),平均成績(jī); SELECT student.sid, sname, COUNT(course_id) AS "course_c", IFNULL(AVG(score),0) AS "score_a" FROM student LEFT JOIN score ON student.sid = score.student_id GROUP BY sname ORDER BY sid; -- 7、查詢學(xué)生編號(hào)為“2”的學(xué)生的姓名、該學(xué)生成績(jī)最高的課程名、成績(jī)最低的課程名及分?jǐn)?shù); SELECT course.cname, score.score FROM course INNER JOIN score ON course.cid = score.course_id WHERE score IN ( (SELECT MAX(score) FROM score WHERE student_id = 2), (SELECT MIN(score) FROM score WHERE student_id = 2)); -- 8、查詢姓“李”的老師的個(gè)數(shù)和所帶班級(jí)數(shù); SELECT COUNT(DISTINCT teacher.tname) AS "teacher_c",COUNT(teach2cls.cid) AS "class_c" FROM teacher LEFT JOIN teach2cls ON teacher.tid = teach2cls.tid WHERE teacher.tname LIKE "李%"; -- 9、查詢班級(jí)數(shù)小于5的年級(jí)id和年級(jí)名; SELECT gid, class_grade.gname FROM class RIGHT JOIN class_grade ON class.grade_id = class_grade.gid GROUP BY class_grade.gname HAVING COUNT(caption) < 5 ORDER BY gid; -- 10、查詢班級(jí)信息,包括班級(jí)id、班級(jí)名稱(chēng)、年級(jí)、年級(jí)級(jí)別(12為低年級(jí),34為中年級(jí),56為高年級(jí)),示例結(jié)果如下; SELECT class.cid, class.caption, class_grade.gname, CASE WHEN class_grade.gid BETWEEN 1 AND 2 THEN "低年級(jí)" WHEN class_grade.gid BETWEEN 3 AND 4 THEN "中年級(jí)" WHEN class_grade.gid BETWEEN 5 AND 6 THEN "高年級(jí)" END AS "level" FROM class INNER JOIN class_grade ON class.grade_id = class_grade.gid; -- 11、查詢學(xué)過(guò)“張三”老師2門(mén)課以上的同學(xué)的學(xué)號(hào)、姓名; SELECT sid, sname FROM student WHERE sid IN (SELECT student_id FROM score WHERE course_id IN (SELECT cid FROM course WHERE teacher_id IN (SELECT tid FROM teacher WHERE tname = "張三" ) ) GROUP BY student_id HAVING COUNT(course_id) > 2); -- 12、查詢教授課程超過(guò)2門(mén)的老師的id和姓名; SELECT teacher.tid, teacher.tname FROM teacher INNER JOIN course ON teacher.tid = course.teacher_id GROUP BY teacher.tid HAVING COUNT(course.cid) > 2; -- 13、查詢學(xué)過(guò)編號(hào)“1”課程和編號(hào)“2”課程的同學(xué)的學(xué)號(hào)、姓名; SELECT student.sid, student.sname FROM student INNER JOIN score ON student.sid = score.student_id WHERE score.course_id IN (1,2) GROUP BY sname HAVING COUNT(student.sid) = 2; -- 14、查詢沒(méi)有帶過(guò)高年級(jí)的老師id和姓名; SELECT tid, tname FROM teacher WHERE tid IN (SELECT tid FROM teach2cls WHERE cid IN (SELECT cid FROM (SELECT class.cid, class.caption, class_grade.gname, CASE WHEN class_grade.gid BETWEEN 1 AND 2 THEN "低年級(jí)" WHEN class_grade.gid BETWEEN 3 AND 4 THEN "中年級(jí)" WHEN class_grade.gid BETWEEN 5 AND 6 THEN "高年級(jí)" END AS "class_level" FROM class INNER JOIN class_grade ON class.grade_id = class_grade.gid ) AS t1 WHERE t1.class_level != "高年級(jí)") ); -- 15、查詢學(xué)過(guò)“張三”老師所教的所有課的同學(xué)的學(xué)號(hào)、姓名; SELECT sid, sname FROM student WHERE sid IN (SELECT student_id FROM score WHERE course_id IN (SELECT cid FROM teacher INNER JOIN course ON teacher.tid = course.teacher_id WHERE teacher.tname = "張三") ); -- 16、查詢帶過(guò)超過(guò)2個(gè)班級(jí)的老師的id和姓名; SELECT teacher.tid, teacher.tname FROM teacher INNER JOIN teach2cls ON teacher.tid = teach2cls.tid GROUP BY teacher.tid HAVING COUNT(cid) > 2; -- 17、查詢課程編號(hào)“2”的成績(jī)比課程編號(hào)“1”課程低的所有同學(xué)的學(xué)號(hào)、姓名; SELECT sid, sname FROM student WHERE sid IN (SELECT t1.student_id FROM (SELECT student_id,score FROM score WHERE course_id = 2 ) AS t1, (SELECT student_id,score FROM score WHERE course_id = 1 ) AS t2 WHERE t1.score < t2.score); -- 18、查詢所帶班級(jí)數(shù)最多的老師id和姓名; SELECT tid, tname FROM teacher WHERE tid IN (SELECT tid FROM teach2cls GROUP BY tid HAVING COUNT(cid) = (SELECT MAX(t1.count) FROM (SELECT tid, COUNT(cid) AS "count" FROM teach2cls GROUP BY tid) AS t1)); -- 19、查詢有課程成績(jī)小于60分的同學(xué)的學(xué)號(hào)、姓名; SELECT sid, sname FROM student INNER JOIN score ON student.sid = score.student_id WHERE score.score < 60; -- 20、查詢沒(méi)有學(xué)全所有課的同學(xué)的學(xué)號(hào)、姓名; SELECT student.sid, student.sname FROM score INNER JOIN student ON score.student_id = student.sid GROUP BY student_id HAVING COUNT(course_id) >= (SELECT COUNT(cid) FROM course); -- 21、查詢至少有一門(mén)課與學(xué)號(hào)為“1”的同學(xué)所學(xué)相同的同學(xué)的學(xué)號(hào)和姓名; SELECT student.sid, student.sname FROM score INNER JOIN student ON score.student_id = student.sid WHERE course_id IN (SELECT course_id FROM score WHERE score.student_id = 1) GROUP BY student_id -- 22、查詢至少學(xué)過(guò)學(xué)號(hào)為“1”同學(xué)所選課程中任意一門(mén)課的其他同學(xué)學(xué)號(hào)和姓名; SELECT student.sid, student.sname FROM score INNER JOIN student ON score.student_id = student.sid WHERE course_id IN (SELECT course_id FROM score WHERE score.student_id = 1) GROUP BY student_id HAVING student_id != 1; # 排除學(xué)號(hào)為1的同學(xué) -- 23、查詢和“2”號(hào)同學(xué)學(xué)習(xí)的課程完全相同的其他同學(xué)的學(xué)號(hào)和姓名; SELECT student.sid, student.sname FROM score INNER JOIN student ON score.student_id = student.sid WHERE student_id != 2 GROUP BY student_id HAVING COUNT(course_id) = (SELECT COUNT(course_id) FROM score WHERE student_id = 2) -- 24、刪除學(xué)習(xí)“張三”老師課的score表記錄; DELETE FROM score WHERE course_id IN (SELECT cid FROM teacher INNER JOIN course ON teacher.tid = course.teacher_id WHERE teacher.tname = "張三"); -- 25、向score表中插入一些記錄,這些記錄要求符合以下條件:①?zèng)]有上過(guò)編號(hào)“2”課程的同學(xué)學(xué)號(hào);②插入“2”號(hào)課程的平均成績(jī); INSERT INTO score(student_id, course_id, score) SELECT t1.sid, t1.cid, t2.avg_score FROM (SELECT student.sid, "2" AS cid FROM student LEFT JOIN score ON student.sid = score.student_id WHERE course_id != 2 OR ISNULL(course_id)) AS t1, (SELECT IFNULL(AVG(score), 0) as "avg_score" FROM score WHERE course_id = "2") AS t2 ORDER BY sid -- 26、按平均成績(jī)從低到高顯示所有學(xué)生的“語(yǔ)文”、“數(shù)學(xué)”、“英語(yǔ)”三門(mén)的課程成績(jī),按如下形式顯示: 學(xué)生ID,語(yǔ)文,數(shù)學(xué),英語(yǔ),有效課程數(shù),有效平均分; SELECT sc.student_id, (select score.score from score left join course on score.course_id = course.cid where course.cname = "語(yǔ)文" and score.student_id = sc.student_id) as yw, (select score.score from score left join course on score.course_id = course.cid where course.cname = "數(shù)學(xué)" and score.student_id = sc.student_id) as sx, (select score.score from score left join course on score.course_id = course.cid where course.cname = "英語(yǔ)" and score.student_id = sc.student_id) as yy, COUNT(sc.course_id), AVG(sc.score) FROM score AS sc GROUP BY sc.student_id ORDER BY avg(sc.score) ASC; -- 27、查詢各科成績(jī)最高和最低的分:以如下形式顯示:課程ID,最高分,最低分; SELECT course.cid, MAX(score), MIN(score) FROM score RIGHT JOIN course ON score.course_id = course.cid GROUP BY course_id; -- 28、按各科平均成績(jī)從低到高和及格率的百分?jǐn)?shù)從高到低順序; SELECT AVG(score) AS "score_avg", CONCAT((CASE WHEN score.score > 60 THEN 1 ELSE 0 END) * 100, "%") AS "percent" FROM score GROUP BY score.course_id ORDER BY score_avg ASC, percent DESC; -- 29、課程平均分從高到低顯示(現(xiàn)實(shí)任課老師); SELECT course.cname,teacher.tname, AVG(score.score) AS "score_avg" FROM (score RIGHT JOIN course ON score.course_id = course.cid) INNER JOIN teacher ON course.teacher_id = teacher.tid GROUP BY course_id ORDER BY score_avg DESC; -- 30、查詢各科成績(jī)前三名的記錄(不考慮成績(jī)并列情況) SELECT course.cid, course.cname, s2.first_s, s2.second_s, s2.third_s FROM (SELECT course_id, (SELECT score FROM score AS s2 WHERE s1.course_id = s2.course_id ORDER BY score DESC LIMIT 0,1) AS "first_s", (SELECT score FROM score AS s2 WHERE s1.course_id = s2.course_id ORDER BY score DESC LIMIT 1,1) AS "second_s", (SELECT score FROM score AS s2 WHERE s1.course_id = s2.course_id ORDER BY score DESC LIMIT 2,1) AS "third_s" FROM score AS s1 GROUP BY s1.course_id) AS s2 RIGHT JOIN course ON s2.course_id = course.cid; -- 32、查詢每門(mén)課程被選修的學(xué)生數(shù); SELECT course.cid, course.cname, COUNT(score.student_id) AS "student_c" FROM score RIGHT JOIN course ON score.course_id = course.cid GROUP BY course.cid; -- 33、查詢選修了2門(mén)以上課程的全部學(xué)生的學(xué)號(hào)和姓名; SELECT student.sid, student.sname FROM score RIGHT JOIN student ON score.student_id = student.sid GROUP BY student.sid HAVING COUNT(score.course_id) > 2; -- 34、查詢男生、女生的人數(shù),按倒序排列; SELECT gender ,COUNT(gender) AS "gender_c" FROM student GROUP BY gender ORDER BY gender_c DESC; -- 35、查詢姓“張”的學(xué)生名單; SELECT student.sid, student.sname, student.gender, class.caption FROM student LEFT JOIN class ON student.class_id = class.cid WHERE sname LIKE "張%"; -- 36、查詢同名同姓學(xué)生名單,并統(tǒng)計(jì)同名人數(shù); SELECT sname, count(sname) AS "same_name_c" FROM student GROUP BY sname HAVING COUNT(sname) > 1; -- 37、查詢每門(mén)課程的平均成績(jī),結(jié)果按平均成績(jī)升序排列,平均成績(jī)相同時(shí),按課程號(hào)降序排列; SELECT course.cid, course.cname, AVG(score) AS "score_a" FROM score RIGHT JOIN course ON score.course_id = course.cid GROUP BY course_id ORDER BY score_a, cid DESC; -- 38、查詢課程名稱(chēng)為“數(shù)學(xué)”,且分?jǐn)?shù)低于60的學(xué)生姓名和分?jǐn)?shù); SELECT student.sname, score.score FROM (score RIGHT JOIN course ON score.course_id = course.cid) INNER JOIN student ON score.student_id = student.sid WHERE course.cname = "數(shù)學(xué)" AND score.score < 60; -- 39、查詢課程編號(hào)為“3”且課程成績(jī)?cè)?0分以上的學(xué)生的學(xué)號(hào)和姓名; SELECT student.sid, student.sname FROM score INNER JOIN student ON score.student_id = student.sid WHERE course_id = 3 AND score > 80; -- 40、求選修了課程的學(xué)生人數(shù) SELECT course.cid, course.cname, COUNT(student_id) AS "student_c" FROM score INNER JOIN course ON score.course_id = course.cid GROUP BY course_id; -- 41、查詢選修“王五”老師所授課程的學(xué)生中,成績(jī)最高和最低的學(xué)生姓名及其成績(jī); SELECT student.sname, t2.course_id, t2.score, t2.max_score, t2.min_score FROM student INNER JOIN (SELECT score.student_id, score.course_id, score.score, t1.max_score, t1.min_score FROM score, (SELECT course_id, MAX(score) AS "max_score" ,MIN(score) AS "min_score" FROM score WHERE course_id IN ( SELECT cid FROM course INNER JOIN teacher ON course.teacher_id = teacher.tid WHERE teacher.tname = "王五") GROUP BY course_id) as t1 WHERE score.course_id = t1.course_id AND score.score IN (max_score, min_score) ) AS t2 ON student.sid = t2.student_id; -- 42、查詢各個(gè)課程及相應(yīng)的選修人數(shù); SELECT course.cid, course.cname, IFNULL(COUNT(score.student_id), 0) AS "student_c" FROM score RIGHT JOIN course ON score.course_id = course.cid GROUP BY course.cid; -- 43、查詢不同課時(shí)但成績(jī)相同的學(xué)生的學(xué)號(hào)、課程號(hào)、學(xué)生成績(jī); SELECT DISTINCT s1.student_id, s1.course_id, s1.score FROM score as s1, score as s2 WHERE s1.student_id != s2.student_id AND s1.course_id != s2.course_id AND s1.score = s2.score; -- 44、查詢每門(mén)課程成績(jī)最好的前兩名學(xué)生id和姓名; SELECT student.sid, student.sname, t2.score,t2.course_id FROM (SELECT score.student_id, score.course_id, score.score FROM (select s1.course_id, (select s2.score from score as s2 where s1.course_id = s2.course_id order by s2.score desc limit 0,1) as first_score, (select s3.score from score as s3 where s1.course_id = s3.course_id order by s3.score desc limit 1,1) as second_score from score as s1 GROUP BY s1.course_id) AS t1 INNER JOIN score ON t1.course_id = score.course_id WHERE score.score IN (t1.first_score, t1.second_score) ) AS t2 INNER JOIN student ON student.sid = t2.student_id ORDER BY t2.course_id , t2.score DESC; -- 45、檢索至少選修兩門(mén)課程的學(xué)生學(xué)號(hào); SELECT student_id FROM score GROUP BY student_id HAVING COUNT(course_id) >= 2; -- 46、查詢沒(méi)有學(xué)生選修的課程的課程號(hào)和課程名; SELECT course.cid, course.cname FROM score RIGHT JOIN course ON score.course_id = course.cid WHERE score.student_id IS NULL; -- 47、查詢沒(méi)帶過(guò)任何班級(jí)的老師id和姓名; SELECT teacher.tid, teacher.tname FROM teach2cls RIGHT JOIN teacher ON teach2cls.tid = teacher.tid WHERE teacher.tid IS NULL; -- 48、查詢有兩門(mén)以上課程超過(guò)80分的學(xué)生id及其平均成績(jī); SELECT student_id, AVG(score) FROM score WHERE score.score > 80 GROUP BY student_id HAVING COUNT(score.course_id) >= 2; -- 49、檢索“3”課程分?jǐn)?shù)小于60,按分?jǐn)?shù)降序排列的同學(xué)學(xué)號(hào); SELECT student_id, score FROM score WHERE score.course_id = 3 AND score.score < 60 ORDER BY student_id DESC; -- 50、刪除編號(hào)為“2”的同學(xué)的“1”課程的成績(jī); DELETE FROM score WHERE score.sid = (SELECT t1.sid FROM (SELECT sid FROM score AS s1 WHERE student_id = 2 AND course_id = 1) AS t1 ); -- 51、查詢同時(shí)選修了物理課和生物課的學(xué)生id和姓名; SELECT sid, sname FROM student WHERE sid IN (SELECT student_id FROM score WHERE course_id IN (SELECT cid FROM course WHERE course.cname IN ("物理","生物")) GROUP BY student_id HAVING count(course_id) = 2 );
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/41704.html
馬上就要開(kāi)始啦這次共組織15個(gè)組隊(duì)學(xué)習(xí) 涵蓋了AI領(lǐng)域從理論知識(shí)到動(dòng)手實(shí)踐的內(nèi)容 按照下面給出的最完備學(xué)習(xí)路線分類(lèi) 難度系數(shù)分為低、中、高三檔 可以按照需要參加 - 學(xué)習(xí)路線 - showImg(https://segmentfault.com/img/remote/1460000019082128); showImg(https://segmentfault.com/img/remote/...
摘要:最近看前端都展開(kāi)了幾場(chǎng)而我大知乎最熱語(yǔ)言還沒(méi)有相關(guān)。有關(guān)書(shū)籍的介紹,大部分截取自是官方介紹。但從開(kāi)始,標(biāo)準(zhǔn)庫(kù)為我們提供了模塊,它提供了和兩個(gè)類(lèi),實(shí)現(xiàn)了對(duì)和的進(jìn)一步抽象,對(duì)編寫(xiě)線程池進(jìn)程池提供了直接的支持。 《流暢的python》閱讀筆記 《流暢的python》是一本適合python進(jìn)階的書(shū), 里面介紹的基本都是高級(jí)的python用法. 對(duì)于初學(xué)python的人來(lái)說(shuō), 基礎(chǔ)大概也就夠用了...
摘要:刷題繼續(xù)大家好,我又回來(lái)了,昨天和大家分享了題,今天繼續(xù)來(lái)看題解法一解法二解法一解法二解法一解法二解法一解法二解法一解法一解法一解法一解法一解法一源代碼下載這十道題的代碼在我的上,如果大家想看一下每道題的輸出結(jié)果,可以點(diǎn)擊以下鏈接下載題 刷題繼續(xù) 大家好,我又回來(lái)了,昨天和大家分享了31-40題,今天繼續(xù)來(lái)看41~50題 Question 41: Write a program whi...
閱讀 1489·2021-10-14 09:43
閱讀 1454·2021-10-09 09:58
閱讀 1947·2021-09-28 09:42
閱讀 3738·2021-09-26 09:55
閱讀 1768·2021-08-27 16:23
閱讀 2769·2021-08-23 09:46
閱讀 917·2019-08-30 15:55
閱讀 1441·2019-08-30 15:54