Java面試中出現率極高的數據庫查詢題

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

Java面試中出現率極高的數據庫查詢題

基本表結構:

teacher(tno,tname) 教師表

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

立即學習Java免費學習筆記(深入)”;

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 sans gb helvetica neue tahoma arial sans-serif white-space: normal>select a.sno, a.sname from student a,</p><p style="font-family: " microsoft yahei sans gb 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 sans gb 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 sans gb helvetica neue tahoma arial sans-serif white-space: normal>where b.score &gt; 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?&gt;?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.sno1group?by?sc.sno)r1where?r1.sno=s.sno

? 版權聲明
THE END
喜歡就支持一下吧
點贊13 分享