日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > 数据库 >内容正文

数据库

SQL数据库面试题以及答案(50题)

發(fā)布時(shí)間:2023/12/2 数据库 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SQL数据库面试题以及答案(50题) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Student(Sid,Sname,Sage,Ssex)學(xué)生表

  • Sid:學(xué)號(hào)

  • Sname:學(xué)生姓名

  • Sage:學(xué)生年齡

  • Ssex:學(xué)生性別

Course(Cid,Cname,T#)課程表

  • Cid:課程編號(hào)

  • Cname:課程名稱

  • Tid:教師編號(hào)

SC(Sid,Cid,score)成績(jī)表

  • Sid:學(xué)號(hào)

  • Cid:課程編號(hào)

  • score:成績(jī)

Teacher(Tid,Tname)教師表

  • Tid:教師編號(hào):

  • Tname:教師名字

1、查詢“001”課程比“002”課程成績(jī)高的所有學(xué)生的學(xué)號(hào)

select?a.sid?from? (select?sid,score?from?sc?where?cid='001')a, (select?sid,score?from?sc?where?cid='002')b? where?a.sid?=?b.sid?and?a.score>b.score;

2、查詢平均成績(jī)大于60分的同學(xué)的學(xué)號(hào)和平均成績(jī)

select?sid,avg(score)?from?sc group?by?sid? having?avg(score)>60;

3、查詢所有同學(xué)的學(xué)號(hào)、姓名、選課數(shù)、總成績(jī)

select?s.sid,s.sname,count_cid?as?選課數(shù),? sum_score??as?總成績(jī) from?student?s left?join? (select?sid,count(cid)?as?count_cid,sum(score)?as?sum_score? from?sc?group?by?sid?)sc on?s.sid?=?sc.sid;

4、查詢姓‘李’的老師的個(gè)數(shù):

select?count(tname) from?teacher? where?tname?like?'李%';

5、查詢沒有學(xué)過(guò)“葉平”老師可的同學(xué)的學(xué)號(hào)、姓名:

select?s.sid,s.sname? from?student?as?s? where?s.sid?not?in?(select?DISTINCT?sid?from?sc?as?sc?where?sc.cid?in?(select?cid?from?course?as?c?left?join?teacher?as?t?on?c.tid?=?t.tid?where?t.tname?=?'葉平') );

6、查詢學(xué)過(guò)“葉平”老師所教的所有課的同學(xué)的學(xué)號(hào)、姓名:

select?s.sid,s.sname? from?student?as?s? where?s.sid?in?(select?distinct?sc.sid?from?sc?as?sc?where?sc.cid?in?(select?cid?from?course?as?c?left?join?teacher?as?t?on?c.tid?=?t.tid?where?t.tname?=?'葉平')group?by?sc.sid?HAVING?count(cid)=(select?count(cid)?from?course?as?c?left?join?teacher?as?t?on?c.tid?=?t.tid?where?t.tname?=?'葉平') );

7、查詢學(xué)過(guò)“011”并且也學(xué)過(guò)編號(hào)“002”課程的同學(xué)的學(xué)號(hào)、姓名:

SELECT?s.sid,s.sname? from?student?as?s? left?join?sc?as?sc?on?s.sid?=?sc.sid where?sc.cid?=?'001' and?EXISTS(select?*?from?sc?as?sc_2?where?sc.sid?=?sc_2.sid?and?sc_2.cid='002');select?s.sid,s.sname from?student?as?s? left?join?sc?as?sc? on?sc.sid?=?s.sid where?sc.cid?=?'001' and?s.sid?in?(select?sid?from?sc?as?sc_2?where?sc_2.cid='002'?and?sc_2.sid?=?sc.sid);

8、查詢課程編號(hào)“002”的成績(jī)比課程編號(hào)“001”課程低的所有同學(xué)的學(xué)號(hào)、姓名:

select?sid,sname from?(select?student.sid,student.sname,score,(select?score?from?sc?as?sc_2?where?sc_2.sid?=?student.sid?and?sc_2.cid?=?'002')?as?score2?from?student,sc?where?student.sid=sc.sid?and?cid?=?'001')?s_2 where?score2<score;

9、查詢所有課程成績(jī)小于60的同學(xué)的學(xué)號(hào)、姓名:

select?sid,sname from?student where?sid?not?in? (select?s.sid? from?student?s,sc? where?s.sid=sc.sid?and?score>60?);select?sid,sname from?student?s where?not?EXISTS?( select?s.sid?from?sc? where?sc.sid?=?s.sid?and?sc.score>60);

10、查詢沒有學(xué)全所有課的同學(xué)的學(xué)號(hào)、姓名:

select?s.sid,s.sname from?student?s?,sc?sc? where?s.sid?=?sc.sid group?by?s.sid,s.sname having?count(sc.cid)<( select?count(cid)? from?course);select?s.sid,s.sname from?student?s? right?join?sc?sc?on?s.sid?=?sc.sid group?by?s.sid,s.sname having?count(sc.cid)< (select?count(cid)?from?course);

11、查詢至少有一門課與學(xué)號(hào)為“1001”同學(xué)所學(xué)相同的同學(xué)的學(xué)號(hào)和姓名:

select?student.sid,sname from?student,sc? where?student.sid?=?sc.sid and?cid?in? (select?cid?from?sc?where?sid='1001');select?s.sid,s.sname from?sc?sc?left?join?student?as?s on?sc.sid?=?s.sid where?sc.cid?in?(select?cid?from?sc?where?sid='1001');select?sc_1.sid,s.sname from?sc?sc_1?left?join?student?as?s on?sc_1.sid?=?s.sid where? exists?(select?sc_2.cid?from?sc?as?sc_2? where?sc_1.cid?=?sc_2.cid? and?sc_2.sid?=?'1001');

12、查詢至少學(xué)過(guò)學(xué)號(hào)為“001”同學(xué)所有一門課的其他同學(xué)學(xué)號(hào)和姓名;

13、把“SC”表中“葉平”老師教的課的成績(jī)都更改為此課程的平均成績(jī):

update?sc?set?score?=? (select?avg(sc_2.score)?from?sc?sc_2?? where?sc_2.cid?=?sc.cid) where?cid?in? (select?c.cid?from?course?c? left?join?teacher?t?on?t.tid?=?c.tid? where?t.tname?=?'葉平');

14、查詢和“1002”號(hào)的同學(xué)學(xué)習(xí)的課程完全相同的其他同學(xué)學(xué)號(hào)和姓名:

select?sc_1.sid? from?(select?cid?from?sc?where?sid='1002')a left?join?sc?sc_1?on?a.cid?=?sc_1.cid where?sc_1.sid<>'1002'? group?by?sc_1.sid? having?count(sc_1.cid)?=? (select?count(cid)?from?sc?where?sid='1002');select?a.sid,s.sname?from? (select?sid,GROUP_CONCAT(cid?order?by?cid?separator?',')?as?cid_str? from?sc?where?sid='1002')b, (select?sid,GROUP_CONCAT(cid?order?by?cid?separator?',')?as?cid_str? from?sc?group?by?sid)a left?join?student?s? on?a.sid?=?s.sid where?a.cid_str?=?b.cid_str?and?a.sid<>'1002';

15、刪除學(xué)習(xí)“葉平”老師課的SC表記錄:

delete?from?sc?WHERE cid?in?( select?c.cid?from?course?c? LEFT?JOIN?teacher?t?on?c.tid=t.tid? where?t.tname?=?'葉平');

16、向SC表中插入一些記錄,這些記錄要求符合以下條件:沒有上過(guò)編號(hào)“003”課程的同學(xué)學(xué)號(hào)、002號(hào)課的平均成績(jī):

insert?into?sc?select?sid,'002', (select?avg(score)?from?sc?where?cid='0022') from?student? where?sid?not?in?(select?sid?from?sc?where?cid='002');

17、按平均成績(jī)從高到低顯示所有學(xué)生的“數(shù)據(jù)庫(kù)”、“企業(yè)管理”、“英語(yǔ)”三門的課程成績(jī),按如下形式顯示:學(xué)生ID,數(shù)據(jù)庫(kù),企業(yè)管理,英語(yǔ),有效課程數(shù),有效平均分:

select?sid?as?學(xué)生id, (SELECT?score?from?sc? where?sc.sid?=?t.sid?and?cid='004')?as?數(shù)據(jù)庫(kù), (select?score?from?sc? where?sc.sid?=?t.sid?and?cid='001')?as?企業(yè)管理, (select?score?from?sc? where?sc.sid?=?t.sid?and?cid='015')?as?英語(yǔ), count(cid)?as?有效課程數(shù),?avg(t.score)?as?平均成績(jī) from?sc?as?t? group?by?sid order?by?avg(t.score);

18、查詢各科成績(jī)最高和最低的分:以如下的形式顯示:課程ID,最高分,最低分

select?l.cid?as?課程id,l.score?as?最高分, r.score?as?最低分 from?sc?l,sc?r where?l.cid?=?r.cid and?l.score?=? (select?max(t.score)?from?sc?t? where?l.cid?=?t.cid?group?by?t.cid) and?r.score?=?(select?min(t.score)?from?sc?t? where?r.cid?=?t.cid?group?by?t.cid) order?by?l.cid;select?cid?as?課程id,max(score)?as?最高分, min(score)?as?最低分 from?sc? group?by?cid;

19、按各科平均成績(jī)從低到高和及格率的百分?jǐn)?shù)從高到低順序:

