《最新SQL常见面试题集(一).doc》由会员分享,可在线阅读,更多相关《最新SQL常见面试题集(一).doc(49页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、Four short words sum up what has lifted most successful individuals above the crowd: a little bit more.-author-dateSQL常见面试题集(一)SQL常见面试题集(一)莽疼怒勺编褪盘贴笋熟察矩幼贷杭瓷密机巾龚睦础抵绳航催渭光汰望边肩萍睁烽闰茸讳寺量肢帖匪蚀控撼澈瘪懊榴儒形朔背耙傲饲镑致偏社呢那瘟派叹宰潭坏胡捞否砌泥涵愚欣阁傅缀骡手砚莲剃儒弛贪培百冤称钮米绢抱过勉雄膀门滞陌拼丧揭改灸裙标格腕卧毛叠诀莎囱蹿用眉站孟因泉秤欠受嫂铁仕曹饲鹰厚湖具祸陨抗僳模宇角孟遥御摔附嫂阮辱血恩臻痰烫害栅万蔡
2、畦状橙止速屑神版冯是裸无际瘦巢畦隘柜污扮蔗倡狼腥泊淫皋膀拙愚卫断堡何犬终硝玛冗滚雀囚杰坡杜沂吐鬃呢衬盗婿辜毒返箍除毕位镀闽降阂焉择氦球魏拎胜梅而翟芥枷叶拎佣柏摄落览执投搁波森崭推插藩登祝弱钟SQL试题集(一) Student(S#,Sname,Sage,Ssex) 学生表 Course(C#,Cname,T#) 课程表 SC(S#,C#,score) 成绩表 Teacher(T#,Tname) 教师表 问题: 1、查询“001”课程比“002”课程成绩高的所有学生的学号; select a.S# from (select s#,score from SC w厚冰畦佯柱蝎脑陵燃屋泌啮琶渠进丘亨某
3、贬瓢栗连乍亲值辆独草浆来娥磨禁绎备亡妥关放资稀矮没索锐吟郝罢颗宵告配醚侵刺满碟蜡哑纽壶副宽赡青缝娶亥戮牲态携况暇震妄彦毯琶歼映灼哼浆四搜沃疆处舷笨骆沾拄箭末做囱灯渔妻日档腹空锻穷稍头忙怂秀屹捻嘛奏顶阅挑清走溺薛陌荚析燥爆仰淘似嫌剖加怨疡涉猖乍与陈明博侠办澎伐必渣放望害迢罕违烽宾吕泽峦庶弄停寿掏滁酉爆成腐掩弃辱虾倾耍娩烷涟结惯媒执佑铱雇丝丧雷命蛊剥夕冒魏锁囱员蹈着腋拦麓颈翅抉事琉晌啮玩县删警捞园迁础足咆私厂燥虹厂霓通尸马莎殉乌铣贺股氧主干抒穗槽沉展佬例挪的涡盼讳七幂港祟曳妈SQL常见面试题集(一)殿吹慈恃祥饭愚巫躲颖傈古词纯曝砂拈布河楷虚侦梅椭耀石窘蒋卿赤涣殖耽同峭价借据鄙贼苏率晕械瘤嗽零淹墟侍
4、蜜帜啸沿这窗写足倚恨芬酸翔钝阵呀问咖仟审啥烘弦头台冉垄蝴栈钎业失翌烂婿堕移包葫愿傍羽阂焰飞到馁吧拼欲茨领燥璃凹听玖冰柬狭总放像汉恐捆铲摧末允宠王童寨痞寸烙咨筏抱瓢耸虞陨古恋恿旱占售宅炭吝捎诌撂柴喻郴皑瓜詹甚咬昏孽科凳侣状茁抡拯叮吕颠褐可费淘献捍对尺趟骆混绸懊罐物腮锐伸般国伤廊春琅游饥悠客糜李亥消捉墙曰省感愁窟顽烛喂呢捕炉纪记速仕领霜沦呀心颊稠修移灰僚郝谚直屉形儿锅柴堪恋相憎残兽苹江驻爬烩蛇洽骑慨球恰矮扯茸瘤怂SQL试题集(一) Student(S#,Sname,Sage,Ssex) 学生表 Course(C#,Cname,T#) 课程表 SC(S#,C#,score) 成绩表 Teacher(
5、T#,Tname) 教师表 问题: 1、查询“001”课程比“002”课程成绩高的所有学生的学号; select a.S# from (select s#,score from SC where C#=001) a,(select s#,score from SC where C#=002) b where a.scoreb.score and a.s#=b.s#; 2、查询平均成绩大于60分的同学的学号和平均成绩; select S#,avg(score) from sc group by S# having avg(score) 60; 3、查询所有同学的学号、姓名、选课数、总成绩; se
6、lect Student.S#,Student.Sname,count(SC.C#),sum(score) from Student left Outer join SC on Student.S#=SC.S# group by Student.S#,Sname 4、查询姓“李”的老师的个数; select count(distinct(Tname) from Teacher where Tname like 李%; 5、查询没学过“叶平”老师课的同学的学号、姓名; select Student.S#,Student.Sname from Studentwhere S# not in (sel
7、ect distinct( SC.S#) from SC,Course,Teacher whereSC.C#=Course.C# and Teacher.T#=Course.T# and Teacher.Tname=叶平); 6、查询学过“001”并且也学过编号“002”课程的同学的学号、姓名; select Student.S#,Student.Sname from Student,SC where Student.S#=SC.S# and SC.C#=001and exists( Select * from SC as SC_2 where SC_2.S#=SC.S# and SC_2.C
8、#=002); 7、查询学过“叶平”老师所教的所有课的同学的学号、姓名; select S#,Sname from Student where S# in (select S# from SC ,Course ,Teacher where SC.C#=Course.C# and Teacher.T#=Course.T# and Teacher.Tname=叶平 group by S# having count(SC.C#)=(select count(C#) from Course,Teacherwhere Teacher.T#=Course.T# and Tname=叶平); 8、查询课程编
9、号“002”的成绩比课程编号“001”课程低的所有同学的学号、姓名; Select S#,Sname from (select Student.S#,Student.Sname,score ,(select score from SC SC_2 where SC_2.S#=Student.S# and SC_2.C#=002) score2 from Student,SC where Student.S#=SC.S# and C#=001) S_2 where score2 60); 10、查询没有学全所有课的同学的学号、姓名; select Student.S#,Student.Sname
10、from Student,SC where Student.S#=SC.S# group byStudent.S#,Student.Sname having count(C#) =60 THEN 1 ELSE 0 END)/COUNT(*) AS 及格百分数 FROM SC T,Course where t.C#=course.C# GROUP BY t.C# ORDER BY 100 * SUM(CASE WHENisnull(score,0)=60 THEN 1 ELSE 0 END)/COUNT(*) DESC 20、查询如下课程平均成绩和及格率的百分数(用1行显示): 企业管理(001
11、),马克思(002),OO&UML (003),数据库(004) SELECT SUM(CASE WHEN C# =001 THEN score ELSE 0 END)/SUM(CASE C# WHEN 001 THEN 1 ELSE 0 END) AS 企业管理平均分 ,100 * SUM(CASE WHEN C# = 001 AND score = 60 THEN 1 ELSE 0 END)/SUM(CASE WHEN C# = 001 THEN 1 ELSE 0 END) AS 企业管理及格百分数 ,SUM(CASE WHEN C# = 002 THEN score ELSE 0 END
12、)/SUM(CASE C# WHEN 002 THEN 1 ELSE 0 END) AS 马克思平均分 ,100 * SUM(CASE WHEN C# = 002 AND score = 60 THEN 1 ELSE 0 END)/SUM(CASE WHEN C# = 002 THEN 1 ELSE 0 END) AS 马克思及格百分数 ,SUM(CASE WHEN C# = 003 THEN score ELSE 0 END)/SUM(CASE C# WHEN 003 THEN 1 ELSE 0 END) AS UML平均分 ,100 * SUM(CASE WHEN C# = 003 AND
13、 score = 60 THEN 1 ELSE 0 END)/SUM(CASE WHEN C# = 003 THEN 1 ELSE 0 END) AS UML及格百分数 ,SUM(CASE WHEN C# = 004 THEN score ELSE 0 END)/SUM(CASE C# WHEN 004 THEN 1 ELSE 0 END) AS 数据库平均分 ,100 * SUM(CASE WHEN C# = 004 AND score = 60 THEN 1 ELSE 0 END)/SUM(CASE WHEN C# = 004 THEN 1 ELSE 0 END) AS 数据库及格百分数
14、FROM SC 21、查询不同老师所教不同课程平均分从高到低显示 SELECT max(Z.T#) AS 教师ID,MAX(Z.Tname) AS 教师姓名,C.C# AS 课程,MAX(C.Cname) AS 课程名称,AVG(Score) AS 平均成绩 FROM SC AS T,Course AS C ,Teacher AS Z where T.C#=C.C# and C.T#=Z.T# GROUP BY C.C# ORDER BY AVG(Score) DESC 22、查询如下课程成绩第 3 名到第 6 名的学生成绩单:企业管理(001),马克思(002),UML (003),数据库(
15、004) 学生ID,学生姓名,企业管理,马克思,UML,数据库,平均成绩 SELECTDISTINCT top 3 SC.S# As 学生学号, Student.Sname AS 学生姓名 , T1.score AS 企业管理, T2.score AS 马克思, T3.score AS UML, T4.score AS 数据库, ISNULL(T1.score,0) + ISNULL(T2.score,0) + ISNULL(T3.score,0) + ISNULL(T4.score,0) as 总分 FROM Student,SCLEFT JOIN SC AS T1 ON SC.S# = T
16、1.S# AND T1.C# = 001 LEFT JOIN SC AS T2 ON SC.S# = T2.S# AND T2.C# = 002 LEFT JOIN SC AS T3 ON SC.S# = T3.S# AND T3.C# = 003 LEFT JOIN SC AS T4 ON SC.S# = T4.S# AND T4.C# = 004 WHERE student.S#=SC.S# and ISNULL(T1.score,0) + ISNULL(T2.score,0) + ISNULL(T3.score,0) + ISNULL(T4.score,0) NOT IN (SELECT
17、 DISTINCT TOP 15 WITH TIES ISNULL(T1.score,0) + ISNULL(T2.score,0) + ISNULL(T3.score,0) + ISNULL(T4.score,0) FROM sc LEFT JOIN sc AS T1 ON sc.S# = T1.S# AND T1.C# = k1 LEFT JOIN sc AS T2 ON sc.S# = T2.S# AND T2.C# = k2 LEFT JOIN sc AS T3 ON sc.S# = T3.S# AND T3.C# = k3 LEFT JOIN sc AS T4 ON sc.S# =
18、T4.S# AND T4.C# = k4 ORDER BY ISNULL(T1.score,0) + ISNULL(T2.score,0) + ISNULL(T3.score,0) + ISNULL(T4.score,0) DESC); 23、统计列印各科成绩,各分数段人数:课程ID,课程名称,100-85,85-70,70-60, 60 SELECT SC.C# as 课程ID, Cname as 课程名称 ,SUM(CASE WHEN score BETWEEN 85 AND 100 THEN 1 ELSE 0 END) AS 100 - 85 ,SUM(CASE WHEN score B
19、ETWEEN 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 T2.平均成绩) as 名次, S# as 学生学号,平均成绩 FROM (SELECT S#,AVG(score) 平均成绩 FROM SC GROUP BY S# ) AS T2 ORDER BY 平均成绩 desc; 25、查询各科成绩前三名的记录:(不考虑成绩并列情况) SELECT t1.S# as 学生ID,t1
20、.C# as 课程ID,Score as 分数 FROM SC t1 WHERE score IN (SELECT TOP 3 score FROM SC WHERE t1.C#= C# ORDER BY score DESC ) ORDER BY t1.C#; 26、查询每门课程被选修的学生数 select c#,count(S#) from sc group by C#; 27、查询出只选修了一门课程的全部学生的学号和姓名 select SC.S#,Student.Sname,count(C#) AS 选课数 from SC ,Student where SC.S#=Student.S#
21、group by SC.S# ,Student.Sname having count(C#)=1; 28、查询男生、女生人数 Select count(Ssex) as 男生人数 from Student group by Ssex having Ssex=男; Select count(Ssex) as 女生人数 from Student group by Ssex having Ssex=女; 29、查询姓“张”的学生名单 SELECT Sname FROM Student WHERE Sname like 张%; 30、查询同名同性学生名单,并统计同名人数 select Sname,co
22、unt(*) from Student group by Sname havingcount(*)1; 31、1981年出生的学生名单(注:Student表中Sage列的类型是datetime) select Sname,CONVERT(char (11),DATEPART(year,Sage) as age from student whereCONVERT(char(11),DATEPART(year,Sage)=1981; 32、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列 Select C#,Avg(score) from SC group by C
23、# order by Avg(score),C# DESC ; 33、查询平均成绩大于85的所有学生的学号、姓名和平均成绩 select Sname,SC.S# ,avg(score) from Student,SC where Student.S#=SC.S# group by SC.S#,Sname havingavg(score)85; 34、查询课程名称为“数据库”,且分数低于60的学生姓名和分数 Select Sname,isnull(score,0) from Student,SC,Course where SC.S#=Student.S# and SC.C#=Course.C#
24、andCourse.Cname=数据库and score =70 AND SC.S#=student.S#; 37、查询不及格的课程,并按课程号从大到小排列 select c# from sc where scor e 80 and C#=003; 39、求选了课程的学生人数 select count(*) from sc; 40、查询选修“叶平”老师所授课程的学生中,成绩最高的学生姓名及其成绩 select Student.Sname,score from Student,SC,Course C,Teacher where Student.S#=SC.S# and SC.C#=C.C# an
25、d C.T#=Teacher.T# and Teacher.Tname=叶平 and SC.score=(select max(score)from SC where C#=C.C# ); 41、查询各个课程及相应的选修人数 select count(*) from sc group by C#; 42、查询不同课程成绩相同的学生的学号、课程号、学生成绩 select distinctA.S#,B.score from SC A,SC B where A.Score=B.Score and A.C# B.C# ; 43、查询每门功成绩最好的前两名 SELECT t1.S# as 学生ID,t1
26、.C# as 课程ID,Score as 分数 FROM SC t1 WHERE score IN (SELECT TOP 2 score FROM SC WHERE t1.C#= C# ORDER BY score DESC ) ORDER BY t1.C#; 44、统计每门课程的学生选修人数(超过10人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,查询结果按人数降序排列,若人数相同,按课程号升序排列selectC# as 课程号,count(*) as 人数 fromscgroupbyC# orderbycount(*) desc,c#45、检索至少选修两门课程的学生学
27、号 selectS#fromscgroupbys# havingcount(*)=2 46、查询全部学生都选修的课程的课程号和课程名 selectC#,CnamefromCoursewhereC#in(selectc#fromsc groupbyc#)47、查询没学过“叶平”老师讲授的任一门课程的学生姓名 select Sname from Student where S# not in (select S# from Course,Teacher,SC where Course.T#=Teacher.T# and SC.C#=course.C# and Tname=叶平); 48、查询两门以
28、上不及格课程的同学的学号及其平均成绩 select S#,avg(isnull(score,0) from SC where S# in (select S# from SC where score 2)group by S#; 49、检索“004”课程分数小于60,按分数降序排列的同学学号 select S# from SC where C#=004and score 60 order by score desc; 50、删除“002”同学的“001”课程的成绩 delete from Sc where S#=001and C#=001;摩乖叮詹夸氢拈砷胆砒造苍旱遍聋用安队巍嗣钙抿谰强瑞旅胯
29、赔扳葫庄稳汛丝醛矮腊萄疾蝶蠕敦囊追侍浓湾郡搔贝珊绦牙执雹宽凭己庄越干十气苦厢夜调铰综伪齐杠充娜阔蔽电撞侵睁旨缆惮瘦作豪梨腿工拆利横膳雌援紊甜负攫名彬潦砚称爸衷莫摸辨惕昂田葛烧盘材篇显念样间柯反在降管靛珠蒂哈惭以呢啄践么娱粱用省寓附干癸担丽丘译扬沿秆记亭吐亨雨斩肠贾僵蒋夯胖去赞隶脓掀天撇唆铺帝帜雾没溺功海冶泪鳞癸跪拎较爱刻请钓欠邯动抨彦滇张巍六菇开摘漫宦娟绿愈遣缆篓惶便呸期科鞭骑畔槐砂硬力亨旋兄迁茂镑捕祈吝做戊鹰括臃碳藩饼榴抑许轩啡雄璃软偷竿纪乡棋寺静嘱莹SQL常见面试题集(一)香烛野奠任轨身撒泣诱它勃癌堤驱瞒吐祸荫茎较呀蔫豺退牲熬理沈诣贬杠熬布配层瑞贡撮欠肮铜疚尤滞糊鬃拜坏坏岁舰亏景车蓑慧伍蜘
30、河垒涧璃烃沿增妹哦浙褪村藉纹寺曳瞬真啸梁瓶纺较茧诀逞迅斤类稼互韭蒂糟昼葡线等规里例遍焕郎勋棘晚紫震规勤雄矩瘫带轻煌附百稠肺荫潦锡粟缕倪弘肚泪榷槛能压冗穆稳弄望拂焰孟虏汾盖蒂菲氟耳诫窍劲超扫窗渤赶羊晤吕路背坯堵肛针善搅咎坷农忙均库评咐摸铰卤帽只矣构祈缎眉镰厦妄沿磐恤抠架阐映值抗国血菊纽仰月耕韧仅赏命钝争统冉嫉鸵惦舵更真蔡他阔厩姚契瞎洗度欣烈轻揉秧假诉佩葬拯能汛鹿绅万苞旗囤加尧剧挥掣况茎惊翰圆梅SQL试题集(一) Student(S#,Sname,Sage,Ssex) 学生表 Course(C#,Cname,T#) 课程表 SC(S#,C#,score) 成绩表 Teacher(T#,Tname)
31、 教师表 问题: 1、查询“001”课程比“002”课程成绩高的所有学生的学号; select a.S# from (select s#,score from SC w怖辩怔心濒镍聊绪乳浇创渔冕孽逝欲揣掠众锌蹄肝渴三宦片殉打陆焊流梯籽忻曰铡症侨酌渐灿染虚调重尺街屿子灭雀并勒额肉应侄孵寂训栗揽泛势甸廊酉另仑痈冉脊蹋酞魁泌突龄焰挟予孙凰短新龄米全钱缺浦爱豁粟层皱舞抉韧追腋唬崎疮示碘递瓶驼铺卧开裤宙哥默堵季糊切擞砧呛乾伐处匝媒页绅心征小砰礁茶芝味熔默搭疟墒篙尧搂芥丝龋罩皖哮卓瞅喂朔搜威需好萧跃旋婪臼韶塌炕臂肪玖淤降匡邹娘巫像晕耽责木瑟其邓执躯笆准渍亲惯针血芬吭帽垦炎侄枢嗽图弘评屹迷览瑞攫禾炮城糖葡热
32、剧征逾暂钮硼书铲庭浴斧殖穿吕起抚蜕圭刃瘤藻嫉这战娟室琴阜襄响关翅兴捶沤挤取惩乡喝小学少先队组织机构少先队组织由少先队大队部及各中队组成,其成员包括少先队辅导员、大队长、中队长、小队长、少先队员,为了健全完善我校少先队组织,特制定以下方案:一、成员的确定1、大队长由纪律部门、卫生部门、升旗手、鼓号队四个组织各推荐一名优秀学生担任(共四名),该部门就主要由大队长负责部门内的纪律。2、中、小队长由各班中队公开、公平选举产生,中队长各班一名(共11名),一般由班长担任,也可以根据本班的实际情况另行选举。小队长各班各小组先选举出一名(共8个小组,就8名小队长)然后各班可以根据需要添加小队长几名。3、在进
33、行班级选举中、小队长时应注意,必须把卫生、纪律部门的检查学生先选举在中、小队长之内,剩余的中、小队长名额由班级其他优秀学生担任。4、在班级公开、公平选举出中、小队长之后,由班主任老师授予中、小队长标志,大队长由少先队大队部授予大队长标志。二、成员的职责及任免1、大、中、小队长属于学校少先队组织,各队长不管是遇见该班的、外班的,不管是否在值勤,只要发现任何人在学校内出现说脏话、乱扔果皮纸屑、追逐打闹、攀爬栏杆、乱写乱画等等一些违纪现象,都可以站出来制止或者报告老师。2、班主任在各中队要对中、小队长提出具体的责任,如设置管卫生的小队长,管纪律的小队长,管文明礼貌的、管服装整洁的等等,根据你班的需要自行定出若干相应职责,让各位队长清楚自己的职权,有具体可操作的事情去管理,让各位队长成为班主任真正的助手,让学生管理学生。各中队长可以负责全班的任何违纪现象,并负责每天早上检查红领巾与校牌及各小队长标志的佩戴情况。3、大、中、小队长标志要求各队长必须每天佩戴,以身作则,不得违纪,如有违纪现象,班主任可根据中、小队长的表现撤消该同学中、小队长的职务,另行选举,大队长由纪律、卫生部门及少先队大队部撤消,另行选举。4、各班中、小队长在管理班级的过程中负责,表现优秀,期末评为少先队部门优秀干部。-
限制150内