您好,欢迎来到网暖!

当前位置:网暖 » 站长资讯 » 建站基础 » 网络技术 » 文章详细 订阅RssFeed

互联网大厂面试揭秘:MySQL查询常考的十道面试题

来源:网络整理 浏览:313次 时间:2021-01-22

互联网大厂面试揭秘:MySQL查询常考的十道面试题

本文已收录GitHub,更有互联网大厂面试真题,面试攻略,高效学习资料等

表结构:
`student`('id'、'name'、'code'、'age'、'sex')学生表`teacher`('id'、'name')教师表`course`('id'、'name'、'teacher_id')课程表`score`('student_id'、'course_id'、'score')成绩表
问题:
  1. 查询001课程比002课程成绩高的所有学生的信息
  2. 查询所有课程成绩小于60分的同学的信息名
  3. 查询平均成绩大于60分的同学平均成绩和学生的信息
  4. 查询所有同学的信息、选课数、总成绩
  5. 查询没学过 “王老师” 课的同学的信息
  6. 查询学过“001”并且也学过编号“002”课程的同学的信息
  7. 查询没有学全所有课的同学的信息
  8. 查询至少有一门课与学号为“1001”的同学所学相同同学的信息
  9. 查询至少学过学号为1001的同学所有课程的 其他同学的信息
  10. 把“score”表中“王老师”教的课的成绩都更改为此课程的平均成绩
解决:

创建表

