首頁 > 資料庫 > mysql教程 > Java面試中出現率極高的資料庫查詢題

Java面試中出現率極高的資料庫查詢題

little bottle
發布: 2019-04-04 18:02:47
原創
3025 人瀏覽過

經歷過面試的程式設計師都知道,面試過程中面試官可能對你問出千奇百怪的問題,但萬變不離其宗,終歸也是要問道重點上來,比如像是某一工作的基本操作步驟以及程式碼的如何寫等等,本文講的就是最經典的資料庫查詢問題。

Java面試中出現率極高的資料庫查詢題

基本表結構:

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中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板