SELECT?t.cid?as?課程號(hào), c.cname?as?課程名, COALESCE(avg(score),0)?as?平均成績(jī), 100*sum(case? when?COALESCE(score,0)>=60? then?1?else?0?END)/count(*)?as?及格百分?jǐn)?shù) from?sc?t left?join?course?c? on?t.cid?=?c.cid group?by?t.cid order?by?100*sum(case? when?COALESCE(score,0)>=60? then?1?else?0?END)/count(*);

20、查詢?nèi)缦抡n程平均成績(jī)和及格率的百分?jǐn)?shù)(用”1行”顯示): 企業(yè)管理(001),馬克思(002),OO&UML (003),數(shù)據(jù)庫(kù)(004):

21、查詢不同老師所教不同課程平均分從高到低顯示:

select?t.tid?as?教師id, t.tname?as?教師姓名, sc.cid?as?課程id, avg(score)?as?平均成績(jī) from?sc?as?sc LEFT?JOIN?course?c?on?sc.cid?=?c.cid left?join?teacher?t?on?c.tid?=?t.tid group?by?sc.cid? order?by?avg(sc.score)?desc;

22、查詢?nèi)缦抡n程成績(jī)第3名到第6名的學(xué)生成績(jī)單:企業(yè)管理(001),馬克思(002),UML(003),數(shù)據(jù)庫(kù)(004):

