一、創建如下表,並創建相關約束
二、操作表
1、自行創建測試數據;
2、查詢學生總人數;
3、查詢“生物”課程和“物理”課程成績都及格的學生id和姓名;
4、查詢每個年級的班級數,取出班級數最多的前三個年級;
5、查詢平均成績最高和最低的學生的id和姓名以及平均成績;
6、查詢每個年級的學生人數;
7、查詢每位學生的學號,姓名,選課數,平均成績;
8、查詢學生編號為“2”的學生的姓名、該學生成績最高的課程名、成績最低的課程名及分數;
9、查詢姓“李”的老師的個數和所帶班級數;
10、查詢班級數小於5的年級id和年級名;
11、查詢班級信息,包括班級id、班級名稱、年級、年級級別(12為低年級,34為中年級,56為高年級),示例結果如下;
12、查詢學過“張三”老師2門課以上的同學的學號、姓名;
13、查詢教授課程超過2門的老師的id和姓名;
14、查詢學過編號“1”課程和編號“2”課程的同學的學號、姓名;
15、查詢沒有帶過高年級的老師id和姓名;
16、查詢學過“張三”老師所教的所有課的同學的學號、姓名;
17、查詢帶過超過2個班級的老師的id和姓名;
18、查詢課程編號“2”的成績比課程編號“1”課程低的所有同學的學號、姓名;
19、查詢所帶班級數最多的老師id和姓名;
20、查詢有課程成績小於60分的同學的學號、姓名;
21、查詢沒有學全所有課的同學的學號、姓名;
22、查詢至少有一門課與學號為“1”的同學所學相同的同學的學號和姓名;
23、查詢至少學過學號為“1”同學所選課程中任意一門課的其他同學學號和姓名;
24、查詢和“2”號同學學習的課程完全相同的其他同學的學號和姓名;
25、刪除學習“張三”老師課的score表記錄;
26、向score表中插入一些記錄,這些記錄要求符合以下條件:①沒有上過編號“2”課程的同學學號;②插入“2”號課程的平均成績;
27、按平均成績從低到高顯示所有學生的“語文”、“數學”、“英語”三門的課程成績,按如下形式顯示: 學生ID,語文,數學,英語,有效課程數,有效平均分;
28、查詢各科成績最高和最低的分:以如下形式顯示:課程ID,最高分,最低分;
29、按各科平均成績從低到高和及格率的百分數從高到低順序;
30、課程平均分從高到低顯示(現實任課老師);
31、查詢各科成績前三名的記錄(不考慮成績並列情況)
32、查詢每門課程被選修的學生數;
33、查詢選修了2門以上課程的全部學生的學號和姓名;
34、查詢男生、女生的人數,按倒序排列;
35、查詢姓“張”的學生名單;
36、查詢同名同姓學生名單,並統計同名人數;
37、查詢每門課程的平均成績,結果按平均成績升序排列,平均成績相同時,按課程號降序排列;
38、查詢課程名稱為“數學”,且分數低於60的學生姓名和分數;
39、查詢課程編號為“3”且課程成績在80分以上的學生的學號和姓名;
40、求選修了課程的學生人數
41、查詢選修“王五”老師所授課程的學生中,成績最高和最低的學生姓名及其成績;
42、查詢各個課程及相應的選修人數;
43、查詢不同課程但成績相同的學生的學號、課程號、學生成績;
44、查詢每門課程成績最好的前兩名學生id和姓名;
45、檢索至少選修兩門課程的學生學號;
46、查詢沒有學生選修的課程的課程號和課程名;
47、查詢沒帶過任何班級的老師id和姓名;
48、查詢有兩門以上課程超過80分的學生id及其平均成績;
49、檢索“3”課程分數小於60,按分數降序排列的同學學號;
50、刪除編號為“2”的同學的“1”課程的成績;
51、查詢同時選修了物理課和生物課的學生id和姓名;
此處是求知的分割線
--------------------------------------------------------------------------------------------------------------------------------------------
test_1

