mysql:10道mysql查询语句面试题
表结构
- 学生表student(id,name)
- 课程表course(id,name)
- 学生课程表student_course(sid,cid,score)
创建表的sql代码
```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条纪录,类似的查询在csdn上也有征集答案
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就需要跟着变。
mysql:10道mysql查询语句面试题的更多相关文章
- 面试必备的10道MySQL题
MySQL 事务,是我们去面试中高级开发经常会被问到的问题,很多人虽然经常使用 MySQL,SQL 语句也写得很溜,但是面试的时候,被问到这些问题,总是不知从何说起.下面我们先来了解一下什么是 MyS ...
- mysql怎么限制某些查询语句的执行?
mysql怎么限制某些查询语句的执行? 比如某些sql语句执行时间很长,超过10s,怎么样超过10s就不让其执行? 后续更新中...
- mysql 存储过程:提供查询语句并返回查询执行影响的行数
mysql 存储过程:提供查询语句并返回查询执行影响的行数DELIMITER $$ DROP PROCEDURE IF EXISTS `p_get_select_row_number`$$ CREAT ...
- 当程序执行一条查询语句时,MySQL内部到底发生了什么? (说一下 MySQL 执行一条查询语句的内部执行过程?
先来个最基本的总结阐述,希望各位小伙伴认真的读一下,哈哈: 1)客户端(运行程序)先通过连接器连接到MySql服务器. 2)连接器通过数据库权限身份验证后,会先查询数据库缓存是否存在(之前执行过相同条 ...
- MySQL基础架构之查询语句执行流程
这篇笔记主要记录mysql的基础架构,一条查询语句是如何执行的. 比如,在我们从student表中查询一个id=2的信息 select * from student where id=2; 在解释这条 ...
- MYSQL数据库中的查询语句
查询的方法 *简单查询:select * from 表名 (* = 所有的) *读取特定列:select 字段一,字段二 from 表名 *条件查询:select * from 表名 where (多 ...
- 深入学习MySQL 01 一条查询语句的执行过程
在学习SpringCloud的同时,也在深入学习MySq中,听着<mysql45讲>,看着<高性能MySQL>,本系列文章是本人学习过程的总结,水平有限,仅供参考,若有不对之处 ...
- mysql系列-⼀条SQL查询语句是如何执⾏的?
⼀条SQL查询语句是如何执⾏的? ⼤体来说,MySQL 可以分为 Server 层和存储引擎层两部分 Server 层 Server 层包括连接器.查询缓存.分析器.优化器.执⾏器等,涵盖 MySQL ...
- 高性能MySql进化论(十一):常见查询语句的优化
总结一下常见查询语句的优化方式 1 COUNT 1. COUNT的作用 · COUNT(table.filed)统计的该字段非空值的记录行数 · ...
- mysql一些常用的查询语句总结
工作中会遇到一些比较有用的mysql查询语句,有了它,可以对mysql进行更全面的维护和管理,下面就写一下我记录的 1.按照字段ru_id查询dsc_order_goods表中ru_id出现次数由多到 ...
随机推荐
- 洛谷 P2376 [USACO09OCT]津贴Allowance
https://www.luogu.org/problemnew/show/P2376 看了题解做的,根本不会贪心.. #include<cstdio> #include<algor ...
- 114 Flatten Binary Tree to Linked List 二叉树转换链表
给定一个二叉树,使用原地算法将它 “压扁” 成链表.示例:给出: 1 / \ 2 5 / \ \ 3 4 6压扁后变成如下: ...
- 17997 Simple Counting 数学
17997 Simple Counting 时间限制:2000MS 内存限制:65535K提交次数:0 通过次数:0 题型: 编程题 语言: 不限定 Description Ly is craz ...
- IDEA Maven无法添加依赖到项目中
IDEA--------->File-------->Setting------------>Maven 勾上即可,OK啦! 完美解决了
- java threadLocal的初探
在网上找了半天,终于找到一篇靠谱的文章了. 文章地址:http://qifuguang.me/2015/09/02/[Java%E5%B9%B6%E5%8F%91%E5%8C%85%E5%AD%A6% ...
- git更新到远程服务器代码
git commit -a 在vi里输入一些内容 wq退出,git pull, git push
- js图片预加载以及延迟加载
当我们需要做图片轮播的时候,如果让图片提前下载到本地,用浏览器缓存起来,我们可以用Image对象: function preLoadImg(){ var img=new Image(); img.sr ...
- BeanUtils 工具类
一.BeanUtils 概述 BeanUtils 是阿帕奇提供的一套专门用于将一些数据封装到java对象中的工具类; 名词:javaBean:特定格式的java类称为java ...
- ef导航属性
https://msdn.microsoft.com/en-us/data/jj574232.aspx 场景是 A表中有B,B表中又C.都是一堆多的关系.怎样Mapping是个问题啊. var ...
- LR常用函数汇总
lr_start_transaction为性能分析标记事务的开始 lr_end_transaction为性能分析标记事务的结束 lr_rendezvous在 Vuser 脚本中设置集合点 lr_thi ...