各初始表如下:
下面的面试题主要基于上面的几张表
1.简单查询
查询姓孟老师的个数
select count(教师姓名)
from teacher
where 教师姓名 LIKE '孟%';
2.汇总分析
查询课程编号为'0002'的总成绩
select sum(成绩)
from score
where 课程号 = 0002;
查询选了课程的学生数
select count(distinct 学号)
from score;
查询各科成绩最高和最低的分
select 课程号,max(成绩),min(成绩)
from score
group by 课程号;
查询每门课程被选修的学生数
select 课程号,count(distinct 学号)
from score
group by 课程号;
查询男生,女生人数
select 性别,count(性别)
from student
group by 性别;
查询平均成绩大于60分学生的学号和平均成绩
select 学号,avg(成绩)
from score
group by 学号
having avg(成绩) >60;
查询至少选修两门课程的学生学号
select 学号,count(distinct 课程号)
from score
group by 学号
having count(distinct 课程号) >= 2;
查询同名同姓学生名单并统计同名人数
select 姓名,count(姓名)
from student
group by 姓名
having count(姓名) >1;
查询不及格的课程并按课程号从大到小的排列
select 课程号
from score
where 课程号<60
order by 课程号 desc;
查询每门课程的平均成绩,结果按平均成绩升序排序,平均成绩相同时,按课程号降序排列
select 课程号,avg(成绩)
from score
group by 课程号
order by avg(成绩) asc,课程号 desc;
检索课程编号为'0004'且分数小于60的学生学号,结果按分数降序排列
select 学号
from score
where 课程号 = 0004 and 成绩 <60
order by 成绩 desc;
统计每门课程的学生选修人数(超过两人的课程才统计),输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
select 课程号,count(学号)
from score
group by 课程号
having count(学号)>2
order by count(学号) desc,课程号 asc;
查询两门以上不及格课程的同学的学号及其平均成绩
select 学号,avg(成绩)
from score
where 成绩<60
group by 学号
having count(成绩) >=2;
3.复杂查询
查询所有课程成绩小于60分学生的学号,姓名
select 学号,姓名
from student
where 学号 in (select 学号 from score group by 学号 having max(成绩) <60);
查询没有学全所有课的学生的学号,姓名:以下两种方法均可以
select a.学号,a.姓名
from student as a left join score as b
on a.学号 = b.学号
group by a.学号,a.姓名
having count(a.学号)<(select count(课程号) from course);
select 学号,姓名
from student
where 学号 in (
select 学号
from score
group by 学号
having count(课程号) < (select count(课程号) from course));
查询出只选修了两门课程的全部学生的学号和姓名
select 学号,姓名
from student
where 学号 in (select 学号 from score group by 学号 having count(学号) =2);
1990年出生的学生名单
select 学号,姓名
from student
where year(出生日期)=1990;
分组取每组最大值,最小值,每组最大的n条(topN)记录
按课程号分组去成绩最大值所在行的数据
select*
from score as a
where 成绩 = (select max(成绩) from score as b where a.课程号 = b.课程号);
按课程号分组取成绩最小值所在行的数据
select*
from score as a
where 成绩 = (select max(成绩) from score as b where a.课程号 = b.课程号);
查询各科成绩前两名的记录
(select*
from score
where 课程号 = 0001
order by 成绩 desc
limit 2)
union all
(select*
from score
where 课程号 = 0002
order by 成绩 desc
limit 2)
union all
(select*
from score
where 课程号 = 0003
order by 成绩 desc
limit 2);
若想得到每组最小的n条记录,将order by子句按某列升序排列(asc)即可。
4.多表查询
不是近视眼的学生都有谁
select a.学号,a.姓名
from 学生表 as a left join 近视学生表 as b on a.学号=b.学生学号
where b.学生学号 is null;
找出所有从不订购任何东西的客户
select a.ID,a.Name
from Customers as a left join Orders as b on a.ID=b.CustomerId
where b.CustomerId is null;
行列转换
select 学号,'课程号0001','课程号0002','课程号0003'
from score;
select 学号,(case 课程号 when '0001' then 成绩 else null end) as '课程号0001',
(case 课程号 when '0002' then 成绩 else null end) as'课程号0002',
(case 课程号 when '0003' then 成绩 else null end) as'课程号0003'
from score;
select 学号,max(case 课程号 when '0001' then 成绩 else null end) as '课程号0001',
max(case 课程号 when '0002' then 成绩 else null end) as'课程号0002',
max(case 课程号 when '0003' then 成绩 else null end) as'课程号0003'
from score
group by 学号;
5.提高sql查询的效率
1.select子句中尽量避免使用*,且如果select*用于多表连接,会造成更大的成本开销
2.where子句比较符号左边避免函数,为提高效率,where子句中遇到函数或加减乘除的运算,可将其移到比较符号的右侧
3.尽量避免使用in和not in, in和not in也会导致数据库进行全表搜索,增加运行时间
4.尽量避免使用or,or同样会使数据库进行全表搜索
5.使用limit子句限制返回额数据行数
6.总结
通过此次学习,对之前的练习有了一次系统的回顾,也学习到了topN问题的解决方法,对sql面试题有了初步的了解,在之后的练习中也会多多注意如何提高sql查询的效率。
sqlzoo练习题
自连接这一部分还需要多加练习。