建庫建表 # 作業庫 create database mysql_work; # 年級表 create table class_grade( -> gid int not null auto_increment, -> gname varchar(20) not null unique, -> primary key(gid) -> ); # 班級表 create table class( -> cid int not null primary key auto_increment, -> caption varchar(20) not null, -> grade_id int not null, -> foreign key(grade_id) references class_grade(gid) -> ); # 學生表 create table student( -> sid int not null primary key auto_increment, -> gender enum('男','女') not null, -> class_id int not null, -> foreign key(class_id) references class(cid) -> ); # 老師表 create table teacher( -> tid int not null primary key auto_increment, -> tname varchar(20) not null -> ); # 課程表 create table course( -> cid int not null primary key auto_increment, -> cname varchar(20) not null, -> teacher_id int not null , -> foreign key(teacher_id) references teacher(tid) -> ); # 成績表 create table score( -> sid int not null unique auto_increment, -> student_id int not null, -> course_id int not null, -> score int not null, -> primary key(student_id,course_id), -> foreign key(student_id) references student(sid) -> on delete cascade -> on update cascade, -> foreign key(course_id) references course(cid) -> on delete cascade -> on update cascade -> ); # 班級任職表 create table teach2cls( -> tcid int not null unique auto_increment, -> tid int not null, -> cid int not null, -> primary key(tid,cid), -> foreign key(tid) references teacher(tid) -> on delete cascade -> on update cascade, -> foreign key(cid) references course(cid) -> on delete cascade -> on update cascade -> ); 插入數據 insert into class_grade(gname) values # 4個年級 ('一年級'), ('二年級'), ('三年級'), ('四年級'); +-----+-----------+ | gid | gname | +-----+-----------+ | 1 | 一年級 | | 3 | 三年級 | | 2 | 二年級 | | 4 | 四年級 | +-----+-----------+ insert into class(caption,grade_id) values # 9個班級 ('一年一班',1), ('一年二班',1), ('一年三班',1), ('二年一班',2), ('二年二班',2), ('三年一班',3), ('三年二班',3), ('四年一班',4), ('四年二班',4); +-----+--------------+----------+ | cid | caption | grade_id | +-----+--------------+----------+ | 1 | 一年一班 | 1 | | 2 | 一年二班 | 1 | | 3 | 一年三班 | 1 | | 4 | 二年一班 | 2 | | 5 | 二年二班 | 2 | | 6 | 三年一班 | 3 | | 7 | 三年二班 | 3 | | 8 | 四年一班 | 4 | | 9 | 四年二班 | 4 | +-----+--------------+----------+ insert into student(sname,gender,class_id) values # 12個學生 ('Jane','女',1), ('Rose','女',1), ('Jack','男',2), ('Alice','女',2), ('Alex','男',3), ('Drigon','男',4), ('Lily','女',5), ('Lucy','女',6), ('Jone','男',6), ('紫霞','女',7), ('張尊寶','男',8), ('高圓圓','女',9); +-----+--------+----------+-----------+ | sid | gender | class_id | sname | +-----+--------+----------+-----------+ | 1 | 女 | 1 | Jane | | 2 | 女 | 1 | Rose | | 3 | 男 | 2 | Jack | | 4 | 女 | 2 | Alice | | 5 | 男 | 3 | Alex | | 6 | 男 | 4 | Drigon | | 7 | 女 | 5 | Lily | | 8 | 女 | 6 | Lucy | | 9 | 男 | 6 | Jone | | 10 | 女 | 7 | 紫霞 | | 11 | 男 | 8 | 張尊寶 | | 12 | 女 | 9 | 高圓圓 | +-----+--------+----------+-----------+ insert into teacher(tname) values # 4個老師 ('曹顯'), ('王浩'), ('王五'), ('趙坤'); +-----+--------+ | tid | tname | +-----+--------+ | 1 | 曹顯 | | 2 | 王浩 | | 3 | 王五 | | 4 | 趙坤 | +-----+--------+ insert into course(cname,teacher_id) values # 6門課程 ('生物',1), ('物理',2), ('化學',3), ('語文',3), ('數學',4), ('地理',2); +-----+--------+------------+ | cid | cname | teacher_id | +-----+--------+------------+ | 1 | 生物 | 1 | | 2 | 物理 | 2 | | 3 | 化學 | 3 | | 4 | 語文 | 3 | | 5 | 數學 | 4 | | 6 | 地理 | 2 | +-----+--------+------------+ insert into score(student_id,course_id,score) values # 12個學生,6門課程 (1,1,60), (1,2,59), (2,4,60), (2,5,59), (2,6,33), (3,1,59), (3,5,28), (4,4,100), (4,6,90), (5,4,88), (6,5,100), (6,6,60), (7,3,57), (7,5,60), (8,2,61), (8,4,59), (9,1,60), (9,2,61), (9,3,21), (10,5,68), (11,1,89), (12,3,100); +-----+------------+-----------+-------+ | sid | student_id | course_id | score | +-----+------------+-----------+-------+ | 1 | 1 | 1 | 60 | | 2 | 1 | 2 | 59 | | 3 | 2 | 4 | 60 | | 4 | 2 | 5 | 59 | | 5 | 2 | 6 | 33 | | 6 | 3 | 1 | 59 | | 7 | 3 | 5 | 28 | | 8 | 4 | 4 | 100 | | 9 | 4 | 6 | 90 | | 10 | 5 | 4 | 88 | | 11 | 6 | 5 | 100 | | 12 | 6 | 6 | 60 | | 13 | 7 | 3 | 57 | | 14 | 7 | 5 | 60 | | 15 | 8 | 2 | 61 | | 16 | 8 | 4 | 59 | | 17 | 9 | 1 | 60 | | 18 | 9 | 2 | 61 | | 19 | 9 | 3 | 21 | | 20 | 10 | 5 | 68 | | 21 | 11 | 1 | 89 | | 22 | 12 | 3 | 100 | +-----+------------+-----------+-------+ insert into teach2cls(tid,cid) values # 4個老師 9個班級 (1,1), (1,2), (1,3), (1,7), (2,4), (2,8), (2,7), (2,5), (3,9), (3,3), (3,5), (3,2), (4,8), (4,4), (4,6), (4,1); +------+-----+-----+ | tcid | tid | cid | +------+-----+-----+ | 1 | 1 | 1 | | 2 | 1 | 2 | | 3 | 1 | 3 | | 4 | 1 | 7 | | 5 | 2 | 4 | | 6 | 2 | 8 | | 7 | 2 | 7 | | 8 | 2 | 5 | | 9 | 3 | 9 | | 10 | 3 | 3 | | 11 | 3 | 5 | | 12 | 3 | 2 | | 13 | 4 | 8 | | 14 | 4 | 4 | | 15 | 4 | 6 | | 16 | 4 | 1 | +------+-----+-----+
test_2

