sql时间降序_SQL求职面试题

各初始表如下:

438fe23827edff094da589c7839497e0.png
course表

2dd66641df8c53ac3712e5dd713331c4.png
score表

2e1a4591ec2449ef6878c6477a31e344.png
student表

83e0f3604bf7b5095f5b12bccd3e663c.png
teacher表

下面的面试题主要基于上面的几张表

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.多表查询

95628dec9593237212c4a300c3a13d43.png
学生表

786ac3b48b4bb5d9eacab28c3ebcdd6f.png
近视学生表

不是近视眼的学生都有谁

select a.学号,a.姓名
from 学生表 as a left join 近视学生表 as b on a.学号=b.学生学号
where b.学生学号 is null;

7aa8a65292063d060ca9ffc3a61ad1b0.png

找出所有从不订购任何东西的客户

select a.ID,a.Name
from Customers as a left join Orders as b on a.ID=b.CustomerId
where b.CustomerId is null;

行列转换

c2b2dcd9f089086ce90583e1fdb5a244.png
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 学号;

5bffee085c064f10fd424fad8f108a14.png
结果1

9b1ff59708494cf9af7922946244ea10.png
结果2

b8206d73aab6f252e9ce151aafbc0c44.png
结果3

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练习题

df3da52a764d273e76187ea78f9683d8.png
details about movie databases

ec4964db67ffa7cb2172a825c2a5234a.png
movie databases:join operation

d42328c7e821999d39d62df8dbba502e.png
more join operation

0501919f087e62dfe7eb027570a1468c.png
inner join, left join, right join

4d4040810548b9f899a6d3f0d55a9df9.png
detail of Edinburgh Buses

224f73a9a2508fc1312ad75fde79c6a7.png
self join

自连接这一部分还需要多加练习。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值