logo资料库

sql面试题(适合软件测试).doc

第1页 / 共16页
第2页 / 共16页
第3页 / 共16页
第4页 / 共16页
第5页 / 共16页
第6页 / 共16页
第7页 / 共16页
第8页 / 共16页
资料共16页,剩余部分请下载后查看
Sql 常见面试题(总结) 1.用一条 SQL 语句 查询出每门课都大于 80 分的学生姓名 name kecheng fenshu 张三 张三 李四 李四 王五 王五 王五 语文 数学 语文 数学 语文 数学 英语 81 75 76 90 81 100 90 A: select distinct name from table where name not in (select distinct name from table where fenshu<=80) 2.学生表 如下: 自动编号 学号 姓名 课程编号 课程名称 分数 1 2 3 2005001 张三 0001 2005002 李四 0001 2005001 张三 0001 数学 数学 数学 69 89 69 删除除了自动编号不同,其他都相同的学生冗余信息 A: delete tablename where 自动编号 not in(select min(自动编号) from tablename g roup by 学号,姓名,课程编号,课程名称,分数) 一个叫 department 的表,里面只有一个字段 name,一共有 4 条纪录,分别是 a,b,c,d,对应 四个球对,现在四个球对进行比赛,用一条 sql 语句显示所有可能的比赛组合. 你先按你自己的想法做一下,看结果有我的这个简单吗? 答:select a.name, b.name from team a, team b where a.name < b.name 请用 SQL 语句实现:从 TestDB 数据表中查询出所有月份的发生额都比 101 科目相应月份的
month amount 发生额高的科目。请注意: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) b where a.Occmonth=b.Occmonth and a.DebitOccur>b.Debit101ccur ******************************************************************************* ***** 面试题:怎么把这样一个表儿 year 1991 1991 1991 1991 1992 1992 1992 1992 查成这样一个结果 year m1 1991 1.1 1.2 1.3 1.4 1992 2.1 2.2 2.3 2.4 1.1 1.2 1.3 1.4 2.1 2.2 2.3 2.4 1 2 3 4 1 2 3 4 m2 m3 m4 答案一、 select year, (select amount from (select amount from (select amount from (select amount from from aaa group by year aaa m where month=1 aaa m where month=2 aaa m where month=3 aaa m where month=4 and m.year=aaa.year) as m1, and m.year=aaa.year) as m2, and m.year=aaa.year) as m3, and m.year=aaa.year) as m4 中做的: 这个是 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,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('minute',f 开始时间,getdate())>5 说明:两张关联表,删除主表中已经在副表中没有的信息 SQL: from delete info.infid=infobz.infid ) info where not exists ( select * from infobz where 说明:-- 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, INBOUND_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') ) Y, WHERE X.NUM = Y.NUM (+) 'YYYY/MM') ¦¦ '/01','YYYY/MM/DD') - 1, AND X.INBOUND_QTY + 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 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'), '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 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 where ..... 说明:得到表中最小的未使用的 ID 号 SQL: SELECT (CASE WHEN EXISTS(SELECT * FROM Handle b WHERE b.HandleID = 1) THEN MIN(HandleID) + 1 ELSE 1 END) as HandleID FROM Handle WHERE NOT HandleID IN (SELECT a.HandleID - 1 FROM Handle a) ******************************************************************************* 有两个表 A 和 B,均有 key 和 value 两个字段,如果 B 的 key 在 A 中也有,就把 B 的 value 换为 A 中对应的 value 这道题的 SQL 语句怎么写? update =b.key) a.key); *************************************************************************** 高级 sql 面试题 b.value=(select in(select a.value b.id where where a.key b.key= where b.id b set from a b,a from
原表: courseid coursename score ------------------------------------- 1 java 70 2 oracle 90 3 xml 40 4 jsp 30 5 servlet 80 ------------------------------------- 为了便于阅读,查询此表后的结果显式如下(及格分数为 60): courseid coursename score mark --------------------------------------------------- 1 java 70 pass 2 oracle 90 pass 3 xml 40 fail 4 jsp 30 fail 5 servlet 80 pass --------------------------------------------------- 写出此查询语句 没有装ORACLE,没试过 select courseid, coursename ,score ,decode(sign(score-60),-1,'fail','pass') as mark from course 完全正确 SQL> desc course_v Name Null? Type ----------------------------------------- -------- ---------------------------- COURSEID NUMBER COURSENAME VARCHAR2(10) SCORE NUMBER SQL> select * from course_v; COURSEID COURSENAME SCORE ---------- ---------- ---------- 1 java 70 2 oracle 90 3 xml 40 4 jsp 30 5 servlet 80 SQL> select courseid, coursename ,score ,decode(sign(score-60),-1,'fail','pass') as mark from course_v;
COURSEID COURSENAME SCORE MARK ---------- ---------- ---------- ---- 1 java 70 pass 2 oracle 90 pass 3 xml 40 fail 4 jsp 30 fail 5 servlet 80 pass ******************************************************************************* 原表: id proid proname 1 1 M 1 2 F 2 1 N 2 2 G 3 1 B 3 2 A 查询后的表: id pro1 pro2 1 M F 2 N G 3 B A 写出查询语句 解决方案 sql 求解 表 a 列 a1 a2 记录 1 a 1 b 2 x 2 y 2 z 用 select 能选成以下结果吗? 1 ab 2 xyz 使用 pl/sql 代码实现,但要求你组合后的长度不能超出 oracle varchar2 长度的限制。 下面是一个例子 create or replace type strings_table is table of varchar2(20); / create or replace function merge (pv in strings_table) return varchar2 is ls varchar2(4000);
begin for i in 1..pv.count loop ls := ls || pv(i); end loop; return ls; end; / create table t (id number,name varchar2(10)); insert into t values(1,'Joan'); insert into t values(1,'Jack'); insert into t values(1,'Tom'); insert into t values(2,'Rose'); insert into t values(2,'Jenny'); column names format a80; select t0.id,merge(cast(multiset(select name from t where t.id = t0.id) as strings_table)) names from (select distinct id from t) t0; drop type strings_table; drop function merge; drop table t; 用 sql: Well if you have a thoretical maximum, which I would assume you would given the legibility of listing hundreds of employees in the way you describe then yes. But the SQL needs to use the LAG function for each employee, hence a hundred emps a hundred LAGs, so kind of bulky. This example uses a max of 6, and would need more cut n pasting to do more than that. SQL> select deptno, dname, emps 2 from ( 3 select d.deptno, d.dname, rtrim(e.ename ||', '|| 4 lead(e.ename,1) over (partition by d.deptno 5 order by e.ename) ||', '|| 6 lead(e.ename,2) over (partition by d.deptno 7 order by e.ename) ||', '|| 8 lead(e.ename,3) over (partition by d.deptno 9 order by e.ename) ||', '||
分享到:
收藏