数据库:Oracle数据库select面试笔…

  1. number(10):存10位数字,即1234567890
    number(10,3):123456.789
    varchar2(20):最大存放20个字节的字符,其中每个字符占2个字节,如存入字符串‘abc’,则实际只占用6个字节空间。
  2. null不能用==或者!=比较  ,必须用 is null 或者is not null
  3. rownum  返回当前号                                                                                                                                                                                                                   
  4. rowid是一个伪列,一般情况用不到,但是当需要具体锁定某一行时,它非常有用,因为它的值绝对不会重复,rowid 类似于hashcode  定位列在内存中的数据。

经典的删除表中重复记录

4、这里不能写成select *, rowid from emp2而必须写成  select emp2.*, rowid from emp2)  ,因为有后面字段的存在

  1. between and >=  <=,  效率低,用的少,一般是直接>= <=
  2. exists、 not exists   表内数据过多情况下用来替代 in 、 not  in,exists的执行效率高。exists是通过索引(key-value)来检索数据库的,而in是遍历数据库的。所以如果想增加对表中某个字段的查询速度,就可以为他们建立索引,但是,索引的建立也有代价,索引是要占空间的,因此,除非必要,否则不要轻易添加索引(如非必要,不要轻易添加视图)
  3. 尽量不用all ,用max来替代他
  4. where and     where or 都是先执行B,后执行A
  5. n个表连接时,至少需要n-1个过滤条件
  6. 注释为 或者--
  7. 只要起了别名,接下来的操作就必须使用别名,别名不是永久更改
  8. 别名要么不加引号,要么加“”,  但是不能加‘’,   而在判断字符串的时候要加‘’,不加引号的会自动全部转换为大写,而“”以及字符串‘’中的内容是区分大小写的。
  9. sum(~)时自动将为null的转换为0求和,avg(~)时跳过null字段,不计入个数中
  10. order by e.sal, e.deptno desc;(指先按e.sal升序,在按deptno降序)                                                     order by e.sal desc, e.deptno desc;(才是两个都按降序)
  11. Savepoint的运用:

          delete from emp_copy where e,deptno 20;

          savepoint a;

          select from emp_copy;

          rollback to a;

          select from emp_copy;

    12、high water mark故名思义为高水平线,一般是相对一个表而言的,当一个表有数据不断的插入时,high water mark值不断增高,对那些全表扫描的select查询是以high water mark为终点的,虽然表中可能只有一行记录.它是表的空间曾经扩充到的值.

truncate将重置高水平线,而delete还是原来的值

 

 0、oracle中分页处理(必须掌握):

oracle中的rownum只能使用<、<=,不能使用=、>、>=等比较操作符。譬如: select * from emp where rownum > 5;而且当rownum和order by一起使用时,会首先选出符合rownum条件的记录,然后再排序。这会给查询带来难度,得需要用到子查询。

rownum的另一个主要用途是用来分页,例如想求按薪水从高到低排列的第6个到第10个人的信息,由于不能使用>、>=,必须使用多层嵌套的子查询:

select e2.*

    from  (select e1.*, rownum r

                   from  (select e.* from emp e order by sal desc) e1

                  where rownum <= 10 ) e2

 where r >= 5

1、查询EMP表显示在198121日到198151日之间雇佣的雇员名、岗位及雇佣日期,并以雇佣日期进行排序。  注意日期转换的知识点

select ename, job, hiredate

 from emp

where hiredate to_date('1981/2/1', 'YYYY/MM/DD')

 and hiredate to_date('1981/5/1', 'YYYY/MM/DD')

order by hiredate;

2、列出至少有一个雇员的所有部门。   

    select d.deptno, d.dname, count(e.empno)

     from emp e, dept d

    where e.deptno(+) d.deptno

     group by d.deptno, d.dname

    having count(e.empno) >= 1

