1、SQL面试题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 from table where fenshu=80)2.学生表 如下:自动编号 学号 姓名 课程编号 课程名称 分数1 2005001 张三 0001 数学 692 2005002 李四 0001 数
2、学 893 2005001 张三 0001 数学 69删除除了自动编号不同,其他都相同的学生冗余信息A: delete tablename where 自动编号 not in(select min(自动编号) from tablename group by 学号,姓名,课程编号,课程名称,分数)一个叫department的表,里面只有一个字段name,一共有4条纪录,分别是a,b,c,d,对应四个球对,现在四个球对进行比赛,用一条sql语句显示所有可能的比赛组合.你先按你自己的想法做一下,看结果有我的这个简单吗?答:select a.name, b.name from team a, team
3、 b where a.name b.Debit101ccur*面试题:怎么把这样一个表儿yearmonth 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 m1m2m3m41991 1.1 1.2 1.3 1.41992 2.1 2.2 2.3 2.4 答案一、select year, (select amount fromaaa m where month=1and m.year=aaa.year) as m1,(select amount f
4、romaaa m where month=2and m.year=aaa.year) as m2,(select amount fromaaa m where month=3and m.year=aaa.year) as m3,(select amount fromaaa m where month=4and m.year=aaa.year) as m4from aaagroup by year这个是ORACLE中做的:select * from (select name, year b1, lead(year) over(partition by name order by year) b2
5、, 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 11 说明:拷贝表(拷贝数据,源表名:a 目标表名:b) SQL: insert into b(a, b, c) select d,e,f fro
6、m b; 说明:显示文章、提交人和最后回复时间 SQL: select a.title,a.username,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(
7、minute,f开始时间,getdate()5 说明:两张关联表,删除主表中已经在副表中没有的信息 SQL: delete from info where not exists ( select * from infobz where info.infid=infobz.infid ) 说明:- SQL: SELECT A.NUM, A.NAME, B.UPD_DATE, B.PREV_UPD_DATE FROM TABLE1, (SELECT X.NUM, X.UPD_DATE, Y.UPD_DATE PREV_UPD_DATE FROM (SELECT NUM, UPD_DATE, INB
8、OUND_QTY, STOCK_ONHAND FROM TABLE2 WHERE TO_CHAR(UPD_DATE,YYYY/MM) = TO_CHAR(SYSDATE, YYYY/MM) X, (SELECT NUM, UPD_DATE, STOCK_ONHAND FROM TABLE2 WHERE 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 +
9、 NVL(Y.STOCK_ONHAND,0) X.STOCK_ONHAND ) B WHERE 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
10、 a.userper, 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),
11、 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_CHA
12、R(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 DEC FROM (SELECT a.userper, a.tel, a.standfee, b.telfeedate, b.factration FROM TELFEESTAND a, TELFEE b WHERE a.tel = b.telfax) a GROUP BY a.userper, 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