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

66次阅读
没有评论

共计 2464 个字符,预计需要花费 7 分钟才能阅读完成。

自动写代码机器人,免费开通

这篇文章将为大家详细讲解有关 Java 面试中出现率极高的数据库查询题有哪些,丸趣 TV 小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

基本表结构:

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 的成绩高的所有同学的学号、姓名

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

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

关于 Java 面试中出现率极高的数据库查询题有哪些就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向 AI 问一下细节

丸趣 TV 网 – 提供最优质的资源集合!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-12-18发表,共计2464字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)