Mysql資料庫的相關練習題及答案
阿新 • • 發佈:2019-02-12
表結構示意圖:
表結構建立語句:
class表建立語句: create table class(cid int not null auto_increment primary key, caption varchar(32) not null)engine=innodb default charset=utf8; student表建立語句: create table student( -> sid int not null auto_increment primary key, -> sname varchar(32) not null, -> gender varchar(8) not null, -> class_id int not null)engine=innodb default charset=utf8; teacher表建立語句: create table teacher( -> tid int not null auto_increment primary key, -> tname varchar(32) not null)engine=innodb default charset=utf8; course表建立語句: create table course( -> cid int not null auto_increment primary key, -> cname varchar(16) not null, -> teacher_id int not null)engine=innodb default charset=utf8; score表建立語句: create table score( -> sid int not null auto_increment primary key, -> student_id int not null, -> corse_id int not null, -> number int not null)engine=innodb default charset=utf8; 建立相關表
參考:https://www.cnblogs.com/xiaoqianghuihui/p/6961131.html1、自行建立測試資料 2、查詢“生物”課程比“物理”課程成績高的所有學生的學號; select A.student_id,sw,ty from (select student_id,number as sw from score left join course on score.corse_id = course.cid where course.cname = '生物') as A left join (select student_id,number as ty from score left join course on score.corse_id = course.cid where course.cname = '體育') as B on A.student_id = B.student_id where sw > if(isnull(ty),0,ty); 3、查詢平均成績大於60分的同學的學號和平均成績; 思路: 產尋學生的id,和成績,然後用avg函式來求得同一id號的學生平均成績,並用having進行成績的篩選 select student_id,avg(number) from score group by student_id having avg(number)>60; 增加顯示學生名 select student_id,student.sname,avg(number) from score left join student on score.student_id=student.sid group by student_id having avg(number)>60; 第二種實現方式 個人覺得這種方式好理解一些,語法結構是 先通過select student_id,avg(number) as stu_num from score group by student_id語句分組將資料取出並起臨時表別名為SCORE,然後在和student表進行連表。 select SCORE.student_id,SCORE.stu_num from (select student_id,avg(number) as stu_num from score group by student_id) as SCORE left join student on SCORE.student_id=student.sid; 4、查詢所有同學的學號、姓名、選課數、總成績; 語句進化過程: (1)先講student表關聯起來,關聯條件是student_id select * from score left join student on score.student_id = student.sid; (2)再通過條件篩選自己需要顯示的內容,用limit來分頁顯示 select score.student_id,student.sname,score.corse_id,score.number from score left join student on score.student_id = student.sid limit 5; (3)用聚合函式count來統計課程數,用sum來算成績的合。 select score.student_id,student.sname,count(score.corse_id),sum(score.number) from score left join student on score.student_id = student.sid group by score.student_id limit 5; 終極: select score.student_id,student.sname,count(score.corse_id),sum(score.number) from score left join student on score.student_id = student.sid group by score.student_id; 5、查詢姓“李”的老師的個數; select count(tname) from teacher where tname like "李%"; 6、查詢沒學過“葉平”老師課的同學的學號、姓名; (1)查出李平老師所受的課 select cid from course left join teacher on course.teacher_id=teacher.tid where teacher.tname="李平老師"; (2)查出選擇李平老師講課的學生 select * from score where score.corse_id in (select cid from course left join teacher on course.teacher_id=teacher.tid where teacher.tname="李平老師") (3)排除選擇李平老師講課的學生 select sid,sname from student where student.sid not in (select student_id from score where score.corse_id in (select cid from course left join teacher on course.teacher_id=teacher.tid where teacher.tname="李平老師")); 7、查詢學過“001”並且也學過編號“002”課程的同學的學號、姓名; (1)取出課程id是1和2的課程 select * from score where corse_id=1 or corse_id=2; (2)通過student_id來進行分組根據having來過來選擇兩門的學生 select NEW_C.student_id,count(NEW_C.corse_id) as NUM from (select student_id,corse_id from score where corse_id=1 or corse_id=2) as NEW_C group by NEW_C.student_id having NUM=2; (3)連表 select A.id,student.sname from (select NEW_C.student_id as ID,count(NEW_C.corse_id) as NUM from (select student_id,corse_id from score where corse_id=1 or corse_id=2) as NEW_C group by NEW_C.student_id having NUM=2) as A left join student on A.ID=student.sid; 8、查詢學過“葉平”老師所教的所有課的同學的學號、姓名; (1)連表查詢課程和老師,並過濾出李平老師 select cid,cname,teacher.tname from course left join teacher on course.teacher_id=teacher.tid where teacher.tname='李平老師'; (2)查出選擇李平老師課程的學生id select * from score where corse_id in (select cid from course left join teacher on course.teacher_id=teacher.tid where teacher.tname='李平老師') group by student_id; (3)關聯學生表顯示姓名 select A.student_id,student.sname from (select score.student_id from score where score.corse_id in (select course.cid from course left join teacher on course.teacher_id=teacher.tid where teacher.tname='李平老師') group by student_id) as A left join student on A.student_id=student.sid; 9、查詢課程編號“002”的成績比課程編號“001”課程低的所有同學的學號、姓名; select A.student_id,num_id1,num_id2 from (select student_id,number as num_id1 from score where corse_id=1) as A left join (select student_id,number as num_id2 from score where corse_id=2) as B on A.student_id = B.student_id where num_id1 > if(isnull(num_id2),0,num_id2); 10、查詢有課程成績小於60分的同學的學號、姓名; select student.sid,student.sname from (select score.student_id from score where score.number <60 group by score.student_id) as A left join student on student.sid= A.student_id; 11、查詢沒有學全所有課的同學的學號、姓名; select student.sid,student.sname from (select student_id,count(corse_id) as S_NUM from score group by student_id having S_NUM < (select count(cname) as C_NUM from course)) as A left join student on student.sid=A.student_id; 12、查詢至少有一門課與學號為“001”的同學所學相同的同學的學號和姓名; (1)查詢學生所選課程是否在學生id為1的學生的課程裡面 select student_id from score where student_id !=1 and score.corse_id in(select corse_id from score where student_id = 1) group by student_id; (2)和學生表關聯取出相關的ID和姓名 select student.sid,student.sname from (select student_id from score where student_id !=1 and score.corse_id in(select corse_id from score where student_id = 1) group by student_id) as A left join student on A.student_id=student.sid; 13、查詢至少學過學號為“001”同學所選課程中任意一門課的其他同學學號和姓名; select student.sid,student.sname from (select student_id from score where student_id !=1 and score.corse_id in(select corse_id from score where student_id = 1) group by student_id having count(corse_id)=(select count(corse_id) from score where student_id=1)) as A left join student on A.student_id=student.sid; 14、查詢和“002”號的同學學習的課程完全相同的其他同學學號和姓名; (1)和002號同學選擇的個數相同的學生id select student_id,count(student_id) from score group by student_id having count(student_id) = (select count(1) as a from score where student_id=2) (2)在篩選和002好同學選擇課程名相同的學生id select student_id,sname from score left join student on score.student_id = student.sid where student_id in (select student_id from score group by student_id having count(student_id) = (select count(1) as a from score where student_id=2)) and corse_id in (select corse_id from score where student_id =2) group by student_id having count(corse_id) = (select count(1) as a from score where student_id=2); 15、刪除學習“葉平”老師課的SC表記錄; delete from score where corse_id in (select cid from course left join teacher on course.teacher_id = teacher.tid where teacher.tname = "李平老師"); 16、向SC表中插入一些記錄,這些記錄要求符合以下條件:①沒有上過編號“002”課程的同學學號;②插入“002”號課程的平均成績;? select student_id,2,(select avg(number) from score where corse_id =2) as a from score where student_id not in (select student_id from score where corse_id=2) group by student_id; insert into score (student_id,corse_id,number) select student_id,2,(select avg(number) from score where corse_id =2) as a from score where student_id not in (select student_id from score where corse_id=2) group by student_id; 17、按平均成績從低到高顯示所有學生的“語文”、“數學”、“英語”三門的課程成績,按如下形式顯示: 學生ID,語文,數學,英語,有效課程數,有效平均分; 第一種實現方式 select SC.student_id,(select number from score where score.student_id = SC.student_id and corse_id = 1) as sw,(select number from score where score.student_id = SC.student_id and corse_id = 2) as wl,(select number from score where score.student_id = SC.student_id and corse_id = 3) as ty,(select number from score where score.student_id = SC.student_id and corse_id = 4) as ms,count(SC.corse_id),avg(SC.number) from score as SC group by SC.student_id order by avg(SC.number) desc; 第二種實現方式 select SC.student_id,(select number from score left join course on score.corse_id = course.cid where course.cname = "生物" and score.student_id = SC.student_id) as sw,(select number from score left join course on score.corse_id = course.cid where course.cname = "物理" and score.student_id = SC.student_id) as wl,(select number from score left join course on score.corse_id = course.cid where course.cname = "體育" and score.student_id = SC.student_id) as ty,(select number from score left join course on score.corse_id = course.cid where course.cname = "美術" and score.student_id = SC.student_id) as ms,count(SC.corse_id),avg(SC.number) from score as SC group by SC.student_id order by avg(SC.number) desc; 18、查詢各科成績最高和最低的分:以如下形式顯示:課程ID,最高分,最低分; 思路:通過課程id來進行分組,這個時候會顯示四行,然後用聚合函式max,min來找出最大值和最小值。 select corse_id,max(number) as max_num,min(number) as min_num from score group by corse_id; 19、按各科平均成績從低到高和及格率的百分數從高到低順序; 新知識點:case when then相當於if判斷 select corse_id,avg(number),sum(case when score.number>60 then 1 else 0 end)/count(1)*100 as jgl from score group by corse_id; 20、課程平均分從高到低顯示(現實任課老師); select A.avg_num,course.cname,teacher.tname from (select avg(number) as avg_num,corse_id from score group by corse_id) as A left join course on course.cid = A.corse_id left join teacher on teacher.tid = course.teacher_id order by A.avg_num desc; 21、查詢各科成績前三名的記錄:(不考慮成績並列情況)? select score.sid,score.corse_id,score.number,T.first_num,T.second_num from score left join (select sid,(select number from score as s2 where s2.corse_id = s1.corse_id order by number desc limit 0,1) as first_num, (select number from score as s2 where s2.corse_id = s1.corse_id order by number desc limit 3,1) as second_num from score as s1) as T on score.sid =T.sid where score.number <= T.first_num and score.number >= T.second_num; 22、查詢每門課程被選修的學生數; select corse_id,count(score.student_id) from score group by score.corse_id; 23、查詢出只選修了一門課程的全部學生的學號和姓名; select A.c_id,student.sname from (select count(corse_id) as c_id,student_id from score group by student_id) as A left join student on student.sid=A.student_id having A.c_id =1; 24、查詢男生、女生的人數; select * from (select count(1) as M from student where gender="男") as A,(select count(1) as W from student where gender="女") as B; 25、查詢姓“張”的學生名單; select sname from student where sname like "張%"; 26、查詢同名同姓學生名單,並統計同名人數; select count(sname) from student group by sname; 27、查詢每門課程的平均成績,結果按平均成績升序排列,平均成績相同時,按課程號降序排列; select corse_id,avg(number) from score group by corse_id order by avg(number) asc,corse_id desc; 28、查詢平均成績大於85的所有學生的學號、姓名和平均成績; select A.student_id,student.sname,A.avg_num from (select avg(number) as avg_num,student_id from score group by student_id having avg_num > 80) as A left join student on student.sid = A.student_id; 29、查詢課程名稱為“數學”,且分數低於60的學生姓名和分數; select A.student_id,student.sname,A.number from (select student_id,number from score where corse_id=(select cid from course where cname="生物") having number < 60) as A left join student on student.sid = A.student_id; 30、查詢課程編號為003且課程成績在80分以上的學生的學號和姓名; 第一種 select A.student_id,student.sname from (select student_id,number from score where corse_id=3 group by student_id having number > 80) as A left join student on student.sid=A.student_id; 第二種 select * from score where score.student_id = 3 and score.num > 80 31、求選了課程的學生人數 select count(A.student_id) from (select student_id from score group by student_id) as A; 32、查詢選修“楊豔”老師所授課程的學生中,成績最高的學生姓名及其成績; select score.corse_id,student.sname,score.number from score left join student on score.student_id=student.sid where score.corse_id in (select course.cid from course left join teacher on course.teacher_id=teacher.tid where tname = "劉海燕老師") order by score.number desc limit 3; 33、查詢各個課程及相應的選修人數; 第一種 select A.c_num,course.cname from (select count(student_id) as c_num,corse_id from score where corse_id in (select cid from course) group by corse_id) as A left join course on course.cid =A.corse_id ; 第二種 select course.cname,count(1) from score left join course on score.corse_id = course.cid group by corse_id; 34、查詢不同課程但成績相同的學生的學號、課程號、學生成績; select DISTINCT s1.corse_id,s2.corse_id,s1.number,s2.number from score as s1, score as s2 where s1.number = s2.number and s1.corse_id != s2.corse_id; 35、查詢每門課程成績最好的前兩名; select score.sid,score.corse_id,score.number,T.first_num,T.second_num from score left join (select sid,(select number from score as s2 where s2.corse_id=s1.corse_id order by number desc limit 0,1)as first_num, (select number from score as s2 where s2.corse_id=s1.corse_id order by number desc limit 1,1) as second_num from score as s1) as T on score.sid = T.sid where score.number <= T.first_num and score.number >= T.second_num; 36、檢索至少選修兩門課程的學生學號; select student_id,count(corse_id) from score group by student_id having count(corse_id)>1 37、查詢全部學生都選修的課程的課程號和課程名; select count(student_id) from score group by corse_id having count(student_id) = (select count(sid) from student); 38、查詢沒學過“葉平”老師講授的任一門課程的學生姓名; 第一種 select sname from(select A.student_id as n_s_id from (select student_id,corse_id from score) as A left join course on course.cid=A.corse_id where course.teacher_id not in (select tid from teacher where tname="李平老師") group by A.student_id ) as B left join student on student.sid=B.n_s_id; 第二種 select student_id,student.sname from score left join student on score.student_id = student.sid where score.corse_id not in (select cid from course left join teacher on course.teacher_id = teacher.tid where tname = "李平老師") group by student_id; 39、查詢兩門以上不及格課程的同學的學號及其平均成績; select avg(number) from score where student_id in (select student_id from score where number < 60 group by student_id having count(corse_id) >1); 40、檢索“004”課程分數小於60,按分數降序排列的同學學號; select A.number,student.sname,student.sid from (select score.number,score.student_id from score where score.corse_id = 4 and score.number<60) as A left join student on student.sid = A.student_id order by A.number desc; 41、刪除“002”同學的“001”課程的成績; delete from score where corse_id = 1 and student_id = 2