Mysql学习笔记(查询语句练习题2)

表格详情:

student表:

Mysql学习笔记(查询语句练习题2)_第1张图片

teacher表:

Mysql学习笔记(查询语句练习题2)_第2张图片

course表:

Mysql学习笔记(查询语句练习题2)_第3张图片

score表:

Mysql学习笔记(查询语句练习题2)_第4张图片
 

Mysql查询语句练习:

23.查询“张旭“教师任课的学生成绩

select * from score where cno=(select cno from course where tno=(select tno from teacher where tname="张旭"));

select * from score where cno=(select x.cno from course x,teacher y where x.tno=y.tno and y.tname="张旭");

select A.* from score A join (teacher B,course C) on A.cno=C.cno and B.tno=C.tno and B.tname="张旭";

 

24.查询选修某课程的同学人数多于5人的教师姓名

select tname from teacher where tno in (select tno from course where cno in (select cno from score group by cno having count(cno)>5));

select tname from teacher where tno in (select x.tno from course x,score y where x.cno=y.cno group by y.cno having count(y.cno)>5);

select A.tname from teacher A join (course B,score C) on A.tno=B.tno and B.cno=C.cno group by C.cno having count(C.cno)>5;

 

25.查询95033班和95031班全体学生的记录

select * from score where sno in (select sno from student where class in("95033","95031"));

select A.* from score A join student B on A.sno=B.sno and B.class in ("95033","95031");

 

26.查询存在有85分以上成绩的课程Cno

select distinct cno from score where degree>85;

select cno from score group by cno having max(degree)>85;

 

27.查询出“计算机系“教师所教课程的成绩表

select * from score where cno in (select cno from course where tno in (select tno from teacher where depart="计算机系"));

select A.* from score A join (course B,teacher C) on A.cno=B.cno and B.tno=C.tno and C.depart="计算机系";

 

28.查询“计算机系”与“电子工程系“不同职称的教师的Tname和Prof

select tname,prof from teacher where depart="计算机系" and prof not in (select prof from teacher where depart = "电子工程系");

 

29.查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序

select * from score where cno="3-105" and degree> any(select degree from score where cno = "3-245") order by degree desc;

 

30.查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree

select * from score where cno="3-105" and degree> all(select degree from score where cno = "3-245");

 

31.查询所有教师和同学的name、sex和birthday.

select sname as name,ssex as sex,sbirthday as birthday from student union select tname ,tsex ,tbirthday  from teacher;

 

32.查询所有“女”教师和“女”同学的name、sex和birthday

select sname as name,ssex as sex,sbirthday as birthday from student where ssex="女" union select tname ,tsex ,tbirthday  from teacher where tsex="女";

 

33.查询成绩比该课程平均成绩低的同学的成绩表

select A.* from score A where A.degree<(select avg(degree) from score B where A.cno=B.cno);

 

34.查询所有任课教师的Tname和Depart

select tname,depart from teacher where tno in (select distinct tno from course);

select A.tname,A.depart from teacher A join course B on A.tno=B.tno;

 

35.查询所有未讲课的教师的Tname和Depart

select tname,depart from teacher where tno not in (select distinct tno from course);

 

36.查询至少有2名男生的班号

select class from student where ssex="男" group by class having count(class) > 1;

 

37.查询Student表中不姓“王”的同学记录

select * from student where sname not like '王%';

 

38.查询Student表中每个学生的姓名和年龄

select sname as "姓名",year(now())-year(sbirthday) as "年龄" from student;

 

39.查询Student表中最大和最小的Sbirthday日期值

select min(sbirthday) as "最大生日",max(sbirthday) as "最小生日" from student;

 

40.以班号和年龄从大到小的顺序查询Student表中的全部记录

select * from student order by class desc,sbirthday asc;

 

41.查询“男”教师及其所上的课程

select A.tname,B.cname from teacher A join course B on A.tno=B.tno and A.tsex="男";

 

42.查询最高分同学的Sno、Cno和Degree列

select sno,cno,degree from score A where degree=(select max(degree) from score B);

 

43.查询和“李军”同性别的所有同学的Sname

select sname from student where ssex=(select ssex from student where sname="李军");

 

44.查询和“李军”同性别并同班的同学Sname

select sname from student where ssex=(select ssex from student where sname="李军") and class=(select class from student where sname="李军");

 

45.查询所有选修“计算机导论”课程的“男”同学的成绩表

select * from score where sno in (select sno from student where ssex="男") and cno=(select cno from course where cname="计算机导论");

select A.* from score A join (student B,course C) on A.sno=B.sno and A.cno=C.cno and B.ssex="男" and C.cname="计算机导论";

 

你可能感兴趣的:(Mysql)