共計(jì) 2464 個(gè)字符,預(yù)計(jì)需要花費(fèi) 7 分鐘才能閱讀完成。
自動(dòng)寫代碼機(jī)器人,免費(fèi)開通
這篇文章將為大家詳細(xì)講解有關(guān) Java 面試中出現(xiàn)率極高的數(shù)據(jù)庫查詢題有哪些,丸趣 TV 小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
基本表結(jié)構(gòu):
teacher(tno,tname) 教師表
student(sno,sname,sage,ssex) 學(xué)生表
course(cno,cname,tno) 課程表
sc(sno,cno,score) 成績表
NO.1 查詢課程 1 的成績比課程 2 的成績高的所有學(xué)生的學(xué)號(hào)
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 分的同學(xué)的學(xué)號(hào)和平均成績
select a.sno as 學(xué)號(hào) , avg(a.score) as 平均成績 from(select sno,score from sc) a group by sno having avg(a.score) 60
NO.2 查詢所有同學(xué)的學(xué)號(hào)、姓名、選課數(shù)、總成績
select a.sno as 學(xué)號(hào), b.sname as 姓名,count(a.cno) as 選課數(shù), sum(a.score) as 總成績 from sc a, student bwhere a.sno = b.snogroup by a.sno, b.sname
或者:
selectstudent.sno as 學(xué)號(hào), student.sname as 姓名, count(sc.cno) as 選課數(shù), sum(score) as 總成績 from student left Outer join sc on student.sno = sc.snogroup by student.sno, sname
NO.3 查詢姓“張”的老師的個(gè)數(shù)
selectcount(distinct(tname)) from teacher where tname like 張 %‘
或者:
select tname as 姓名 , count(distinct(tname)) as 人數(shù) from teacher where tname like 張 % group by tname
NO.4 查詢沒學(xué)過“張三”老師課的同學(xué)的學(xué)號(hào)、姓名
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 查詢同時(shí)學(xué)過課程 1 和課程 2 的同學(xué)的學(xué)號(hào)、姓名
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 查詢學(xué)過“李四”老師所教所有課程的所有同學(xué)的學(xué)號(hào)、姓名
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 查詢課程編號(hào) 1 的成績比課程編號(hào) 2 的成績高的所有同學(xué)的學(xué)號(hào)、姓名
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 分的同學(xué)的學(xué)號(hào)、姓名
select sno,sname from studentwhere sno not in (select distinct sno from sc where score 60)
NO.9 查詢至少有一門課程與學(xué)號(hào)為 1 的同學(xué)所學(xué)課程相同的同學(xué)的學(xué)號(hào)和姓名
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
關(guān)于 Java 面試中出現(xiàn)率極高的數(shù)據(jù)庫查詢題有哪些就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
向 AI 問一下細(xì)節(jié)
丸趣 TV 網(wǎng) – 提供最優(yōu)質(zhì)的資源集合!