3、列出与“SCOTT”从事相同工作的所有雇员。(注意关于exist的东西,因为in的执行效率比较低,虽然写起来简单

select  from emp where e.job in (select distinct p.job from emp where p.ename='SCOTT');

select  from emp where exists (select from emp   where p.job e.job and p.ename='SCOTT');

4、列出薪金等于在部门30工作的所有雇员的薪金的雇员的姓名和薪金。  注意考虑e.deptno is null这种情况及exist的写法

select  from emp where e.sal in (select p.sal from emp where p.deptno 30) and (e.deptno <> 30 or e.deptno is null);

select from emp where exists (select from emp where p.sal e.sal and p.deptno=30 and (e.deptno<>30 or e.deptno is null) );

5、列出在每个部门工作的雇员的数量以及其他信息。

select  d.deptno,d.loc,d.dname ,count(e.empno) from emp ,dept where e.deptno (+)= d.deptno group by d.deptno,d.loc,d.dname;

select  e.*,d.deptno,count(e.empno) over(partition by d.deptno) from emp ,dept where e.deptno (+)= d.deptno 

6、列出分配有雇员数量的所有部门的详细信息即使是分配有0个雇员。

    select  d.deptno,d.loc,d.dname ,count(e.empno) from emp ,dept where e.deptno (+)= d.deptno group by d.deptno,d.loc,d.dname;

7、找出各月倒数第3天受雇的所有员工.

select e.* from emp where last_day(e.hiredate)= e.hiredate+2;

8、找出早于12年前受雇的员工.

    select *

      from emp

     where hiredate trunc(sysdate)+(INTERVAL '-12' YEAR);

9、以首字母大写的方式显示所有员工的姓名.

     select  initcap(ename) from emp;

     select Upper(substr(ename,1,1))||Lower(substr(ename,2)) from emp

10、显示正好为5个字符的员工的姓名                                           

     select ename from emp where length(ename) 5;

11、显示所有员工姓名的前三个字符.

     select substr(ename,1,3) from emp;

12、显示所有员工的姓名,a替换所有"A"

     select replace(e.ename,'A','a') from emp e;

     select translate(ename,'A','a') from emp;

13、显示所有员工的姓名、工作和薪金,按工作的降序排序,若工作相同则按薪金排序.

     select ename, job, sal from emp order by job desc, sal;

14、显示所有员工的姓名、加入公司的年份和月份,按受雇日期所在月排序,若月份相同则将最早年份的员工排在最前面.

     select ename,

            extract(YEAR from hiredate) ,

            extract(MONTH from hiredate) 

       from emp

      order by ;

      

      select e.ename,

             to_char(e.hiredate, 'yyyy') ,

             to_char(e.hiredate, 'mm') 

        from emp e

       order by ;

15、找出在(任何年份的)2月受聘的所有员工。

    select from emp where extract(MONTH from hiredate) 2;

    select from emp where to_char(hiredate,'mm') '02';

16、对于每个员工,显示其加入公司的天数 (注意如果select后面的字段中除了emp表中原有字段还有其他字段,那么需要将emp重命名)

    select e.*,ceil(sysdate-e.hiredate) "入职天数 from emp ;

    select ename, trunc(sysdate-hiredate) from emp; 

如果我们需要将两个select语句的结果作为一个整体显示出来,我们就需要用到union或者union all关键字。union(或称为联合)的作用是将多个结果合并在一起显示出来。

unionunion all的区别是,union会自动压缩多个结果集合中的重复结果,而union all则将所有的结果全部显示出来,不管是不是重复。  

Union:对两个结果集进行并集操作,不包括重复行,同时进行默认规则的排序;

Union All:对两个结果集进行并集操作,包括重复行,不进行排序;

Select1 union select2 取两个select 去除重复的

Select1 union all select  不去除重复的

Select1 intersect select2  取交集

Select1 minus select2 取差集

17用一个查询语句,实现查询各个岗位的总工资和各个部门的总工资和所有雇员的总工资   

 (方法1) --  union all !!

  select e.job||'岗位总工资"总工资",sum(e.sal) from emp group by e.job

  union all

  select e.deptno||'部门总工资',sum(e.sal) from emp group by e.deptno

  union all

  select '雇员总工资',sum(e.sal) from emp e;

 (方法2) grouping sets!!

  select e.job,e.deptno,sum(e.sal) from emp group by grouping sets((e.job),(e.deptno),(null));

 

 (方法3

  select distinct

       sum(e.sal) over(partition by e.job) 岗位总工资,

       sum(e.sal) over(partition by e.deptno) 部门总工资,

       sum(e.sal) over() 雇员总工资

  from emp e;

 (方法3grouping sets(不重要)

  select sum(e.sal),

       decode(grouping_id(e.job), 1, 'deptno合计', e.job) job,

       decode(grouping_id(e.deptno), 1, 'job合计', e.deptno) deptno,

       decode(grouping_id(e.job),

              1,

              decode(grouping_id(e.deptno), 1, '总计')) sal_sum

  from emp e

 group by grouping sets((e.job),(e.deptno),(null));

  

(方法4cube (不重要)

   select *

   from (select sum(e.sal),

                decode(grouping_id(e.job), 1, '合计', e.job) job,

                decode(grouping_id(e.deptno), 1, '合计', e.deptno) deptno

           from emp e

          group by cube(e.job, e.deptno)) a

  where a.job '合计

        or(a.job is not null and a.deptno ='合计')

18、一道SQL语句面试题,关于group by

表内容:

2005-05-09 

2005-05-09 

2005-05-09 

2005-05-09 

2005-05-10 

2005-05-10 

2005-05-10 

如果要生成下列结果该如何写sql语句?

            胜 

2005-05-09 2

2005-05-10 2

------------------------------------------

create table tmp(rq varchar2(10),shengfu varchar2(1))

insert into tmp values('2005-05-09','')

insert into tmp values('2005-05-09','')

insert into tmp values('2005-05-09','')

insert into tmp values('2005-05-09','')

insert into tmp values('2005-05-10','')

insert into tmp values('2005-05-10','')

insert into tmp values('2005-05-10','')

答案:

(方法1):

select tt.rq ",

       (select count(*) from tmp where tt.rq rq and shengfu '') ,

       (select count(*) from tmp where tt.rq rq and shengfu '') 

  from tmp tt group by tt.rq order by tt.rq;

(方法2):

select rq ",

       sum(case when shengfu 'then else end) ,

       sum(case when shengfu 'then else end) 

  from tmp group by rq order by rq;

192.请教一个面试中遇到的SQL语句的查询问题

  --表中有C三列,SQL语句实现:当A列大于B列时选择A列否则选择B列,当B列大于C列时选择B列否则选择C列。

create table compare(A number(2),B number(2), number(2));

insert into compare values (70, 80, 98);

select from compare;

答案:

select (case when then else end),

       (case when then else end)

  from compare

20、有一张表,里面有3个字段:语文,数学,英语。其中有3条记录分别表示语文70分,数学80分,英语58分,请用一条sql语句查询出这三条记录并按以下条件显示出来(并写出您的思路):  

   大于或等于80表示优秀,大于或等于60表示及格,小于60分表示不及格。  

       显示格式:  

       语文              数学                英语  

       及格              优秀                不及格  

*/  

------------------------------------------

create table exam("语文number(2),"数学number(2), "英语number(2));

insert into exam values (70, 80, 58);

select from exam;

答案:

select case

         when 语文 >= 80 then '优秀'

         when 语文 >= 60 then '及格'

         else '不及格'

       end 语文,

       case

         when 数学 >= 80 then '优秀'

         when 数学 >= 60 then '及格'

         else '不及格'

       end 数学,

       case

         when 英语 >= 80 then '优秀'

         when 英语 >= 60 then '及格'

         else '不及格'

       end 英语

  from exam;

21、请用一个sql语句得出结果

table1,table2中取出如table3所列格式数据(如使用存储过程也可以)

table1

月份mon 部门dep 业绩yj

-------------------------------

一月份      01      10

一月份      02      10

一月份      03      5

二月份      02      8

二月份      04      9

三月份      03      8

table2

部门dep      部门名称dname

--------------------------------

      01      国内业务一部

      02      国内业务二部

      03      国内业务三部

      04      国际业务部

table3 result

部门dep 一月份      二月份      三月份

--------------------------------------

      01      10             

      02      10             

      03                        8

      04                   

------------------------------------------

*/

create table table1(mon varchar2(10), dep number(2), yj number(2));

insert into table1 values ('一月份', 01, 10);

insert into table1 values ('一月份', 02, 10);

insert into table1 values ('一月份', 03, 5);

insert into table1 values ('二月份', 02, 8);

insert into table1 values ('二月份', 04, 9);

insert into table1 values ('三月份', 03, 8);

select from table1;

create table table2(dep number(2), dname varchar2(20));

insert into table2 values(01, '国内业务一部');

insert into table2 values(02, '国内业务二部');

insert into table2 values(03, '国内业务三部');

insert into table2 values(04, '国际业务部'); 

select from table2;

答案:

(方法1):

select dep,

       (select yj from table1 where mon '一月份and dep table2.dep) 一月份,

       (select yj from table1 where mon '二月份and dep table2.dep) 二月份,

       (select yj from table1 where mon '三月份and dep table2.dep) 三月份

  from table2;

(方法2):

select a.dep,

       sum(case when b.mon '一月份then b.yj else end)  一月份,

       sum(case when b.mon '二月份then b.yj else end)  二月份,

       sum(case when b.mon '三月份then b.yj else end)  三月份

  from table2 left join table1 on a.dep=b.dep group by a.dep order by a.dep 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值