查詢學生總人數; select count(sid) as count_student from student; +---------------+ | count_student | +---------------+ | 12 | +---------------+
test_3

查詢“生物”課程和“物理”課程成績都及格的學生id和姓名; select sid,sname from student where sid in( select score.student_id from score inner join course on score.course_id=course.cid where course.cname in( "生物", "物理") and score.score >= 60 group by score.student_id having count(course_id) = 2 ); +-----+-------+ | sid | sname | +-----+-------+ | 9 | Jone | +-----+-------+
test_4

查詢每個年級的班級數,取出班級數最多的前三個年級; select class_grade.gname from class_grade inner join (select grade_id,count(cid) as count_course from class group by grade_id order by count_course desc limit 3) as t1 on class_grade.gid = t1.grade_id; +-----------+ | gname | +-----------+ | 一年級 | | 三年級 | | 二年級 | +-----------+
test_5

查詢平均成績最高和最低的學生的id和姓名以及平均成績; select student.sid,student.sname,t1.avg_score from student inner join ( select student_id,avg(score) as avg_score from score group by student_id having avg(score) in ( ( select avg(score) as max_avg_score from score group by student_id order by avg(score) desc limit 1 ), ( select avg(score) as min_avg_score from score group by student_id order by avg(score) asc limit 1 ) ) )as t1 on student.sid = t1.student_id; +-----+-----------+-----------+ | sid | sname | avg_score | +-----+-----------+-----------+ | 3 | Jack | 43.5000 | | 12 | 高圓圓 | 100.0000 | +-----+-----------+-----------+
test_6

查詢每個年級的學生人數; select t1.grade_id,count(t1.sid) as count_student from ( select student.sid,class.grade_id from student, class where student.class_id = class.cid )as t1 group by t1.grade_id; +----------+---------------+ | grade_id | count_student | +----------+---------------+ | 1 | 5 | | 2 | 2 | | 3 | 3 | | 4 | 2 | +----------+---------------+
test_7

查詢每位學生的學號,姓名,選課數,平均成績; select student.sid,student.sname,t1.count_course,t1.avg_score from student left join( select student_id, count(course_id) as count_course, avg(score) as avg_score from score group by student_id )as t1 on student.sid=t1.student_id; +-----+-----------+--------------+-----------+ | sid | sname | count_course | avg_score | +-----+-----------+--------------+-----------+ | 1 | Jane | 2 | 59.5000 | | 2 | Rose | 3 | 50.6667 | | 3 | Jack | 2 | 43.5000 | | 4 | Alice | 2 | 95.0000 | | 5 | Alex | 1 | 88.0000 | | 6 | Drigon | 2 | 80.0000 | | 7 | Lily | 2 | 58.5000 | | 8 | Lucy | 2 | 60.0000 | | 9 | Jone | 3 | 47.3333 | | 10 | 紫霞 | 1 | 68.0000 | | 11 | 張尊寶 | 1 | 89.0000 | | 12 | 高圓圓 | 1 | 100.0000 | +-----+-----------+--------------+-----------+
test_8

