Sql常见面试题(总结)1.用一条SQL语句查询出每门课都大于80分的学生姓名namekechengfenshu张三语文81张三数学75李四语文76李四数学90王五语文81王五数学100王五英语90A:selectdistinctnamefromtablewherenamenotin(selectdistinctnamefromtablewherefenshu=80)2.学生表如下:自动编号学号姓名课程编号课程名称分数12005001张三0001数学6922005002李四0001数学8932005001张三0001数学69删除除了自动编号不同,其他都相同的学生冗余信息A:deletefromtablenamewhere自动编号notin(selectmin(自动编号)fromtablenamegroupby学号,姓名,课程编号,课程名称,分数)一个叫department的表,里面只有一个字段name,一共有4条纪录,分别是a,b,c,d,对应四个球对,现在四个球对进行比赛,用一条sql语句显示所有可能的比赛组合.你先按你自己的想法做一下,看结果有我的这个简单吗?答:selecta.name,b.namefromteama,teambwherea.nameb.name请用SQL语句实现:从TestDB数据表中查询出所有月份的发生额都比101科目相应月份的发生额高的科目。请注意:TestDB中有很多科目,都有1-12月份的发生额。AccID:科目代码,Occmonth:发生额月份,DebitOccur:发生额。数据库名:JcyAudit,数据集:Select*fromTestDB答:selecta.*fromTestDBa,(selectOccmonth,max(DebitOccur)Debit101ccurfromTestDBwhereAccID='101'groupbyOccmonth)bwherea.Occmonth=b.Occmonthanda.DebitOccurb.Debit101ccur************************************************************************************面试题:怎么把这样一个表儿yearmonthamount199111.1199121.2199131.3199141.4199212.1199222.2199232.3199242.4查成这样一个结果yearm1m2m3m419911.11.21.31.419922.12.22.32.4答案一、selectyear,(selectamountfromaaamwheremonth=1andm.year=aaa.year)asm1,(selectamountfromaaamwheremonth=2andm.year=aaa.year)asm2,(selectamountfromaaamwheremonth=3andm.year=aaa.year)asm3,(selectamountfromaaamwheremonth=4andm.year=aaa.year)asm4fromaaagroupbyyear这个是ORACLE中做的:select*from(selectname,yearb1,lead(year)over(partitionbynameorderbyyear)b2,lead(m,2)over(partitionbynameorderbyyear)b3,rank()over(partitionbynameorderbyyear)rkfromt)whererk=1;************************************************************************************精妙的SQL语句!精妙SQL语句作者:不详发文时间:2003.05.2910:55:05说明:复制表(只复制结构,源表名:a新表名:b)SQL:select*intobfromawhere11说明:拷贝表(拷贝数据,源表名:a目标表名:b)SQL:insertintob(a,b,c)selectd,e,ffromb;说明:显示文章、提交人和最后回复时间SQL:selecta.title,a.username,b.adddatefromtablea,(selectmax(adddate)adddatefromtablewheretable.title=a.title)b说明:外连接查询(表名1:a表名2:b)SQL:selecta.a,a.b,a.c,b.c,b.d,b.ffromaLEFTOUTJOINbONa.a=b.c说明:日程安排提前五分钟提醒SQL:select*from日程安排wheredatediff('minute',f开始时间,getdate())5说明:两张关联表,删除主表中已经在副表中没有的信息SQL:deletefrominfowherenotexists(select*frominfobzwhereinfo.infid=infobz.infid)说明:--SQL:SELECTA.NUM,A.NAME,B.UPD_DATE,B.PREV_UPD_DATEFROMTABLE1,(SELECTX.NUM,X.UPD_DATE,Y.UPD_DATEPREV_UPD_DATEFROM(SELECTNUM,UPD_DATE,INBOUND_QTY,STOCK_ONHANDFROMTABLE2WHERETO_CHAR(UPD_DATE,'YYYY/MM')=TO_CHAR(SYSDATE,'YYYY/MM'))X,(SELECTNUM,UPD_DATE,STOCK_ONHANDFROMTABLE2WHERETO_CHAR(UPD_DATE,'YYYY/MM')=TO_CHAR(TO_DATE(TO_CHAR(SYSDATE,'YYYY/MM')¦¦'/01','YYYY/MM/DD')-1,'YYYY/MM'))Y,WHEREX.NUM=Y.NUM(+)ANDX.INBOUND_QTY+NVL(Y.STOCK_ONHAND,0)X.STOCK_ONHAND)BWHEREA.NUM=B.NUM说明:--SQL:select*fromstudentinfowherenotexists(select*fromstudentwherestudentinfo.id=student.id)and系名称='&strdepartmentname&'and专业名称='&strprofessionname&'orderby性别,生源地,高考总成绩说明:从数据库中去一年的各单位电话费统计(电话费定额贺电化肥清单两个表来源)SQL:SELECTa.userper,a.tel,a.standfee,TO_CHAR(a.telfeedate,'yyyy')AStelyear,SUM(decode(TO_CHAR(a.telfeedate,'mm'),'01',a.factration))ASJAN,SUM(decode(TO_CHAR(a.telfeedate,'mm'),'02',a.factration))ASFRI,SUM(decode(TO_CHAR(a.telfeedate,'mm'),'03',a.factration))ASMAR,SUM(decode(TO_CHAR(a.telfeedate,'mm'),'04',a.factration))ASAPR,SUM(decode(TO_CHAR(a.telfeedate,'mm'),'05',a.factration))ASMAY,SUM(decode(TO_CHAR(a.telfeedate,'mm'),'06',a.factration))ASJUE,SUM(decode(TO_CHAR(a.telfeedate,'mm'),'07',a.factration))ASJUL,SUM(decode(TO_CHAR(a.telfeedate,'mm'),'08',a.factration))ASAGU,SUM(decode(TO_CHAR(a.telfeedate,'mm'),'09',a.factration))ASSEP,SUM(decode(TO_CHAR(a.telfeedate,'mm'),'10',a.factration))ASOCT,SUM(decode(TO_CHAR(a.telfeedate,'mm'),'11',a.factration))ASNOV,SUM(decode(TO_CHAR(a.telfeedate,'mm'),'12',a.factration))ASDECFROM(SELECTa.userper,a.tel,a.standfee,b.telfeedate,b.factrationFROMTELFEESTANDa,TELFEEbWHEREa.tel=b.telfax)aGROUPBYa.userper,a.tel,a.standfee,TO_CHAR(a.telfeedate,'yyyy')说明:四表联查问题:SQL:select*fromaleftinnerjoinbona.a=b.brightinnerjoincona.a=c.cinnerjoindona.a=d.dwhere.....说明:得到表中最小的未使用的ID号SQL:SELECT(CASEWHENEXISTS(SELECT*FROMHandlebWHEREb.HandleID=1)THENMIN(HandleID)+1ELSE1END)asHandleIDFROMHandleWHERENOTHandleIDIN(SELECTa.HandleID-1FROMHandlea)*******************************************************************************有两个表A和B,均有key和value两个字段,如果B的key在A中也有,就把B的value换为A中对应的value这道题的SQL语句怎么写?updatebsetb.value=(selecta.valuefromawherea.key=b.key)whereb.idin(selectb.idfromb,awhereb.key=a.key);***************************************************************************高级sql面试题原表:courseidcoursenamescore-------------------------------------1java702oracle903xml404jsp305servlet80-------------------------------------为了便于阅读,查询此表后的结果显式如下(及格分数为60):courseidcoursenamescoremark---------------------------------------------------1java70pass2oracle90pass3xml40fail4jsp30fail5servlet80pass---------------------------------------------------写出此查询语句没有装ORACLE,没试过selectcourseid,coursename,score,decode(sign(score-60),-1,'fail','pass')asmarkfromcourse完全正确SQLdesccourse_vNameNull?Type-----------------------------------