MySQL測試題
一、表關系
請創建如下表,並創建相關約束
創建數據庫
create database school charset utf8;
建表
create table class(
cid int not null auto_increment,
caption char(32) not null,
primary key(cid)
);
create table student(
sid int not null auto_increment,
sname char(32) not null,
gender char(8) not null,
class_id int not null,
constraint fk_classid_classcid foreign key (class_id) references class(cid),
primary key(sid)
);
create table teacher(
tid int not null auto_increment,
tname char(32) not null,
primary key(tid)
);
create table course(
cid int not null auto_increment,
cname char(32) not null,
teach_id int not null,
constraint fk_teachid_teachertid foreign key (teach_id) references teacher(tid),
primary key(cid)
);
create table score(
sid int not null auto_increment,
student_id int not null,
course_id int not null,
number int not null,
constraint fk_studentid_studentsid foreign key (student_id) references student(sid),
constraint fk_courseid_coursecid foreign key (course_id) references course(cid),
primary key(sid)
);
插入數據
insert into class(caption) values('三年二班'),('一年三班'),('三年一班');
insert into student (sname,gender,class_id) values('鋼彈','女',1),('鐵錘','女',1),('山炮','男',2);
insert into teacher(tname) values('李大錘'),('葉平'),('楊偉');
insert into course(cname,teach_id) values('生物',1),('體育',1),('物理',2);
insert into score(student_id,course_id,number) values(1,1,60),(1,2,59),(2,2,100);
二、操作表
1、自行創建測試數據
2、查詢“生物”課程比“物理”課程成績高的所有學生的學號;
select a.student_id from (select * from score where course_id = (select cid from course where cname='生物'))a inner join (select * from score where course_id = (select cid from course where cname='物理'))b on a.student_id=b.student_id and a.number>b.number;
3、查詢平均成績大於60分的同學的學號和平均成績;
select student_id,AVG(number) from score group by student_id having AVG(number)>60;
4、查詢所有同學的學號、姓名、選課數、總成績;
select student.sid,student.sname,COUNT(score.course_id),SUM(score.number) from score left join student on student.sid=score.student_id group by student.sid;
5、查詢姓“李”的老師的個數;
select COUNT(a.tname) from (select * from teacher where tname like '李%') a;
6、查詢沒學過“葉平”老師課的同學的學號、姓名;
select j.sid,j.sname from (select student.sid,student.sname,b.sid as e,b.sname as f from student left join (select student.sid,student.sname from student inner join (select * from score where course_id in (select cid from course where teach_id=(select tid from teacher where tname='葉平')))a on student.sid=a.student_id group by sid)b on student.sid=b.sid)j where j.e is null;
7、查詢學過“001”並且也學過編號“002”課程的同學的學號、姓名;
select student.sid,student.sname from student inner join (select a.student_id from (select * from score where course_id=001)a inner join (select * from score where course_id=002)b on a.student_id=b.student_id)c on student.sid=c.student_id;
8、查詢學過“葉平”老師所教的所有課的同學的學號、姓名;
select student.sid,student.sname from student inner join (select * from score where course_id in (select cid from course where teach_id=(select tid from teacher where tname='葉平')))a on student.sid=a.student_id group by sid;
9、查詢課程編號“002”的成績比課程編號“001”課程低的所有同學的學號、姓名;
select student.sid,student.sname from student inner join (select a.student_id from (select * from score where course_id=001)a inner join (select * from score where course_id=002)b on a.student_id=b.student_id and a.number>b.number)c on student.sid=c.student_id;
10、查詢有課程成績小於60分的同學的學號、姓名;
select student.sid,student.sname from student inner join (select * from score where number<60 group by student_id)a on student.sid=a.student_id;
11、查詢沒有學全所有課的同學的學號、姓名;
select student.sid,student.sname from student inner join (select student_id,COUNT(course_id) as c from score group by student_id having c=(select COUNT(cid) as d from course))f on student.sid=f.student_id;
12、查詢至少有一門課與學號為“001”的同學所學相同的同學的學號和姓名;
select student.sid,student.sname from student inner join (select score.student_id from score,(select * from score where student_id=001)a where score.course_id=a.course_id group by score.student_id)b on student.sid=b.student_id;
13、查詢至少學過學號為“001”同學所選課程中任意一門課的其他同學學號和姓名;
select student.sid,student.sname from student inner join (select score.student_id from score,(select * from score where student_id=001)a where score.course_id=a.course_id and score.student_id !=001 group by score.student_id)b on student.sid=b.student_id;
14、查詢和“002”號的同學學習的課程完全相同的其他同學學號和姓名;
15、刪除學習“葉平”老師課的SC表記錄;
16、向SC表中插入一些記錄,這些記錄要求符合以下條件:①沒有上過編號“002”課程的同學學號;②插入“002”號課程的平均成績;
17、按平均成績從低到高顯示所有學生的“語文”、“數學”、“英語”三門的課程成績,按如下形式顯示: 學生ID,語文,數學,英語,有效課程數,有效平均分;
18、查詢各科成績最高和最低的分:以如下形式顯示:課程ID,最高分,最低分;
19、按各科平均成績從低到高和及格率的百分數從高到低順序;
20、課程平均分從高到低顯示(現實任課老師);
21、查詢各科成績前三名的記錄:(不考慮成績並列情況)
22、查詢每門課程被選修的學生數;
23、查詢出只選修了一門課程的全部學生的學號和姓名;
24、查詢男生、女生的人數;
25、查詢姓“張”的學生名單;
26、查詢同名同姓學生名單,並統計同名人數;
27、查詢每門課程的平均成績,結果按平均成績升序排列,平均成績相同時,按課程號降序排列;
28、查詢平均成績大於85的所有學生的學號、姓名和平均成績;
29、查詢課程名稱為“數學”,且分數低於60的學生姓名和分數;
30、查詢課程編號為003且課程成績在80分以上的學生的學號和姓名;
31、求選了課程的學生人數
32、查詢選修“楊艷”老師所授課程的學生中,成績最高的學生姓名及其成績;
33、查詢各個課程及相應的選修人數;
34、查詢不同課程但成績相同的學生的學號、課程號、學生成績;
35、查詢每門課程成績最好的前兩名;
36、檢索至少選修兩門課程的學生學號;
37、查詢全部學生都選修的課程的課程號和課程名;
38、查詢沒學過“葉平”老師講授的任一門課程的學生姓名;
39、查詢兩門以上不及格課程的同學的學號及其平均成績;
40、檢索“004”課程分數小於60,按分數降序排列的同學學號;
41、刪除“002”同學的“001”課程的成績;