查詢學生編號為“2”的學生的姓名、該學生成績最高的課程名、成績最低的課程名及分數; select student.sname,course.cname,t1.score from( select student_id, course_id, t1.score from score where student_id=2 and score in ( select max(score) from score where student_id=2 ), ( select min(score) from score where student_id=2 ) )as t1 inner join student on t1.student_id=student.sid inner join course on t1.course_id=course.cid; +-------+--------+-------+ | sname | cname | score | +-------+--------+-------+ | Rose | 語文 | 60 | | Rose | 地理 | 33 | +-------+--------+-------+
test_9

查詢班級數小於5的年級id和年級名; 方法1: select gid,gname,count(cid) as count_cid from class_grade inner join class on gid=grade_id group by gid having count(cid)<5; 方法2: select gid, gname from class_grade where gid in ( select grade_id from class group by grade_id having count(caption) < 5 ); +-----+-----------+-----------+ | gid | gname | count_cid | +-----+-----------+-----------+ | 1 | 一年級 | 3 | | 2 | 二年級 | 2 | | 3 | 三年級 | 2 | | 4 | 四年級 | 2 | +-----+-----------+-----------+
test_10

查詢班級數小於5的年級id和年級名; 方法1: select gid,gname,count(cid) as count_cid from class_grade inner join class on gid=grade_id group by gid having count(cid)<5; 方法2: select gid, gname from class_grade where gid in ( select grade_id from class group by grade_id having count(caption) < 5 ); +-----+-----------+-----------+ | gid | gname | count_cid | +-----+-----------+-----------+ | 1 | 一年級 | 3 | | 2 | 二年級 | 2 | | 3 | 三年級 | 2 | | 4 | 四年級 | 2 | +-----+-----------+-----------+
test_11

查詢班級信息,包括班級id、班級名稱、年級、年級級別 select class.cid,class.caption,class_grade.gid, case when class_grade.gid between 1 and 2 then '低' when class_grade.gid between 3 and 4 then '中' when class_grade.gid between 5 and 6 then '高' else 0 end as '年級級別' from class, class_grade where class.grade_id=class_grade.gid; +-----+--------------+-----+--------------+ | cid | caption | gid | 年級級別 | +-----+--------------+-----+--------------+ | 1 | 一年一班 | 1 | 低 | | 2 | 一年二班 | 1 | 低 | | 3 | 一年三班 | 1 | 低 | | 6 | 三年一班 | 3 | 中 | | 7 | 三年二班 | 3 | 中 | | 4 | 二年一班 | 2 | 低 | | 5 | 二年二班 | 2 | 低 | | 8 | 四年一班 | 4 | 中 | | 9 | 四年二班 | 4 | 中 | +-----+--------------+-----+--------------+
test_12

查詢學過“張三”老師2門課以上的同學的學號、姓名; select student.sid,student.sname from student where sid in( select student_id from score where course_id in( select course.cid from teacher, course where teacher.tid=course.teacher_id and teacher.tname='張三' ) group by student_id having count(course_id) >2 ); # Empty set (0.00 sec) 記錄為空
test_13

查詢教授課程超過2門的老師的id和姓名; select teacher.tid,teacher.tname from teacher where tid in( select teacher_id from course group by teacher_id having count(cid)>2 ); # Empty set (0.00 sec) 無記錄
test_14

查詢學過編號“1”課程和編號“2”課程的同學的學號、姓名; select sid, sname from student where sid in ( select distinct student_id from score where course_id in ( 1, 2 ) ); +-----+-----------+ | sid | sname | +-----+-----------+ | 1 | Jane | | 3 | Jack | | 8 | Lucy | | 9 | Jone | | 11 | 張尊寶 | +-----+-----------+
test_15

查詢沒有帶過高年級的老師id和姓名; select tid,tname from teacher where tid not in( select tid from teach2cls where cid in( select t1.cid from( select class.cid, class.caption, class_grade.gname, case when class_grade.gid between 1 and 2 then '低' when class_grade.gid between 3 and 4 then '中' when class_grade.gid between 5 and 6 then '高' else 0 end as grade_layer from class, class_grade where class.grade_id=class_grade.gid )as t1 where t1.grade_layer='高' ) ); +-----+--------+ | tid | tname | +-----+--------+ | 1 | 曹顯 | | 2 | 王浩 | | 3 | 王五 | | 4 | 趙坤 | +-----+--------+
test_16

查詢學過“張三”老師所教的所有課的同學的學號、姓名; select sid,sname from student where sid in ( select student_id from score where course_id in( select cid from course inner join teacher on course.teacher_id=teacher.tid where teacher.tname = '張三' ) ); Empty set (0.00 sec) 記錄為空
test_17

