Java面试题—初级(7)
作为一枚Java后端开发者,数据库知识必不可少,对数据库的掌握熟悉度的考察也是对这个人是否有扎实基本功的考察。特别对于初级开发者,面试可能不会去问框架相关知识,但是绝对不会不去考察数据库知识,这里收集一些常见类型的SQL语句,无论对于平常开发还是准备面试,都会有助益。
基本表结构:
student(sno,sname,sage,ssex)学生表
course(cno,cname,tno) 课程表
sc(sno,cno,score) 成绩表teacher(tno,tname) 教师表
101,查询课程1的成绩比课程2的成绩高的所有学生的学号
1 2 3 4 | select a.sno from ( select sno,score from sc where cno=1) a, ( select sno,score from sc where cno=2) b where a.score>b.score and a.sno=b.sno |
102,查询平均成绩大于60分的同学的学号和平均成绩
1 2 3 4 | select a.sno as "学号" , avg (a.score) as "平均成绩" from ( select sno,score from sc) a group by sno having avg (a.score)>60 |
103,查询所有同学的学号、姓名、选课数、总成绩
1 2 3 4 5 | select a.sno as 学号, b.sname as 姓名, count (a.cno) as 选课数, sum (a.score) as 总成绩 from sc a, student b where a.sno = b.sno group by a.sno, b.sname |
或者:
1 2 3 4 | selectstudent.sno as 学号, student.sname as 姓名, count (sc.cno) as 选课数, sum (score) as 总成绩 from student left Outer join sc on student.sno = sc.sno group by student.sno, sname |
104,查询姓“张”的老师的个数
1 | selectcount( distinct (tname)) from teacher where tname like '张%‘ |
或者:
1 2 3 4 | select tname as "姓名" , count ( distinct (tname)) as "人数" from teacher where tname like '张%' group by tname |
105,查询没学过“张三”老师课的同学的学号、姓名
1 2 3 | 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= '张三' ) |
106,查询同时学过课程1和课程2的同学的学号、姓名
1 2 3 | select sno, sname from student where sno in ( select sno from sc where sc.cno = 1) and sno in ( select sno from sc where sc.cno = 2) |
或者:
1 2 3 4 5 | selectc.sno, c.sname from ( select sno from sc where sc.cno = 1) a, ( select sno from sc where sc.cno = 2) b, student c where a.sno = b.sno and a.sno = c.sno |
或者:
1 2 | select student.sno,student.sname from student,sc where student.sno=sc.sno and sc.cno=1 and exists( select * from sc as sc_2 where sc_2.sno=sc.sno and sc_2.cno=2) |
107,查询学过“李四”老师所教所有课程的所有同学的学号、姓名
1 2 3 | select a.sno, a.sname from student a, sc b where a.sno = b.sno and b.cno in ( select c.cno from course c, teacher d where c.tno = d.tno and d.tname = '李四' ) |
或者:
1 2 3 | 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 = '李四' ) e where a.sno = b.sno and b.cno = e.cno |
108,查询课程编号1的成绩比课程编号2的成绩高的所有同学的学号、姓名
1 2 3 4 | select a.sno, a.sname from student a, ( select sno, score from sc where cno = 1) b, ( select sno, score from sc where cno = 2) c where b.score > c.score and b.sno = c.sno and a.sno = b.sno |
109,查询所有课程成绩小于60分的同学的学号、姓名
1 2 | select sno,sname from student where sno not in ( select distinct sno from sc where score > 60) |
110,查询至少有一门课程与学号为1的同学所学课程相同的同学的学号和姓名
1 2 3 4 | select distinct a.sno, a.sname from student a, sc b where a.sno <> 1 and a.sno=b.sno and b.cno in ( select cno from sc where sno = 1) |
或者:
1 2 3 4 5 6 7 | select s.sno,s.sname from student s, ( select sc.sno from sc where sc.cno in ( select sc1.cno from sc sc1 where sc1.sno=1) and sc.sno<>1 group by sc.sno)r1 where r1.sno=s.sno |

《《--扫描二维码关注他!
【Java知音】公众号,每天早上8:30为您准时推送一篇技术文章
在Java知音公众号内回复“面试题聚合”,送你一份Java面试题宝典。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?