CREATE TABLE `student` (  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,  `name` varchar(30) DEFAULT NULL,  `code` varchar(15) DEFAULT NULL,  `age` int(11) DEFAULT NULL,  `sex` int(11) DEFAULT '1' COMMENT '1 男 2 女',  PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4;CREATE TABLE `teacher` (  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,  `name` varchar(30) DEFAULT '' COMMENT '老师名',  PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4;CREATE TABLE `course` (  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,  `name` varchar(30) DEFAULT NULL COMMENT '课程名',  `teache_id` int(11) DEFAULT NULL COMMENT '教师ID',  PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4;CREATE TABLE `score` (  `student_id` int(11) DEFAULT NULL COMMENT '学生ID',  `course_id` int(11) DEFAULT NULL COMMENT '课程ID',  `score` int(11) DEFAULT NULL COMMENT '成绩') ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
问题1: 查询001课程比002课程成绩高的所有学生的信息;
SELECT st.* FROM student st WHERE ( SELECT sc.`score` FROM score sc LEFT JOIN `course` co ON co.`id`=sc.`course_id` WHERE st.`id` = sc.`student_id` AND co.`name` = '001' ) > ( SELECT sc.`score` FROM score sc LEFT JOIN `course` co ON co.`id`=sc.`course_id` WHERE st.`id` = sc.`student_id` AND co.`name` = '002' );

分解:

1: 按题意理解、写的如下SQL

SELECT st.* FROM student st WHERE ( ) > ( );

2: 获取指定ID的学生的001课程的成绩

SELECT sc.score FROM score sc LEFT JOIN course co ON co.id=sc.course_id WHERE [指定ID] = sc.student_id AND co.name = '001';

3: 获取指定ID的学生的002课程的成绩

SELECT sc.score FROM score sc LEFT JOIN course co ON co.id=sc.course_id WHERE [指定ID] = sc.student_id AND co.name = '002';

4: 组装SQL

SELECT st.* FROM student st WHERE ( SELECT sc.score FROM score sc LEFT JOIN course co ON co.id=sc.course_id WHERE st.id = sc.student_id AND co.name = '001' ) > ( SELECT sc.score FROM score sc LEFT JOIN course co ON co.id=sc.course_id WHERE st.id = sc.student_id AND co.name = '002' );
问题2: 查询所有课程成绩小于60分的同学的信息;
SELECT st.* FROM `student` st WHERE st.id NOT IN ( SELECT sc.`student_id` FROM `score` sc WHERE sc.`score` > 60 );

分解:

1: 先是获取成绩大于60的同学 (题意是所有成绩都小于60的才符合、那么排除只要有一门成绩大于60的即可)

SELECT sc.student_id FROM score sc WHERE sc.score > 60;

2: 然后获取剩余的学生信息(通过NOT IN)

SELECT st.* FROM student st WHERE st.id NOT IN ( SELECT sc.student_id FROM score sc WHERE sc.score > 60 );
问题3: 查询平均成绩大于60分的同学的学号和平均成绩和学生的信息;
SELECT st.*,AVG( sc.`score`) as AvgScore  FROM `score` sc LEFT JOIN student st ON st.`id` = sc.`student_id` GROUP BY sc.`student_id` HAVING AVG( sc.`score` ) > 60;

注意:

HAVING 应用与对 where 和 group by 查询出来的分组进行过滤、查询出满足条件的分组结果。

  1. having 只能应用与 group by(分组统计语句中)
  2. where 是用于在初始表中筛选查询,having用于在where和group by 结果分组中查询
  3. having 子句中的每一个元素也必须出现在select列表中
  4. having语句可以使用聚合函数,而where不使用
问题4: 查询所有同学的信息、选课数、总成绩;
SELECT st.*,(SELECT COUNT( sc.`course_id`) FROM `score` sc WHERE sc.`student_id` = st.`id` ) courseNum, (SELECT SUM(sc.`score`) FROM `score` sc WHERE sc.`student_id` = st.`id`) scoreNum FROM student st;

分解:

1: 获取所有同学的信息

SELECT st.* FROM student st;

2: 获取选课数( 每一个同学都是一个特定的ID)

SELECT COUNT( sc.course_id) FROM score sc WHERE sc.student_id = [特定ID];

3: 获取总成绩(每一个同学的)

SELECT SUM(sc.score) FROM score sc WHERE sc.student_id = [特定ID];

4: 组装SQL

SELECT st.*,(SELECT COUNT( sc.course_id) FROM score sc WHERE sc.student_id = st.id ) courseNum, (SELECT SUM(sc.score) FROM score sc WHERE sc.student_id = st.id) scoreNum FROM student st;
问题5: 查询没学过 “王老师” 课的同学信息
SELECT st.* FROM `student` st WHERE st.`id` NOT IN ( SELECT sc.`student_id` FROM `score` sc LEFT JOIN `course` co ON co.`id` = sc.`course_id` LEFT JOIN `teacher` te ON te.`id` = co.`teache_id` WHERE te.`name` = '王老师' );

分解:

1: 根据题意、取反、先获取学过王老师课的同学

SELECT sc.student_id FROM score sc LEFT JOIN course co ON co.id = sc.course_id LEFT JOIN teacher te ON te.id = co.teache_id WHERE te.name = '王老师';

2: 然后在取反、获取剩余的学生信息即可

SELECT st.* FROM student st WHERE st.id NOT IN ( SELECT sc.student_id FROM score sc LEFT JOIN course co ON co.id = sc.course_id LEFT JOIN teacher te ON te.id = co.teache_id WHERE te.name = '王老师' );
问题6: 查询学过“001”也学过编号“002”课程的同学信息

解决方法1:

SELECT st.* FROM `student` st WHERE (SELECT count(*) FROM `score` sc LEFT JOIN `course` co ON co.`id` = sc.`course_id` WHERE sc.`student_id` = st.`id` AND co.`name` = '001') > 0 AND (SELECT count(*) FROM `score` sc LEFT JOIN `course` co ON co.`id` = sc.`course_id` WHERE sc.`student_id` = st.`id` AND co.`name` = '002') > 0;

分解:

1: 统计某一学生是否学过 001 课程的信息

SELECT count(*) FROM score sc LEFT JOIN course co ON co.id = sc.course_id WHERE sc.student_id = [特定ID] AND co.name = '001';

2: 统计某一学生是否学过 002 课程的信息

SELECT count(*) FROM score sc LEFT JOIN course co ON co.id = sc.course_id WHERE sc.student_id = [特定ID] AND co.name = '002';

3: 直接获取 条件1 和 条件2 同时成立的数据

SELECT st.* FROM student st WHERE (SELECT count() FROM score sc LEFT JOIN course co ON co.id = sc.course_id WHERE sc.student_id = st.id AND co.name = '001') > 0 AND (SELECT count() FROM score sc LEFT JOIN course co ON co.id = sc.course_id WHERE sc.student_id = st.id AND co.name = '002') > 0;

解决方法2:

SELECT * FROM `student` st WHERE st.`id` IN ( SELECT st1.student_id FROM ( SELECT sc.`student_id` FROM `score` sc LEFT JOIN `course` co ON co.`id` = sc.`course_id` WHERE co.`name` = '001' ) st1,( SELECT sc.`student_id` FROM `score` sc LEFT JOIN `course` co ON co.`id` = sc.`course_id` WHERE co.`name` = '002' )st2 WHERE st1.`student_id` = st2.`student_id` );

或者

SELECT st.* FROM `student` st,(SELECT st1.student_id FROM ( SELECT sc.`student_id` FROM `score` sc LEFT JOIN `course` co ON co.`id` = sc.`course_id` WHERE co.`name` = '001' ) st1,( SELECT sc.`student_id` FROM `score` sc LEFT JOIN `course` co ON co.`id` = sc.`course_id` WHERE co.`name` = '002' )st2 WHERE st1.`student_id` = st2.`student_id`) st3 WHERE st3.`student_id`= st.`id`;

分解:

1: 获取学过 001 课程的学生ID

SELECT sc.student_id FROM score sc LEFT JOIN course co ON co.id = sc.course_id WHERE co.name = '001';

2: 获取学过 001 课程的学生ID

SELECT sc.student_id FROM score sc LEFT JOIN course co ON co.id = sc.course_id WHERE co.name = '002'

3: 获取即学过 001 又学过 002 课程的学生ID

SELECT st1.student_id FROM ( SELECT sc.student_id FROM score sc LEFT JOIN course co ON co.id = sc.course_id WHERE co.name = '001' ) st1, ( SELECT sc.student_id FROM score sc LEFT JOIN course co ON co.id = sc.course_id WHERE co.name = '002' ) st2 WHERE st1.student_id = st2.student_id;

4:根据学生ID获取学生信息(可以有多种写法)

-- IN 写法:SELECT * FROM student st WHERE st.id IN ( SELECT st1.student_id FROM ( SELECT sc.student_id FROM score sc LEFT JOIN course co ON co.id = sc.course_id WHERE co.name = '001' ) st1,( SELECT sc.student_id FROM score sc LEFT JOIN course co ON co.id = sc.course_id WHERE co.name = '002' )st2 WHERE st1.student_id = st2.student_id );-- 把结果当作一个表、起别名再去查询:SELECT st.* FROM student st,(SELECT st1.student_id FROM ( SELECT sc.student_id FROM score sc LEFT JOIN course co ON co.id = sc.course_id WHERE co.name = '001' ) st1,( SELECT sc.student_id FROM score sc LEFT JOIN course co ON co.id = sc.course_id WHERE co.name = '002' )st2 WHERE st1.student_id = st2.student_id) st3 WHERE st3.student_id= st.id;
问题7: 查询没有学全所有课的同学的信息
SELECT st.* FROM `student` st WHERE (SELECT count(*) FROM `score` sc WHERE sc.`student_id` = st.`id`) < (SELECT count(*) FROM `course`);

分解:

1: 获取课的总数;

SELECT count(*) FROM course;

2: 获取每个人的学习的课的总数;

SELECT count(*) FROM score sc WHERE sc.student_id = [特定ID];

3: 然后查询的是 没有学全所有课的学生、也就是学习的课数小于总课数

(SELECT count(* ) FROM score sc WHERE sc.student_id = [特定ID]) < (SELECT count(*) FROM course );

4:获取学生的所有信息、组合sql 如下:

SELECT st.* FROM student st WHERE (SELECT count(* ) FROM score sc WHERE sc.student_id = st.id) < (SELECT count(*) FROM course);
问题8: 查询至少有一门课与学号为1001的同学所学相同同学的信息

解决方法 1:

SELECT DISTINCT st.* FROM `student` st INNER JOIN `score` sc ON sc.`student_id` = st.`id` WHERE sc.`course_id` IN ( SELECT sc.`course_id` FROM `student` st LEFT JOIN `score` sc ON sc.`student_id` = st.`id` WHERE st.`code` = '1001' );

分解:

先获取到学号为1001同学的所有学习课程、然后根据获取的课程ID去查所有的学生信息、然后 DISTINCT 去重即可。

1: 先获取到学号为1001同学的所有学习课程;

SELECT sc.course_id FROM student st LEFT JOIN score sc ON sc.student_id = st.id WHERE st.code = '1001';

2: 然后根据获取的课程ID去查所有的学生信息、同时去重即可;

SELECT DISTINCT st.* FROM student st INNER JOIN score sc ON sc.student_id = st.id WHERE sc.course_id IN ( SELECT sc.course_id FROM student st LEFT JOIN score sc ON sc.student_id = st.id WHERE st.code = '1001' );

解决方法 2:

SELECT st.* FROM `student` st WHERE st.`id` IN (  SELECT DISTINCT sc.`student_id` FROM `score` sc WHERE sc.`course_id` IN ( SELECT sc.`course_id` FROM `student` st LEFT JOIN `score` sc ON sc.`student_id` = st.`id` WHERE st.`code` = '1001' ) );

分解:

先获取学号为1001学生的课程、然后根据获取到课程ID获取学生ID、然后去重、然后获取学生信息。(嵌套子查询)

1: 先获取到学号为1001同学的所有学习课程;

SELECT sc.course_id FROM student st LEFT JOIN score sc ON sc.student_id = st.id WHERE st.code = '1001';

2: 然后根据获取到课程ID获取学生ID;

SELECT DISTINCT sc.student_id FROM score sc WHERE sc.course_id IN ( SELECT sc.course_id FROM student st LEFT JOIN score sc ON sc.student_id = st.id WHERE st.code = '1001' );

3: 然后获取学生信息

SELECT st.* FROM student st WHERE st.id IN ( SELECT DISTINCT sc.student_id FROM score sc WHERE sc.course_id IN ( SELECT sc.course_id FROM student st LEFT JOIN score sc ON sc.student_id = st.id WHERE st.code = '1001' ) );
问题9: 查询至少学过学号为1001的同学所有课程的 其他同学的信息
SELECT st.* FROM `student` st WHERE st.`id` IN ( SELECT sc1.`student_id` FROM ( SELECT sc.* FROM `score` sc WHERE sc.`course_id` IN ( SELECT sc.`course_id` FROM `student` st LEFT JOIN `score` sc ON sc.`student_id` = st.`id` WHERE st.`code` = '1001' ) ) sc1 GROUP BY sc1.`student_id` HAVING COUNT(*) = ( SELECT COUNT(*) FROM `student` st LEFT JOIN `score` sc ON sc.`student_id` = st.`id` WHERE st.`code` = '1001' ) );

分解:

1: 获取学号为 1001 的同学的所有课程ID;

SELECT sc.course_id FROM student st LEFT JOIN score sc ON sc.student_id = st.id WHERE st.code = '1001';

2: 获取对应课程的所有学习同学的ID、并且分组;

SELECT sc.student_id FROM score sc WHERE sc.course_id IN ( SELECT sc.course_id FROM student st LEFT JOIN score sc ON sc.student_id = st.id WHERE st.code = '1001' ) GROUP BY sc.student_id;

到此为止发现问题:只学了其中一门的也被查询出来了、应该去掉.

3: 获取学号为 1001 的同学所学课程数量

SELECT COUNT(*) FROM student st LEFT JOIN score sc ON sc.student_id = st.id WHERE st.code = '1001';

4: 所以所有的符合条件的学生的ID集为:

SELECT sc.student_id FROM score sc WHERE sc.course_id IN ( SELECT sc.course_id FROM student st LEFT JOIN score sc ON sc.student_id = st.id WHERE st.code = '1001' ) GROUP BY sc.student_id HAVING COUNT() = ( SELECT COUNT() FROM student st LEFT JOIN score sc ON sc.student_id = st.id WHERE st.code = '1001' );

5: 组装SQL、查询学生信息。

SELECT st.* FROM student st WHERE st.id IN ( SELECT sc.student_id FROM score sc WHERE sc.course_id IN ( SELECT sc.course_id FROM student st LEFT JOIN score sc ON sc.student_id = st.id WHERE st.code = '1001' ) GROUP BY sc.student_id HAVING COUNT() = ( SELECT COUNT() FROM student st LEFT JOIN score sc ON sc.student_id = st.id WHERE st.code = '1001' ) );
问题10: 把“score”表中“王老师的成绩都更改为此课程的平均成绩
UPDATE `score` sc SET sc.`score` = ( SELECT AVG(sc1.`score`) avgScore FROM (SELECT sc.* FROM `score` sc LEFT JOIN `course` co ON co.`id` = sc.`course_id` LEFT JOIN `teacher` te ON te.`id` = co.`teache_id` WHERE te.`name` = '王老师' ) sc1 ) WHERE sc.`course_id` = ( SELECT co.`id` FROM `course` co LEFT JOIN `teacher` te ON te.`id` = co.`teache_id` WHERE te.`name` = '王老师' );

分解:

1: 理解为修改特定ID的数据

UPDATE score sc SET sc.score = () WHERE sc.course_id = ();

2: 要修改的数据( 获取“score”表中“王老师”教的课的成绩)

SELECT sc.* FROM score sc LEFT JOIN course co ON co.id = sc.course_id LEFT JOIN teacher te ON te.id = co.teache_id WHERE te.name = '网王老师'

3: 确定要修改的值(获取要修改的数据的平均值)

SELECT AVG(sc1.score) avgScore FROM (SELECT sc.* FROM score sc LEFT JOIN course co ON co.id = sc.course_id LEFT JOIN teacher te ON te.id = co.teache_id WHERE te.name = '王老师' ) sc1

4: 确定修改的条件(获取王老师所带课程的ID)

SELECT co.* FROM course co LEFT JOIN teacher te ON te.id = co.teache_id WHERE te.name = '王老师'

5: 组装SQL即可

UPDATE score sc SET sc.score = ( SELECT AVG(sc1.score) avgScore FROM (SELECT sc.* FROM score sc LEFT JOIN course co ON co.id = sc.course_id LEFT JOIN teacher te ON te.id = co.teache_id WHERE te.name = '王老师' ) sc1 ) WHERE sc.course_id = ( SELECT co.id FROM course co LEFT JOIN teacher te ON te.id = co.teache_id WHERE te.name = '王老师' );

推荐站点

  • 腾讯腾讯

    腾讯网(www.QQ.com)是中国浏览量最大的中文门户网站,是腾讯公司推出的集新闻信息、互动社区、娱乐产品和基础服务为一体的大型综合门户网站。腾讯网服务于全球华人用户,致力成为最具传播力和互动性,权威、主流、时尚的互联网媒体平台。通过强大的实时新闻和全面深入的信息资讯服务,为中国数以亿计的互联网用户提供富有创意的网上新生活。

    www.qq.com
  • 搜狐搜狐

    搜狐网是全球最大的中文门户网站,为用户提供24小时不间断的最新资讯,及搜索、邮件等网络服务。内容包括全球热点事件、突发新闻、时事评论、热播影视剧、体育赛事、行业动态、生活服务信息,以及论坛、博客、微博、我的搜狐等互动空间。

    www.sohu.com
  • 网易网易

    网易是中国领先的互联网技术公司,为用户提供免费邮箱、游戏、搜索引擎服务,开设新闻、娱乐、体育等30多个内容频道,及博客、视频、论坛等互动交流,网聚人的力量。

    www.163.com
  • 新浪新浪

    新浪网为全球用户24小时提供全面及时的中文资讯,内容覆盖国内外突发新闻事件、体坛赛事、娱乐时尚、产业资讯、实用信息等,设有新闻、体育、娱乐、财经、科技、房产、汽车等30多个内容频道,同时开设博客、视频、论坛等自由互动交流空间。

    www.sina.com.cn
  • 百度一下百度一下

    百度一下,你就知道

    www.baidu.com