23、統(tǒng)計(jì)下列各科成績(jī),各分?jǐn)?shù)段人數(shù):課程ID,課程名稱,[100-85],[85-70],[70-60],[ 小于60] :

select?sc.cid?as?課程id,cname?as?課程名稱, sum(case?when?score?between?85?and?100?then?1?else?0?end)?as?'[100-85]', sum(case?when?score?between?70?and?85?then?1?else?0?end)?as?'[85-70]', sum(case?when?score?between?60?and?70?then?1?else?0?end)?as?'[70-60]', sum(case?when?score<60?then?1?else?0?end)?as?'[60-0]' from?sc?as?sc? left?join?course?as?c on?sc.cid?=?c.cid group?by?sc.cid;

24、查詢學(xué)生平均成績(jī)及其名次:

select?1+(select?count(distinct?平均成績(jī))? from?(select?sid,avg(score)?as?平均成績(jī)? from?sc?group?by?sid)t1? where?平均成績(jī)>t2.平均成績(jī))?as?名次, sid?as?學(xué)生學(xué)號(hào),平均成績(jī)? from?(select?sid,avg(score)?平均成績(jī)?from?sc?group?by?sid)?as?t2 order?by?平均成績(jī)?desc;

25、查詢各科成績(jī)前三名的記錄(不考慮成績(jī)并列情況):

select?sid,cid,score from?sc?sc_1 where?( select?count(3)?from?sc?sc_2? where?sc_1.cid?=?sc_2.cid? and?sc_2.score>=sc_1.score)<=2? order?by?sc_1.cid );

26、查詢每門課程被選修的學(xué)生數(shù):

select?cid,?count(sid) from?sc? group?by?cid;

27、查詢出只選修一門課程的全部學(xué)生的學(xué)號(hào)和姓名:

select?sc.sid,s.sname, count(sc.cid)?as?課程數(shù) from?sc?as?sc LEFT?JOIN?student?as?s on?sc.sid?=?s.sid group?by?sc.sid having?count(sc.cid)=1;

28、查詢男生、女生人數(shù):

select?count(ssex)?as?男生人數(shù) from?student group?by?ssex having?ssex?=?'男'; select?count(2)?from?student where?ssex?=?'女';

29、查詢姓“張”的學(xué)生名單:

select?sid,sname from?student? where?sname?like?'張%';

30、查詢同名同姓的學(xué)生名單,并統(tǒng)計(jì)同名人數(shù):

select?sname,count(8) from?student? group?by?sname having?count(8)>1;

31、1981年出生的學(xué)生名單(注:student表中sage列的類型是datetime):

關(guān)注公眾號(hào)Java技術(shù)?;貜?fù)面試獲取更多面試題。

32、查詢平均成績(jī)大于85的所有學(xué)生的學(xué)號(hào)、姓名和平均成績(jī):

select?s.sname,sc.sid,avg(sc.score)?as?平均成績(jī) from?sc?as?sc left?join?student?as?s? on?sc.sid?=?s.sid group?by?sc.sid? having?avg(sc.score)>85;

33、查詢每門課程的平均成績(jī),結(jié)果按平均成績(jī)升序排序,平均成績(jī)相同時(shí),按課程號(hào)降序排列:

select?cid,avg(score) from?sc? group?by?cid order?by?avg(score),cid?desc;

34、查詢課程名稱為“數(shù)據(jù)庫(kù)”,且分?jǐn)?shù)低于60的學(xué)生名字和分?jǐn)?shù):

