(资料来源:互联网)Sql常见面试题(总结)1.用一条SQL语句查询出每门课都大于80分的学生姓名name kecheng fenshu张三语文 81张三数学 75李四语文 76李四数学 90王五语文 81王五数学 100王五英语 90A: select distinct name from table where name not in (select distinct name fr om table where fenshu<=80)2.学生表如下:自动编号学号姓名课程编号课程名称分数1 2005001 张三 0001 数学 692 2005002 李四 0001 数学 893 2005001 张三 0001 数学 69删除除了自动编号不同,其他都相同的学生冗余信息A: delete tablename where 自动编号 not in(select min(自动编号) from tablename g roup by 学号,姓名,课程编号,课程名称,分数)一个叫department的表,里面只有一个字段name,一共有4条纪录,分别是a,b,c,d,对应四个球对,现在四个球对进行比赛,用一条sql语句显示所有可能的比赛组合.你先按你自己的想法做一下,看结果有我的这个简单吗?答:select , from team a, team bwhere < 请用SQL语句实现:从TestDB数据表中查询出所有月份的发生额都比101科目相应月份的发生额高的科目。
请注意:TestDB中有很多科目,都有1-12月份的发生额。
AccID:科目代码,Occmonth:发生额月份,DebitOccur:发生额。
数据库名:JcyAudit,数据集:Select * from TestDB答:select a.*from TestDB a,(select Occmonth,max(DebitOccur) Debit101ccur from TestDB where AccID='101' group by Occmonth) bwhere a.Occmonth=b.Occmonth and a.DebitOccur>b.Debit101ccur******************************************************************************* *****面试题:怎么把这样一个表儿year month amount1991 1 1.11991 2 1.21991 3 1.31991 4 1.41992 1 2.11992 2 2.21992 3 2.31992 4 2.4查成这样一个结果year m1 m2 m3 m41991 1.1 1.2 1.3 1.41992 2.1 2.2 2.3 2.4答案一、select year,(select amount from aaa m where month=1 and m.year=aaa.year) as m1,(select amount from aaa m where month=2 and m.year=aaa.year) as m2,(select amount from aaa m where month=3 and m.year=aaa.year) as m3,(select amount from aaa m where month=4 and m.year=aaa.year) as m4from aaa group by year这个是ORACLE 中做的:select * from (select name, year b1, lead(year) over(partition by name order by year) b2, lead(m,2) over(partition by name order by year) b3,rank()over(partition by name order by year) rk from t) where rk=1;******************************************************************************* *****精妙的SQL语句!精妙SQL语句作者:不详发文时间:2003.05.29 10:55:05说明:复制表(只复制结构,源表名:a 新表名:b)SQL: select * into b from a where 1<>1说明:拷贝表(拷贝数据,源表名:a 目标表名:b)SQL: insert into b(a, b, c) select d,e,f from b;说明:显示文章、提交人和最后回复时间SQL: select a.title,ername,b.adddate from table a,(select max(adddate) adddate from table where table.title=a.title) b说明:外连接查询(表名1:a 表名2:b)SQL: select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c说明:日程安排提前五分钟提醒SQL: select * from 日程安排 where datediff('minute',f开始时间,getdate())>5说明:两张关联表,删除主表中已经在副表中没有的信息SQL:delete from info where not exists ( select * from infobz where info.infid=infobz.infid )说明:--SQL:SELECT A.NUM, , B.UPD_DATE, B.PREV_UPD_DATEFROM TABLE1,(SELECT X.NUM, X.UPD_DATE, Y.UPD_DATE PREV_UPD_DATEFROM (SELECT NUM, UPD_DATE, INBOUND_QTY, STOCK_ONHANDFROM TABLE2WHERE TO_CHAR(UPD_DATE,'YYYY/MM') = TO_CHAR(SYSDATE, 'YYYY/MM')) X,(SELECT NUM, UPD_DATE, STOCK_ONHANDFROM TABLE2WHERE TO_CHAR(UPD_DATE,'YYYY/MM') =TO_CHAR(TO_DATE(TO_CHAR(SYSDATE, 'YYYY/MM') ¦¦ '/01','YYYY/MM/DD') - 1, 'YYYY/MM') ) Y,WHERE X.NUM = Y.NUM (+)AND X.INBOUND_QTY + NVL(Y.STOCK_ONHAND,0) <> X.STOCK_ONHAND ) BWHERE A.NUM = B.NUM说明:--SQL:select * from studentinfo where not exists(select * from student where studentinfo.id=student.id) and 系名称='"&strdepartmentname&"' and 专业名称='"&strprofessionname&"' order by 性别,生源地,高考总成绩说明:从数据库中去一年的各单位电话费统计(电话费定额贺电化肥清单两个表来源)SQL:SELECT erper, a.tel, a.standfee, TO_CHAR(a.telfeedate, 'yyyy') AS telyear,SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '01', a.factration)) AS JAN,SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '02', a.factration)) AS FRI,SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '03', a.factration)) AS MAR,SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '04', a.factration)) AS APR,SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '05', a.factration)) AS MAY,SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '06', a.factration)) AS JUE,SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '07', a.factration)) AS JUL,SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '08', a.factration)) AS AGU,SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '09', a.factration)) AS SEP,SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '10', a.factration)) AS OCT,SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '11', a.factration)) AS NOV,SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '12', a.factration)) AS DECFROM (SELECT erper, a.tel, a.standfee, b.telfeedate, b.factrationFROM TELFEESTAND a, TELFEE bWHERE a.tel = b.telfax) aGROUP BY erper, a.tel, a.standfee, TO_CHAR(a.telfeedate, 'yyyy')说明:四表联查问题:SQL: select * from a left inner join b on a.a=b.b right inner join c on a.a=c.c inner join d on a.a=d.d where .....说明:得到表中最小的未使用的ID号SQL:SELECT (CASE WHEN EXISTS(SELECT * FROM Handle b WHERE b.HandleID = 1) THEN MIN(HandleID) + 1 ELSE 1 END) as HandleIDFROM HandleWHERE NOT HandleID IN (SELECT a.HandleID - 1 FROM Handle a)******************************************************************************* 有两个表A和B,均有key和value两个字段,如果B的key在A中也有,就把B的value 换为A中对应的value这道题的SQL语句怎么写?update b set b.value=(select a.value from a where a.key=b.key) where b.id in(select b.id from b,a where b.key=a.key);***************************************************************************高级sql面试题原表:courseid coursename score-------------------------------------1 java 702 oracle 903 xml 404 jsp 305 servlet 80-------------------------------------为了便于阅读,查询此表后的结果显式如下(及格分数为60):courseid coursename score mark---------------------------------------------------1 java 70 pass2 oracle 90 pass3 xml 40 fail4 jsp 30 fail5 servlet 80 pass---------------------------------------------------写出此查询语句没有装ORACLE,没试过select courseid, coursename ,score ,decode(sign(score-60),-1,'fail','pass') as mark from course完全正确SQL> desc course_vName Null? Type----------------------------------------- -------- ---------------------------- COURSEID NUMBERCOURSENAME VARCHAR2(10)SCORE NUMBERSQL> select * from course_v;COURSEID COURSENAME SCORE---------- ---------- ----------1 java 702 oracle 903 xml 404 jsp 305 servlet 80SQL> select courseid, coursename ,score ,decode(sign(score-60),-1,'fail','pass') as mark from course_v;COURSEID COURSENAME SCORE MARK---------- ---------- ---------- ----1 java 70 pass2 oracle 90 pass3 xml 40 fail4 jsp 30 fail5 servlet 80 pass******************************************************************************* 原表:id proid proname1 1 M1 2 F2 1 N2 2 G3 1 B3 2 A查询后的表:id pro1 pro21 M F2 N G3 B A写出查询语句解决方案sql求解表a列 a1 a2记录 1 a1 b2 x2 y2 z用select能选成以下结果吗?1 ab2 xyz使用pl/sql代码实现,但要求你组合后的长度不能超出oracle varchar2长度的限制。