查詢帶過超過2個班級的老師的id和姓名; select tid,tname from teacher where tid in( select tid from teach2cls group by tid having count(cid)>2 ); +-----+--------+ | tid | tname | +-----+--------+ | 1 | 曹顯 | | 2 | 王浩 | | 3 | 王五 | | 4 | 趙坤 | +-----+--------+
test_18

查詢課程編號“2”的成績比課程編號“1”課程低的所有同學的學號、姓名; select sid,sname from student where sid in( select t1.student_id from (select student_id,score from score where course_id=2 group by student_id ) as t1, (select student_id,score from score where course_id=1 group by student_id ) as t2 where t1.student_id = t2.student_id and t1.score < t2.score ); +-----+-------+ | sid | sname | +-----+-------+ | 1 | Jane | +-----+-------+
test_19

查詢所帶班級數最多的老師id和姓名; select tid,tname from teacher where tid in( select tid from teach2cls group by tid having count(cid)=( select count(cid) from teach2cls group by tid order by count(cid) desc limit 1 ) ); +-----+--------+ | tid | tname | +-----+--------+ | 1 | 曹顯 | | 2 | 王浩 | | 3 | 王五 | | 4 | 趙坤 | +-----+--------+
test_20

查詢有課程成績小於60分的同學的學號、姓名; select sid,sname from student where sid in ( select distinct student_id from score where score<60 ); +-----+-------+ | sid | sname | +-----+-------+ | 1 | Jane | | 2 | Rose | | 3 | Jack | | 7 | Lily | | 8 | Lucy | | 9 | Jone | +-----+-------+
test_21

查詢沒有學全所有課的同學的學號、姓名; select sid,sname from student where sid not in( select student_id from score group by student_id having count(course_id) = ( select count(cid) from course) ); +-----+-----------+ | sid | sname | +-----+-----------+ | 1 | Jane | | 2 | Rose | | 3 | Jack | | 4 | Alice | | 5 | Alex | | 6 | Drigon | | 7 | Lily | | 8 | Lucy | | 9 | Jone | | 10 | 紫霞 | | 11 | 張尊寶 | | 12 | 高圓圓 | +-----+-----------+
test_22

查詢至少有一門課與學號為“1”的同學所學相同的同學的學號和姓名; select sid,sname from student where sid in( select student_id from score where course_id in ( select course_id from score where student_id = 1 ) group by student_id ); +-----+-----------+ | sid | sname | +-----+-----------+ | 1 | Jane | | 3 | Jack | | 8 | Lucy | | 9 | Jone | | 11 | 張尊寶 | +-----+-----------+
test_23

查詢至少學過學號為“1”同學所選課程中任意一門課的其他同學學號和姓名; select sid,sname from student where sid in( select student_id from score where course_id in( select course_id from score where student_id = 1 ) group by student_id ) and sid!=1; +-----+-----------+ | sid | sname | +-----+-----------+ | 3 | Jack | | 9 | Jone | | 11 | 張尊寶 | | 8 | Lucy | +-----+-----------+
test_24

查詢和“2”號同學學習的課程完全相同的其他同學的學號和姓名; select sid,sname from student where sid in ( select score.student_id from score, ( select course_id from score where student_id = 2 ) as t1 where score.course_id = t1.course_id and score.student_id != 2 group by score.student_id having count(score.course_id) = ( select count(course_id) from score where student_id = 2 ) );
test_25

刪除學習“張三”老師課的score表記錄; delete from score where course_id in( select course.cid from course,teacher where course.teacher_id = teacher.tid and teacher.tname = '張三'); Query OK, 0 rows affected (0.08 sec) 無數據受到影響
test_26

向score表中插入一些記錄,這些記錄要求符合以下條件:①沒有上過編號“2”課程的同學學號;②插入“2”號課程的平均成績; insert into score(student_id,course_id,score) select t1.sid,2,t2.avg_score from ( select sid from student where sid not in ( select student_id from score where course_id=2 ) ) as t1, ( select avg(score) as avg_score from score where course_id=2 ) as t2; Query OK, 9 rows affected (0.14 sec) # 添加了9行記錄 Records: 9 Duplicates: 0 Warnings: 0
test_27

