1. 程式人生 > >初學mysql經典練習題及答案

初學mysql經典練習題及答案

建立如下各表

   班級表class

學生表:student


教師表:teacher


課程表:course

成績表:score
建立各表的語句:
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,
    -> name 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;

題目及答案:

1、查詢“生物”課程比“體育”課程成績高的所有學生的學號;

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)

2、查詢平均成績大於60分的同學的學號和平均成績;

select student_id,avg(number) from score group by student_id having avg(number)>60;

     增加顯示學生名

select student_id,student.name,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 where SCORE.stu_num>60

3、查詢所有同學的學號、姓名、選課數、總成績;

select score.student_id,student.name,count(score.corse_id),sum(score.number) from score left join student on score.student_id = student.sid group by score.student_id;

4、查詢姓“趙”的老師的個數;

select count(tname) from teacher where tname like "趙%";

5、查詢沒學過趙老師課的同學的學號、姓名;

(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,name 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="趙"));

6、查詢學過“1”並且也學過編號“2”課程的同學的學號、姓名;

(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.name 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;

7、查詢學過趙老師所教的所有課的同學的學號、姓名;

(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.name from(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)as A left join student on A.student_id=student.sid

8、查詢課程編號“2”的成績比課程編號“1”課程低的所有同學的學號、姓名;

select student_id,name from(select A.student_id,num_1,num_2 from(select student_id, number as num_1 from score where corse_id=1)as A left join (select student_id,number as num_2 from score where corse_id=2)as B on A.student_id=B.student_id where num_1>if (isnull(num_2),0,num_2))as C left join student on C.student_id=sid

9、查詢有課程成績小於60分的同學的學號、姓名;

select student_id,name from(select *from score where score.number<60)as A left join student on A.student_id=student.sid

10、查詢沒有學全所有課的同學的學號、姓名;

select student.sid,student.name from (select student_id,count(corse_id)as S_NUM from score group by student_id having S_NUM<(select count(cname) from course))as A right join student on A.student_id=student.sid

11、查詢至少有一門課與學號為“1”的同學所學相同的同學的學號和姓名;

(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_id,name 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
12、查詢和“2”號的同學學習的課程完全相同的其他同學學號和姓名;

(1)和2號同學選擇的課程相同的學生id(其中的count(1)表示滿足條件的記錄數量)

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)在篩選和2號同學選擇課程名相同的學生id

select student_id,name 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);

13、向score表中插入一些記錄,這些記錄要求符合以下條件:①沒有上過編號“2”課程的同學學號;②插入“2”號課程的平均成績;

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;

14、按平均成績從低到高顯示所有學生的“生物”、“物理”、“體育”三門的課程成績,按如下形式顯示: 學生ID,生物,物理體育,有效課程數,有效平均分;

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,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,count(SC.corse_id),avg(SC.number) from score as SC group by SC.student_id order by avg(SC.number) desc;
15、查詢各科成績最高和最低的分:以如下形式顯示:課程ID,最高分,最低分;思路:通過課程id來進行分組,這個時候會顯示四行,然後用聚合函式max,min來找出最大值和最小值。
select corse_id,max(number) as max_num,min(number) as min_num from score group by corse_id;

16、按各科平均成績從低到高和及格率的百分數從高到低順序;

新知識點: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;

17、課程平均分從高到低顯示(現實任課老師);

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;

18、查詢每門課程被選修的學生數;

select corse_id,count(score.student_id) from score group by score.corse_id;

19、查詢出只選修了兩門課程的全部學生的學號和姓名;

select A.c_id,student.name 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 =2;

20、查詢男生、女生的人數;

select * from (select count(1) as M from student where gender="男") as A,(select count(1) as W from student where gender="女") as B;
21、查詢每門課程的平均成績,結果按平均成績升序排列,平均成績相同時,按課程號降序排列;
select corse_id,avg(number) from score group by corse_id order by avg(number) asc,corse_id desc;
22、查詢平均成績大於75的所有學生的學號、姓名和平均成績;
select A.student_id,student.name,A.avg_num from (select avg(number) as avg_num,student_id from score group by student_id having avg_num > 75) as A left join student on student.sid = A.student_id;
23、查詢課程名稱為“體育”,且分數低於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;
24、查詢課程編號為3且課程成績在80分以上的學生的學號和姓名;
select A.student_id,student.name 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.corse_id = 3 and score.number > 80
25、求選了課程的學生人數
select count(A.student_id) from (select student_id from score group by student_id) as A;
26、查詢選修趙老師所授課程的學生中,成績最高的學生姓名及其成績;
select score.corse_id,student.name,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
27、查詢各個課程及相應的選修人數;
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;
28、查詢不同課程但成績相同的學生的學號、課程號、學生成績;
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;
29、檢索至少選修兩門課程的學生學號;
select student_id,count(corse_id) from score group by student_id having count(corse_id)>1
30、查詢全部學生都選修的課程的課程號和課程名;
select corse_id,cname,count(student_id)  from score left join course on score.corse_id=course.cid group by corse_id having count(student_id) = (select count(sid) from student)
31、查詢沒學過孫老師講授的任一門課程的學生姓名;

select name 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;
32、查詢一門以上不及格課程的同學的學號及其平均成績;
select student_id,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);
33、查詢課程號為2且分數小於60的記錄,按分數降序排列的同學學號;
select A.number,student.name,student.sid from (select score.number,score.student_id from score where score.corse_id = 2 and score.number<60) as A left join student on student.sid = A.student_id order by A.number desc;

34、刪除學習孫老師課的score表記錄;

delete from score where corse_id in(select cid from course left join teacher on course.teacher_id=teacher.tid where teacher.tname='孫')
35、刪除“1”同學的“2”課程的成績;
delete from score where corse_id = 1 and student_id = 2

參考:https://www.cnblogs.com/xiaoqianghuihui/p/6961131.html

(PS.對樣例資料進行了修改,對其中有錯誤的部分進行了修正。)

如對文中內容有異議,歡迎探討交流!QQ:285899326