10道有深度的mysql查询语句面试题
wptr33 2024-11-09 15:00 25 浏览
介绍
很多查询都可以用经典的学生-课程-成绩案例来表示,下面是一些我在笔试或面试中遇到的经典题型。这些题目没有涉及到索引,函数,存储过程等高级功能,都是一些基本的查询语句,但难度却不小。
表结构
- 学生表student(id,name)
- 列表项目
- 课程表course(id,name)
- 学生课程表student_course(sid,cid,score)
- sql
create table student( id int unsigned primary key auto_increment, name char(10) not null ); insert into student(name) values('张三'),('李四'); create table course( id int unsigned primary key auto_increment, name char(20) not null ); insert into course(name) values('语文'),('数学'); create table student_course( sid int unsigned, cid int unsigned, score int unsigned not null, foreign key (sid) references student(id), foreign key (cid) references course(id), primary key(sid, cid) ); insert into student_course values(1,1,80),(1,2,90),(2,1,90),(2,2,70);
问题
- 查询student表中重名的学生,结果包含id和name,按name,id升序
select id,name from student where name in ( select name from student group by name having(count(*) > 1) ) order by name;
我们经常需要查询某一列重复的行,一般通过group by(有重复的列)然后取count>1的值。 关系型数据库有他的局限性, 有些看似简单的查询写出来的sql很复杂,而且效率也会很低。
- 在student_course表中查询平均分不及格的学生,列出学生id和平均分
select sid,avg(score) as avg_score from student_course group by sid having(avg_score<60);
group by和having是最常考的。 where子句中不能用聚集函数作为条件表达式,但是having短语可以,where和having的区别在于对用对象不同,where作用于记录,having作用于组。
- 在student_course表中查询每门课成绩都不低于80的学生id
select distinct sid from student_course where sid not in (select sid from student_course where score < 80);
用到反向思想,其实就是数理逻辑中的?x:P和??x:?P是等价的。
- 查询每个学生的总成绩,结果列出学生姓名和总成绩 如果使用下面的sql会过滤掉没有成绩的人
select name,sum(score) total from student,student_course where student.id=student_course.sid group by sid;
更保险的做法应该是使用 左外连接
select name,sum(score) from student left join student_course on student.id=student_course.sid group by sid;
- 总成绩最高的学生,结果列出学生id和总成绩 下面的sql效率很低,因为要重复计算所有的总成绩。
select sid,sum(score) as sum_score from student_course group by sid having sum_score>=all( select sum(score) from student_course group by sid );
因为order by中可以使用聚集函数,最简单的方法是:
select sid,sum(score) as sum_score from student_course group by sid order by sum_score desc limit 1;
同理可以查总成绩的前三名。
- 在student_course表查询课程1成绩第2高的学生,如果第2高的不止一个则列出所有的学生
这是个查询 第N大数 的问题。 我们先查出第2高的成绩:
select min(score) from student_course where cid = 1 group by score order by score desc limit 2;
使用这种方式是错的,因为作用的先后顺序是group by->min->order by->limit,mysql提供了limit offset,size这种方式来取第N大的值,因此正确的做法是:
select score from student_course where cid = 1 group by score order by score desc limit 1,1;
然后再取出该成绩对应的学生:
select * from student_course where cid=1 and score = ( select score from student_course where cid = 1 group by score order by score desc limit 1,1 );
类似的,可以查询 某个值第N高 的记录。
- 在student_course表查询各科成绩最高的学生,结果列出学生id、课程id和对应的成绩 你可能会这样写:
select sid,cid,max(score) from student_course group by cid;
然而上面是不对的,因为 使用了group by的查询字段只能是group by中的字段或者聚集函数或者是每个分组内均相同的字段。 虽然不会报错,但是sid是无效的,如果去掉sid的话只能查出没门课程的最高分,不包含学生id。 本题的正确解法是使用相关嵌套查询:
select * from student_course as x where score>=( select max(score) from student_course as y where cid=x.cid );
相关嵌套查询也就是在进行内层查询的时候需要用到外层查询,有一些注意事项:
- 子查询一定要有括号
- as可以省略
- 使用相关查询;>=max等价于>=all,但是聚合函数比使用any或all效率高
- 在student_course表中查询每门课的前2名,结果按课程id升序,同一课程按成绩降序 这个问题也就是取每组的前N条纪录
select * from student_course x where 2>( select count(*) from student_course y where y.cid=x.cid and y.score>x.score ) order by cid,score desc;
这也是一个相关嵌套查询,对于每一个分数,如果同一门课程下只有0个、1个分数比这个分数还高,那么这个分数肯定是前2名之一
- 一个叫team的表,里面只有一个字段name,一共有4条纪录,分别是a,b,c,d,对应四个球队,两两进行比赛,用一条sql语句显示所有可能的比赛组合
- select a.name, b.name
- from team a, team b
- where a.name < b.name
其实就是一个表和自己连接查询。
- 题目:数据库中有一张如下所示的表,表名为sales。
年 季度 销售
1991 1 11
1991 2 12
1991 3 13
1991 4 14
1992 1 21
1992 2 22
1992 3 23
1992 4 24
要求:写一个SQL语句查询出如下所示的结果。
年 一季度 二季度 三季度 四季度
1991 11 12 13 14
1992 21 22 23 24
select 年, sum(case when 季度=1 then 销售量 else 0 end) as 一季度, sum(case when 季度=2 then 销售量 else 0 end) as 二季度, sum(case when 季度=3 then 销售量 else 0 end) as 三季度, sum(case when 季度=4 then 销售量 else 0 end) as 四季度 from sales group by 年;
同理,如果要查询每个人的每门课的成绩可以使用如下sql
create view temp as select student.name as sname,course.name as cname,score from student_course join (student,course) on(student_course.sid=student.id and student_course.cid=course.id); select sname, sum(case when cname='语文' then score else 0 end) as 语文, sum(case when cname='数学' then score else 0 end) as 数学 from temp group by sname;
当然如果新增了一门课,第二条sql就需要跟着变。
推荐阅读:
原文:https://segmentfault.com/a/1190000015570878
相关推荐
- Linux高性能服务器设计
-
C10K和C10M计算机领域的很多技术都是需求推动的,上世纪90年代,由于互联网的飞速发展,网络服务器无法支撑快速增长的用户规模。1999年,DanKegel提出了著名的C10问题:一台服务器上同时...
- 独立游戏开发者常犯的十大错误
-
...
- 学C了一头雾水该咋办?
-
学C了一头雾水该怎么办?最简单的方法就是你再学一遍呗。俗话说熟能生巧,铁杵也能磨成针。但是一味的为学而学,这个好像没什么卵用。为什么学了还是一头雾水,重点就在这,找出为什么会这个样子?1、概念理解不深...
- C++基础语法梳理:inline 内联函数!虚函数可以是内联函数吗?
-
上节我们分析了C++基础语法的const,static以及this指针,那么这节内容我们来看一下inline内联函数吧!inline内联函数...
- C语言实战小游戏:井字棋(三子棋)大战!文内含有源码
-
井字棋是黑白棋的一种。井字棋是一种民间传统游戏,又叫九宫棋、圈圈叉叉、一条龙、三子旗等。将正方形对角线连起来,相对两边依次摆上三个双方棋子,只要将自己的三个棋子走成一条线,对方就算输了。但是,有很多时...
- C++语言到底是不是C语言的超集之一
-
C与C++两个关系亲密的编程语言,它们本质上是两中语言,只是C++语言设计时要求尽可能的兼容C语言特性,因此C语言中99%以上的功能都可以使用C++完成。本文探讨那些存在于C语言中的特性,但是在C++...
- 在C++中,如何避免出现Bug?
-
C++中的主要问题之一是存在大量行为未定义或对程序员来说意外的构造。我们在使用静态分析器检查各种项目时经常会遇到这些问题。但正如我们所知,最佳做法是在编译阶段尽早检测错误。让我们来看看现代C++中的一...
- ESL-通过事件控制FreeSWITCH
-
通过事件提供的最底层控制机制,允许我们有效地利用工具箱,适时选择使用其中的单个工具。FreeSWITCH是一个核心交换与混合矩阵,它周围有几十个模块提供各种功能特性。我们完全控制了所有的即时信息,这些...
- 物理老师教你学C++语言(中篇)
-
一、条件语句与实验判断...
- C语言入门指南
-
当然!以下是关于C语言入门编程的基础介绍和入门建议,希望能帮你顺利起步:C语言入门指南...
- C++选择结构,让程序自动进行决策
-
什么是选择结构?正常的程序都是从上至下顺序执行,这就是顺序结构...
- C++特性使用建议
-
1.引用参数使用引用替代指针且所有不变的引用参数必须加上const。在C语言中,如果函数需要修改变量的值,参数必须为指针,如...
- C++程序员学习Zig指南(中篇)
-
1.复合数据类型结构体与方法的对比C++类:...
- 研一自学C++啃得动吗?
-
研一自学C++啃得动吗?在开始前我有一些资料,是我根据网友给的问题精心整理了一份「C++的资料从专业入门到高级教程」,点个关注在评论区回复“888”之后私信回复“888”,全部无偿共享给大家!!!个人...
- C++关键字介绍
-
下表列出了C++中的常用关键字,这些关键字不能作为变量名或其他标识符名称。1、autoC++11的auto用于表示变量的自动类型推断。即在声明变量的时候,根据变量初始值的类型自动为此变量选择匹配的...
- 一周热门
-
-
C# 13 和 .NET 9 全知道 :13 使用 ASP.NET Core 构建网站 (1)
-
因果推断Matching方式实现代码 因果推断模型
-
git pull命令使用实例 git pull--rebase
-
git pull 和git fetch 命令分别有什么作用?二者有什么区别?
-
面试官:git pull是哪两个指令的组合?
-
git 执行pull错误如何撤销 git pull fail
-
git fetch 和git pull 的异同 git中fetch和pull的区别
-
git pull 之后本地代码被覆盖 解决方案
-
还可以这样玩?Git基本原理及各种骚操作,涨知识了
-
git命令之pull git.pull
-
- 最近发表
- 标签列表
-
- git pull (33)
- git fetch (35)
- mysql insert (35)
- mysql distinct (37)
- concat_ws (36)
- java continue (36)
- jenkins官网 (37)
- mysql 子查询 (37)
- python元组 (33)
- mysql max (33)
- vba instr (33)
- mybatis 分页 (35)
- vba split (37)
- redis watch (34)
- python list sort (37)
- nvarchar2 (34)
- mysql not null (36)
- hmset (35)
- python telnet (35)
- python readlines() 方法 (36)
- munmap (35)
- docker network create (35)
- redis 集合 (37)
- python sftp (37)
- setpriority (34)