按平均成績從低到高顯示所有學生的“語文”、“數學”、“物理”三門的課程成績,按如下形式顯示: select sc.student_id, (select score.score from score left join course on score.course_id=course.cid where course.cname='語文' and score.student_id = sc.student_id )as Chinese, (select score.score from score left join course on score.course_id = course.cid where course.cname='數學' and score.student_id=sc.student_id )as math, (select score.score from score left join course on score.course_id = course.cid where course.cname='物理' and score.student_id=sc.student_id )as physics, count(sc.course_id), avg(sc.score) from score as sc group by sc.student_id order by avg(sc.score) asc; +------------+---------+------+---------+---------------------+---------------+ | student_id | Chinese | math | physics | count(sc.course_id) | avg(sc.score) | +------------+---------+------+---------+---------------------+---------------+ | 9 | NULL | NULL | 61 | 3 | 47.3333 | | 3 | NULL | 28 | 60 | 3 | 49.0000 | | 2 | 60 | 59 | 60 | 4 | 53.0000 | | 7 | NULL | 60 | 60 | 3 | 59.0000 | | 1 | NULL | NULL | 59 | 2 | 59.5000 | | 8 | 59 | NULL | 61 | 2 | 60.0000 | | 10 | NULL | 68 | 60 | 2 | 64.0000 | | 6 | NULL | 100 | 60 | 3 | 73.3333 | | 5 | 88 | NULL | 60 | 2 | 74.0000 | | 11 | NULL | NULL | 60 | 2 | 74.5000 | | 12 | NULL | NULL | 60 | 2 | 80.0000 | | 4 | 100 | NULL | 60 | 3 | 83.3333 | +------------+---------+------+---------+---------------------+---------------+ 12 rows in set (0.00 sec)
test_28

查詢各科成績最高和最低的分:以如下形式顯示:課程ID,最高分,最低分; select course.cid,max(score.score) as max_score,min(score.score) as min_score from course left join score on course.cid=score.course_id group by score.course_id; +-----+-----------+-----------+ | cid | max_score | min_score | +-----+-----------+-----------+ | 1 | 89 | 59 | | 2 | 61 | 59 | | 3 | 100 | 21 | | 4 | 100 | 59 | | 5 | 100 | 28 | | 6 | 90 | 33 | +-----+-----------+-----------+ 6 rows in set (2.43 sec)
test_29

按各科平均成績從低到高和及格率的百分數從高到低順序; select course_id,avg(score) as avg_score, sum(case when score.score>60 then 1 else 0 end)/count(1)*100 as percent from score group by course_id order by avg(score) asc, percent desc;
test_30

課程平均分從高到低顯示(顯示任課老師); select t1.course_id,t1.avg_score,teacher.tname from course,teacher, ( select course_id,avg(score) as avg_score from score group by course_id order by avg_score desc ) as t1 where course.cid = t1.course_id and course.teacher_id = teacher.tid order by t1.avg_score desc; +-----------+-----------+--------+ | course_id | avg_score | tname | +-----------+-----------+--------+ | 4 | 76.7500 | 王五 | | 1 | 67.0000 | 曹顯 | | 5 | 63.0000 | 趙坤 | | 6 | 61.0000 | 王浩 | | 2 | 60.0833 | 王浩 | | 3 | 59.3333 | 王五 | +-----------+-----------+--------+ 6 rows in set (0.05 sec)
test_31

查詢各科成績前三名的記錄 select score.sid, score.student_id, score.course_id, score.score, t1.first_score, t1.second_score, t1.third_score from score inner join( select s1.sid, (select score from score as s2 where s1.course_id=s2.course_id order by score desc limit 0,1) as first_score, (select score from score as s3 where s1.course_id=s3.course_id order by score desc limit 1,1) as second_score, (select score from score as s4 where s1.course_id=s4.course_id order by score desc limit 2,1) as third_score from score as s1 ) as t1 on score.sid = t1.sid where score.score in( t1.first_score, t1.second_score, t1.third_score ); +-----+------------+-----------+-------+-------------+--------------+-------------+ | sid | student_id | course_id | score | first_score | second_score | third_score | +-----+------------+-----------+-------+-------------+--------------+-------------+ | 1 | 1 | 1 | 60 | 89 | 60 | 60 | | 3 | 2 | 4 | 60 | 100 | 88 | 60 | | 5 | 2 | 6 | 33 | 90 | 60 | 33 | | 8 | 4 | 4 | 100 | 100 | 88 | 60 | | 9 | 4 | 6 | 90 | 90 | 60 | 33 | | 10 | 5 | 4 | 88 | 100 | 88 | 60 | | 11 | 6 | 5 | 100 | 100 | 68 | 60 | | 12 | 6 | 6 | 60 | 90 | 60 | 33 | | 13 | 7 | 3 | 57 | 100 | 57 | 21 | | 14 | 7 | 5 | 60 | 100 | 68 | 60 | | 15 | 8 | 2 | 61 | 61 | 61 | 60 | | 17 | 9 | 1 | 60 | 89 | 60 | 60 | | 18 | 9 | 2 | 61 | 61 | 61 | 60 | | 19 | 9 | 3 | 21 | 100 | 57 | 21 | | 20 | 10 | 5 | 68 | 100 | 68 | 60 | | 21 | 11 | 1 | 89 | 89 | 60 | 60 | | 22 | 12 | 3 | 100 | 100 | 57 | 21 | | 23 | 2 | 2 | 60 | 61 | 61 | 60 | | 24 | 3 | 2 | 60 | 61 | 61 | 60 | | 25 | 4 | 2 | 60 | 61 | 61 | 60 | | 26 | 5 | 2 | 60 | 61 | 61 | 60 | | 27 | 6 | 2 | 60 | 61 | 61 | 60 | | 28 | 7 | 2 | 60 | 61 | 61 | 60 | | 29 | 10 | 2 | 60 | 61 | 61 | 60 | | 30 | 11 | 2 | 60 | 61 | 61 | 60 | | 31 | 12 | 2 | 60 | 61 | 61 | 60 | +-----+------------+-----------+-------+-------------+--------------+-------------+ 26 rows in set (0.00 sec)
test_32

