面试题:数据条件筛选时有几种方式?有什么区别?
限定组的结果:having子句 having子句用来对分组后的结果进行条件过滤。 having和where的区别; where和having都是用于条件过滤。 where在分组前进行条件过滤,having子句是在分组后进行条件过滤, where子句中不能使用聚合函数,having子句可以使用聚合函数。 select deptno,count(*),sum(sal),avg(sal),max(sal),min(sal) from scott.emp group by deptno having sum(sal)>9000 order by sum(sal) desc;
(9)子查询 子查询就是把多条语句写成一条,子查询执行的时候先执行子查询 再执行主查询: select * from scott.emp where sal>( select sal from scott.emp where ename=allen ); 子查询需要注意的问题:单行子查询返回多行 多行比较运算符: In:与列表中的一个值相等(包含) any:与子查询返回的每一个值比较 all:与子查询返回的所有值比较 in: select * from scott.emp where sal in( select min(sal) from scott.emp group by deptno ); any与子查询返回的每一个值比较 >any 大于最小的 <any 小于最大的 select * from scott.emp where sal>any( select min(sal) from scott.emp group by deptno ); all与子查询返回的所有值进行比较 >all 大于最大的 <all 小于最小的 select * from scott.emp where sal>all( select min(sal) from scott.emp group by deptno ); (10)集合运算:从多个结果集中提取数据 union 把两个结果集的数据合起来,然后干掉重复的 select deptno from scott.dept union select deptno from scott.emp; union all:把两个结果集的数据合起来 select deptno from scott.dept union all select deptno from scott.emp; intersect:把两个结果集的数据合起来,然后干掉重复的, 再找两个查询中都出现的记录 select deptno from scott.dept intersert slect deptno from scott.emp; minus:判断数据存在第一查询的结果集,而·不存在第二查询的数据集 select deptno from scott.dept minus slect deptno from scott.emp; 面试题: select r.id,r.R1,nvl(b.R2,null) from r,b where r.id=b.id(+) union selet b.id,nvl(r.R1,null),b.R2 from r,b where r.id(+)=b.id; 标准外连接: select r.id,r.R1,b.R2 from r left outer join b on r.id=b.id union select b.id,r.R1,b.R2 from r right outer join b on b.id=r.id; 全外连接: select nvl(r.id,b.id) as id,r.R1,b.R2 from r full outer join b on r.id=b.id; (11)rownum 伪列 ‘结果集’中产生的序列 select rownum,deptno,dname,loc from scott.dept; 只有存在rownum=1的记录,才可能存在rownum=2的记录 利用rownum分页: select * from( select rownum as tempid,empno,ename from scott.emp )t1 where t1.tempid between 6 and 10; rowid:一般来说每一行数据对应一个rowid,而且固定且唯一。 在这一行存入时就确定了。可以理解为java对象中的内存地址。 可以利用rowid来查询记录,而且通过rowid查询速度最快的查询方法。 rowid只有在表发生移动(比如表空间变化,数据导入/导出后)才会发生变化。 面试题: 删除重复数据(删除所有重复数据 /删除重复数据但保留一条(保留最大rowid或者最小)): 删除所有数据; delete from tb_test where name in( select name from tb_test group by name,age having count(*)>1 ); 删除数据,保留一条数据(第一种方法); create table tb_tmp as select distinct name,age from tb_test; truncate table tb_test; insert into tb_test(name,age) select name,age from tb_tmp; 删除数据,保留一条数据(方法二): delete from tb_test where rowid not in( select max(rowid) from tb_test group by name,age ); (12)常用函数 dual是oracle提供的一个虚表 select length(hello) from dual; 常用函数: lower把大写转成小写 upper把小写转大写: select upper(helloworld) from dual; select lower(HELLOWORLD) from dual; select * from scott.emp where ename=smith; select * from scott.emp where lower(ename)=smith; initcap使串中的所有单词首字母变成大写: select initcap(sql course) from dual; concat 连接两个字符串: select concat(Hello,World) from dual; substr 取字符串,从start开始,取count个: select substr(HelloWorld,1,5) from dual; 从4开始取到末尾: select substr(Helloworld,4) from dual; length 返回字符串的长度: select length(HelloWorld) from dual; instr 在一个字符串中搜索指定的字符,返回发现指定的字符的位置,从1开始: select instr(HelloWorld,l) from dual; trim 删除首尾的空字符串 select trim( HelloWorld ) from dual; replace 替换: select replace(HelloWorld,ll,ff) from dual; round 四舍五入; select round(45.926,2) from dual; trunc 截断: select trunc(45.926,2) from dual; mod 取模: select mod(1600,300) from dual; decode函数: select ename,job,sal 基本工资,decode(job, salesman, sal*0.9, manager,sal*0.85, clerk,sal+100, pre sident,sal )as 实发工资 from scott.emp;
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-73999-4.html
就像炒大蒜
时间不定