登录  /  注册

Java面试中出现率极高的数据库查询题

little bottle
发布: 2019-04-04 12:00:52
原创
2774人浏览过

经历过面试的程序员都知道,面试过程中面试官可能对你问出千奇百怪的问题,但万变不离其宗,终归也是要问道重点上来,比如像是某一工作的基本操作步骤以及代码的如何编写等等,本文讲的就是最最经典的数据库查询问题。

1554349207837929.jpg

基本表结构:

teacher(tno,tname) 教师表

student(sno,sname,sage,ssex)学生表

course(cno,cname,tno) 课程表

sc(sno,cno,score) 成绩表

NO.1查询课程1的成绩比课程2的成绩高的所有学生的学号

select a.sno from(select sno,score from sc where cno=1) a,(select sno,score from sc where cno=2) bwhere a.score>b.score and a.sno=b.sno
登录后复制

NO.2查询平均成绩大于60分的同学的学号和平均成绩

select a.sno as "学号", avg(a.score) as "平均成绩" from(select sno,score from sc) a group by sno having avg(a.score)>60
登录后复制

NO.2查询所有同学的学号、姓名、选课数、总成绩

select a.sno as 学号, b.sname as 姓名,count(a.cno) as 选课数, sum(a.score) as 总成绩from sc a, student bwhere a.sno = b.snogroup by a.sno, b.sname
登录后复制

或者:

selectstudent.sno as 学号, student.sname as 姓名, count(sc.cno) as 选课数, sum(score) as 总成绩from student left Outer join sc on student.sno = sc.snogroup by student.sno, sname
登录后复制

NO.3查询姓“张”的老师的个数

selectcount(distinct(tname)) from teacher where tname like '张%‘
登录后复制

或者:

select tname as "姓名", count(distinct(tname)) as "人数" from teacher where tname like'张%'group by tname
登录后复制

NO.4查询没学过“张三”老师课的同学的学号、姓名

select student.sno,student.sname from student
where sno not in (select distinct(sc.sno) from sc,course,teacher
where sc.cno=course.cno and teacher.tno=course.tno and teacher.tname='张三')
登录后复制

NO.5查询同时学过课程1和课程2的同学的学号、姓名

select sno, sname from studentwhere sno in (select sno from sc where sc.cno = 1)and sno in (select sno from sc where sc.cno = 2)
登录后复制

或者:

selectc.sno, c.sname from(select sno from sc where sc.cno = 1) a,(select sno from sc where sc.cno = 2) b,student cwhere a.sno = b.sno and a.sno = c.sno
登录后复制

或者:

select student.sno,student.sname from student,sc where student.sno=sc.sno and sc.cno=1and exists( select * from sc as sc_2 where sc_2.sno=sc.sno and sc_2.cno=2)
登录后复制

NO.6查询学过“李四”老师所教所有课程的所有同学的学号、姓名

select a.sno, a.sname from student a, sc bwhere a.sno = b.sno and b.cno in(select c.cno from course c, teacher d where c.tno = d.tno and d.tname = '李四')
登录后复制

或者:

select a.sno, a.sname from student a, sc b,(select c.cno from course c, teacher d where c.tno = d.tno and d.tname = '李四') ewhere a.sno = b.sno and b.cno = e.cno
登录后复制

NO.7查询课程编号1的成绩比课程编号2的成绩高的所有同学的学号、姓名

<p style="font-family: "Microsoft Yahei", "Hiragino Sans GB", Helvetica, "Helvetica Neue", 微软雅黑, Tahoma, Arial, sans-serif; white-space: normal;">select a.sno, a.sname from student a,</p><p style="font-family: "Microsoft Yahei", "Hiragino Sans GB", Helvetica, "Helvetica Neue", 微软雅黑, Tahoma, Arial, sans-serif; white-space: normal;">(select sno, score from sc where cno = 1) b,</p><p style="font-family: "Microsoft Yahei", "Hiragino Sans GB", Helvetica, "Helvetica Neue", 微软雅黑, Tahoma, Arial, sans-serif; white-space: normal;">(select sno, score from sc where cno = 2) c</p><p style="font-family: "Microsoft Yahei", "Hiragino Sans GB", Helvetica, "Helvetica Neue", 微软雅黑, Tahoma, Arial, sans-serif; white-space: normal;">where b.score > c.score and b.sno = c.sno and a.sno = b.sno<br/></p>
登录后复制

NO.8查询所有课程成绩小于60分的同学的学号、姓名

select sno,sname from studentwhere sno not in (select distinct sno from sc where score > 60)
登录后复制

NO.9查询至少有一门课程与学号为1的同学所学课程相同的同学的学号和姓名

select distinct a.sno, a.snamefrom student a, sc bwhere a.sno <> 1 and a.sno=b.sno andb.cno in (select cno from sc where sno = 1)
登录后复制

或者:

select s.sno,s.sname from student s,(select sc.sno from scwhere sc.cno in (select sc1.cno from sc sc1 where sc1.sno=1)and sc.sno<>1group by sc.sno)r1where r1.sno=s.sno
登录后复制

以上就是面试数据库相关工作灰常有可能遇到的题目,赶紧收藏起来,好好看看吧!

【推荐课程:MYSQL学习视频

以上就是Java面试中出现率极高的数据库查询题的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
相关标签:
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号