查詢每門課程被選修的學生數; select course.cid,ifnull(t1.count_students,0) as count_student from course left join ( select course_id,count(student_id) as count_students from score group by course_id ) as t1 on course.cid = t1.course_id; +-----+---------------+ | cid | count_student | +-----+---------------+ | 1 | 4 | | 2 | 12 | | 6 | 3 | | 3 | 3 | | 4 | 4 | | 5 | 5 | +-----+---------------+ 6 rows in set (0.06 sec)
test_33

查詢選修了2門以上課程的全部學生的學號和姓名; select sid,sname from student where sid in( select student_id from score group by student_id having count(course_id)>2 ); +-----+--------+ | sid | sname | +-----+--------+ | 2 | Rose | | 3 | Jack | | 4 | Alice | | 6 | Drigon | | 7 | Lily | | 9 | Jone | +-----+--------+ 6 rows in set (0.06 sec)
test_34

查詢男生、女生的人數,按倒序排列; select gender,count(sid) as count_student from student group by gender order by count_student desc; +--------+---------------+ | gender | count_student | +--------+---------------+ | 女 | 7 | | 男 | 5 | +--------+---------------+ 2 rows in set (0.02 sec)
test_35

查詢姓“張”的學生名單; select sid,sname,gender,class.caption from student inner join class on student.class_id = class.cid where sname like '張%'; +-----+-----------+--------+--------------+ | sid | sname | gender | caption | +-----+-----------+--------+--------------+ | 11 | 張尊寶 | 男 | 四年一班 | +-----+-----------+--------+--------------+ 1 row in set (0.06 sec)
test_36

查詢同名同姓學生名單,並統計同名人數; select sid,sname,count(sname) as count_sname from student group by sname having count(sname)>1; Empty set (0.00 sec)
test_37

查詢每門課程的平均成績,結果按平均成績升序排列,平均成績相同時,按課程號降序排列; select cid,cname,avg(score.score) as avg_score from course inner join score on course.cid = score.course_id group by cid having avg(score.score) order by avg(score.score), course_id desc; +-----+--------+-----------+ | cid | cname | avg_score | +-----+--------+-----------+ | 3 | 化學 | 59.3333 | | 2 | 物理 | 60.0833 | | 6 | 地理 | 61.0000 | | 5 | 數學 | 63.0000 | | 1 | 生物 | 67.0000 | | 4 | 語文 | 76.7500 | +-----+--------+-----------+ 6 rows in set (0.00 sec)
test_38

查詢課程名稱為“數學”,且分數低於60的學生姓名和分數; select student.sid,student.sname,t1.score from student inner join ( select score.student_id,score.score from score inner join course on score.course_id=course.cid where cname='數學' and score.score<60 )as t1 on student.sid=t1.student_id; +-----+-------+-------+ | sid | sname | score | +-----+-------+-------+ | 2 | Rose | 59 | | 3 | Jack | 28 | +-----+-------+-------+ 2 rows in set (0.00 sec)
test_39

查詢課程編號為“3”且課程成績在80分以上的學生的學號和姓名; select student.sid,student.sname,t1.score from student inner join( select score.student_id,score.score from score inner join course on score.course_id=course.cid where cid=3 and score.score>80 )as t1 on student.sid = t1.student_id; +-----+-----------+-------+ | sid | sname | score | +-----+-----------+-------+ | 12 | 高圓圓 | 100 | +-----+-----------+-------+ 1 row in set (0.00 sec)
test_40

求選修了課程的學生人數 select course_id,count(student_id) as count_student from score group by course_id; +-----------+---------------+ | course_id | count_student | +-----------+---------------+ | 1 | 4 | | 2 | 12 | | 3 | 3 | | 4 | 4 | | 5 | 5 | | 6 | 3 | +-----------+---------------+ 6 rows in set (0.00 sec)
test_41

