MYSQL select查询练习题

 

10、 查询Score表中的最高分的学生学号和课程号。(子查询或者排序)
 select sno,cno from score where degree=(select max(degree) from score)
 select * from score order by degree desc limit 0,1

12、查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。

 select avg(degree) from score where cno like'3%' and cno in(select cno from score group by cno having count(*)>4)
 select avg(degree) from score group by cno having count(*)>4 and cno like '3%'

18、 假设使用如下命令建立了一个grade表:
create table grade(low  int(3),upp  int(3),rank  char(1))
insert into grade values(90,100,’A’)
insert into grade values(80,89,’B’)
insert into grade values(70,79,’C’)
insert into grade values(60,69,’D’)
insert into grade values(0,59,’E’)
现查询所有同学的Sno、Cno和rank列。
 select sno,cno,rank from score,grade where degree between low and upp
19、  查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。
 (1)select * from score where cno = '3-105' and degree>(select max(degree) from score where sno='109')
 (2)select * from score where cno = '3-105' and degree>(select max(degree) from score where sno='109' and cno='3-105')

20、查询score中选学多门课程的同学中分数为非最高分成绩的记录。
 (1)select * from score where sno in(select sno from score group by sno having count(*)>1) and degree<(select max (degree) from score)

 (2)select * from score a where sno in(select sno from score group by sno having count(*)>1) and degree<(select max (degree) from score b where b.cno = a.cno)

28、查询“计算机系”与“电子工程系“不同职称的教师的Tname和Prof。
 select tname,prof from teacher where depart='计算机系'  and prof not in(select prof from teacher where depart='电子工程系')
union
select tname,prof from teacher where depart='电子工程系'  and prof not in(select prof from teacher where depart='计算机系')

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

select tname,prof from teacher a where prof not in(select prof from teacher b where a.depart!=b.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')
 select * from score where cno='3-105' and degree>(select min(degree) from score where cno='3-245')

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')
 select * from score where cno='3-105' and degree>(select max(degree) from score where cno='3-245')

36、查询至少有2名男生的班号。
 select class from student where ssex='男' group by class having count(*)>1

38、查询Student表中每个学生的姓名和年龄。
 select sname,year(now())-year(sbirthday) from student;
 select sname,(DATE_FORMAT(from_days(to_days(now())-to_days(sbirthday)),'%Y')+0) as age from student;

45、查询所有选修“计算机导论”课程的“男”同学的成绩表。
 select * from score where sno in(select sno from student where ssex='男') and cno in(select cno from course where cname='计算机导论');

 select student.sno,student.sname,student.ssex,student.sbirthday,student.class,course.cno,course.cname,score.degree from student,course,score where student.sno in (select sno from student where ssex='男') and course.cno in (select cno from course where cname='计算机导论') and student.sno=score.sno and course.cno=score.cno;

posted @ 2016-10-21 10:53  不见晨曦的占星师  阅读(598)  评论(0编辑  收藏  举报