select?c.cname,s.sid,s.sname,sc.score from?course?c left?join?sc?on?sc.cid?=?c.cid LEFT?JOIN?student?s?on?s.sid?=?sc.sid where?c.cname?=?'數(shù)據(jù)庫(kù)'?and?sc.score<60;

35、查詢所有學(xué)生的選課情況:

select?sc.sid,sc.cid,s.sname,c.cname from?sc? LEFT?JOIN?course?c?on?sc.cid?=?c.cid left?join?student?s?on?sc.sid?=?s.sid;

36、查詢?nèi)魏我婚T課程成績(jī)?cè)?0分以上的姓名、課程名稱和分?jǐn)?shù):

select?distinct?s.sid,s.sname,sc.cid,sc.score from?sc? left?join?student?s?on?sc.sid?=?s.sid left?join?course?c?on?sc.cid?=?c.cid where?sc.score>70;

37、查詢不及格的課程,并按課程號(hào)從大到小的排列:

select?cid from?sc? where?score<60 ORDER?BY?cid;

38、查詢課程編號(hào)為“003”且課程成績(jī)?cè)?0分以上的學(xué)生的學(xué)號(hào)和姓名:

select?sc.sid,s.sname? from?sc? left?join?student?s?on?sc.sid?=?s.sid where?sc.cid?=?'003'?and?sc.score>80;

39、求選了課程的學(xué)生人數(shù):

select?count(2)?from? (select?distinct?sid?from?sc)a;

40、查詢選修“葉平”老師所授課程的學(xué)生中,成績(jī)最高的學(xué)生姓名及其成績(jī):

select?s.sname,sc.score from?sc?sc? left?join?student?s?on?sc.sid?=?s.sid left?join?course?c?on?sc.cid?=?c.cid left?join?teacher?t?on?c.tid?=?t.tid where?t.tname?=?'葉平' and?sc.score?=?( select?max(score)? from?sc?sc_1? where?sc.cid?=?sc_1.cid);

41、查詢各個(gè)課程及相應(yīng)的選修人數(shù):

select?cid,count(*)?from?sc?group?by?cid;

42、查詢不同課程成績(jī)相同的學(xué)生和學(xué)號(hào)、課程號(hào)、學(xué)生成績(jī):

select?DISTINCT?a.sid,a.cid,a.score from?sc?as?a?,sc?as?b? where?a.score?=?b.score and?a.cid?<>?b.cid;

43、查詢每門課程成績(jī)最好的前兩名:

關(guān)注公眾號(hào)Java技術(shù)棧回復(fù)面試獲取更多面試題。

44、統(tǒng)計(jì)每門課程的學(xué)生選修人數(shù)(超過(guò)10人的課程才統(tǒng)計(jì))。要求輸出課程號(hào)和選修人數(shù),查詢結(jié)果按人數(shù)降序排序,若人數(shù)相同,按課程號(hào)升序排序:

select?cid?as?課程號(hào),count(8)?as?選修人數(shù) from?sc group?by?cid HAVING?count(sid)>10 order?by?count(8)?desc,cid;

45、檢索至少選修兩門課程的學(xué)生學(xué)號(hào):

select?sid from?sc group?by?sid having?count(8)>=2;

46、查詢?nèi)繉W(xué)生選修的課程和課程號(hào)和課程名:

select?cid,cname from?course? where?cid?in?(select?cid?from?sc?group?by?cid);

47、查詢沒學(xué)過(guò)”葉平”老師講授的任一門課程的學(xué)生姓名:

select?sname? from?student? where?sid?not?in?(select?sid?from?sc,course,teacher?where?course.tid?=?teacher.tid?and?sc.cid?=?course.cid?and?teacher.tname='葉平' );

48、查詢兩門以上不及格課程的同學(xué)的學(xué)號(hào)以及其平均成績(jī):

select?sid,avg(COALESCE(score,0)) from?sc where?sid?in?(select?sid?from?sc?where?score<60?group?by?sid?having?count(8)>2 ) group?by?sid;

49、檢索“004”課程分?jǐn)?shù)小于60,按分?jǐn)?shù)降序排列的同學(xué)學(xué)號(hào):

select?sid,score from?sc where?cid='004' and?score<60 order?by?score?desc;

50、刪除“002”同學(xué)的“001”課程的成績(jī):

delete?from?sc where?sid?=?'002' and?cid?=?'001';

?

版權(quán)聲明:本文為博主原創(chuàng)文章,遵循 CC 4.0 BY-SA 版權(quán)協(xié)議,轉(zhuǎn)載請(qǐng)附上原文出處鏈接和本聲明。本文鏈接:https://blog.csdn.net/hundan_520520/article/details/54881208

總結(jié)

以上是生活随笔為你收集整理的SQL数据库面试题以及答案(50题)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。