查詢選修“王五”老師所授課程的學生中,成績最高和最低的學生姓名及其成績; select student.sid,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; +-----+-----------+-----------+-------+-----------+-----------+ | sid | sname | course_id | score | max_score | min_score | +-----+-----------+-----------+-------+-----------+-----------+ | 4 | Alice | 4 | 100 | 100 | 59 | | 8 | Lucy | 4 | 59 | 100 | 59 | | 9 | Jone | 3 | 21 | 100 | 21 | | 12 | 高圓圓 | 3 | 100 | 100 | 21 | +-----+-----------+-----------+-------+-----------+-----------+ 4 rows in set (0.00 sec)
test_42

查詢各個課程及相應的選修人數; select course.cid,course.cname,count(student_id) as count_student from course inner join score on course.cid = score.course_id group by course.cid having count(student_id); -+--------+---------------+ | cid | cname | count_student | +-----+--------+---------------+ | 1 | 生物 | 4 | | 2 | 物理 | 12 | | 3 | 化學 | 3 | | 4 | 語文 | 4 | | 5 | 數學 | 5 | | 6 | 地理 | 3 | +-----+--------+---------------+ 6 rows in set (0.00 sec)
test_43

查詢不同課程但成績相同的學生的學號、課程號、學生成績; select distinct s1.student_id,s2.student_id, s1.course_id as s1_course_id, s2.course_id as s2_course_id, s1.score,s2.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; +------------+------------+--------------+--------------+-------+-------+ | student_id | student_id | s1_course_id | s2_course_id | score | score | +------------+------------+--------------+--------------+-------+-------+ | 2 | 2 | 2 | 4 | 60 | 60 | | 2 | 2 | 4 | 2 | 60 | 60 | | 6 | 6 | 2 | 6 | 60 | 60 | | 6 | 6 | 6 | 2 | 60 | 60 | | 7 | 7 | 2 | 5 | 60 | 60 | | 7 | 7 | 5 | 2 | 60 | 60 | +------------+------------+--------------+--------------+-------+-------+ 6 rows in set (0.00 sec)
test_44

查詢每門課程成績最好的前兩名學生id和姓名; select student.sid, student.sname, t2.course_id, t2.score, t2.first_score, t2.second_score from student inner join ( select score.student_id, score.course_id, score.score, t1.first_score, t1.second_score from score inner join ( select s1.sid, (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 ) as t1 on score.sid = t1.sid where score.score in ( t1.first_score, t1.second_score ) ) as t2 on student.sid = t2.student_id;
test_45

檢索至少選修兩門課程的學生學號; select student_id,student.sname from score inner join student on score.student_id = student.sid group by student_id having count(course_id)>=2; +------------+-----------+ | student_id | sname | +------------+-----------+ | 1 | Jane | | 2 | Rose | | 3 | Jack | | 4 | Alice | | 5 | Alex | | 6 | Drigon | | 7 | Lily | | 8 | Lucy | | 9 | Jone | | 10 | 紫霞 | | 11 | 張尊寶 | | 12 | 高圓圓 | +------------+-----------+ 12 rows in set (0.00 sec)
test_46

查詢沒有學生選修的課程的課程號和課程名; select course.cid,course.cname from course where course.cid not in ( select course_id from score group by course_id ); Empty set (0.00 sec) 無數據受到影響
test_47

查詢沒帶過任何班級的老師id和姓名; select teacher.tid,teacher.tname from teacher where teacher.tid not in( select tid from teach2cls group by tid ); Empty set (0.05 sec) 無數據受到影響
test_48

查詢有兩門以上課程超過80分的學生id及其平均成績; select score.student_id,avg(score) as avg_score from score where student_id in ( select student_id from score where score>80 group by student_id having count(score.course_id)>2 ) group by student_id; Empty set (0.00 sec) 無數據受到影響
test_49

檢索“3”課程分數小於60,按分數降序排列的同學學號; select score.student_id,score.score from score where score<60 and course_id = 3 order by score.score desc; +------------+-------+ | student_id | score | +------------+-------+ | 7 | 57 | | 9 | 21 | +------------+-------+ 2 rows in set (0.00 sec)
test_50

刪除編號為“2”的同學的“1”課程的成績; 方法一: delete score from score where student_id=2 and course_id=1; 方法二: delete from score where sid=( select t1.sid from ( select sid from score where student_id = 2 and course_id = 1 )as t1 ); Query OK, 0 rows affected (0.00 sec) 無數據受到影響
test_51

查詢同時選修了物理課和生物課的學生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 ); +-----+-----------+ | sid | sname | +-----+-----------+ | 1 | Jane | | 3 | Jack | | 9 | Jone | | 11 | 張尊寶 | +-----+-----------+ 4 rows in set (0.00 sec)