MySQL系列-查询语句练习题

先准备4张表:

学生表


老师表


课程表


成绩表


1.查询选修课程'8105'且成绩在80到90之间的所有记录

解析:简单题

答案:select * from sc where cno='8105' and grade <=90 and grade >=80;

2.查询成绩为79、89、或99的记录

解析:简单题

答案:2. select * from sc where grade in('79','89','99');

3.查询'9803'班的人数

解析:简单题

答案:select count(*) from student where class='9803';

4.查询至少有四名学生选修并且是以'8'开头的课程,以课程名和平均成绩的形式打印出来

解析:首先他要的是课程名,可以先来次join连接,在where里面选出8开头的课程,在根据课程进行分组查询。

答案:select cname,avg(grade) from sc join course on sc.cno=course.cno where sc.cno like '8%' group by sc.cno having count(*) >= 4;

5.查询最低分大于等于80,最高分小于等于95的sno与平均成绩

解析:可以按学号分组,然后在having里面筛选符合要求的。

答案: select sno,avg(grade) from sc group by sno having min(grade)>=80 and max(grade)<=95;

6.查询9803班所选的各个课程的课程号和平均成绩

解析:先来次join连接在where里面吧9803班的同学筛选出来,根据课程号进行分组

答案:select cno,avg(grade) from student join sc on student.sno=sc.sno where class='9803' group by cno;

10.查询选修课程人数大于等于4人的教师名字和课程名

解析:先把成绩表和教师表连、课程表接起来,按课程进行分组查询在having里面选出大于等于人的

答案: select tname,cname from teacher t join course c on t.tno=c.tno join sc s on c.cno=s.cno group by s.cno having count(*)>=4;

11.查询同学选修8105课程且成绩至少高于其选修编号为8104课程的同学的sno及8105课程成绩,从高到底排序

解析:关联子查询,如果子查询返回为空,那么grade>null也为false;举例select * from sc where grade>null,返回false

答案:select sno,grade from sc where cno='8105' and grade>(select grade from sc s2 where sc.sno=s2.sno and s2.cno='8104') order by grade desc;

12.查询同学选修8245课程且成绩高于所有选修8105课程的同学。

解析:这个比较简单,直接对比全部的8105课程就好了

答案: select * from sc where cno='8245' and grade>(select max(grade) from sc s2 where s2.cno='8105');

14.查询成绩比该课程平均成绩高的同学的成绩表

解析:关联子查询

答案:select * from sc where grade>(select avg(s2.grade) from sc s2 where sc.cno=s2.cno);

16.列出所有未讲课的教师的tname和dept

解析:这个题目可以使用in或者外连接来做

答案(使用in):select tname,dept from teacher where tno not in(select tno from course);

答案(使用外链接):select tname,dept from teacher t left join course c on t.tno=c.tno where c.tno is null;

17.列出男生大于等于2人的班号

解析:先在where里面把男生过滤出来然后再分组查询

答案:select class from student where sex='男' group by class having count(*)>=2;

18.查询不姓张的学生的记录

解析:简单题

答案:select * from student where sname not like '张%';

19.查询每门课程最高分的同学成绩记录

解析:这是一个比较容易错的题目,我们可能会写出分组查询如下

错误答案:select sno,cno,max(grade) from sc group by cno;

解析:但是分组查询得到的sno只是该组第一条记录的sno并不是分数最高的那一个。而且分组查询只返回一条记录,成绩可能会有并列的情况。正确答案应该使用关联子查询,逐一对比成绩。

答案:select * from sc where grade=(select max(grade) from sc s2 where sc.cno=s2.cno);

22.查询选修数据库的男同学的成绩表

解析:这个比较简单可以多写几种方法

答案:不写连接的写法:select * from sc where exists (select * from student s where sc.sno=s.sno and sex='男') and cno=(select cno from course c where cname='数据库');
      连接一次的写法:select sc.sno,cno,grade from student s join sc on s.sno=sc.sno where sex='男' and cno=(select cno from course where cname='数据库');

      连接两次的写法:select sc.sno,sc.cno,grade from student s join sc on s.sno=sc.sno join course c on sc.cno=c.cno where sex='男' and cname='数据库';

24.查询不讲授8104号课程的教师名字

解析:注意,课程和老师是多对多的关系。

答案:select tname from teacher where tno not in (select tno from course where cno='8104');

总结:其实很多的SQL语句也就这样,就是关联子查询、非关联子查询、自连接(也可以修改为关联子查询)、分组查询,但是实现起来效率可能大不一样,后面出博客专门讲解explain分析SQL语句的执行效率。

猜你喜欢

转载自blog.csdn.net/ufo___/article/details/80707670