oracle 上機操做題整理

每一個題目都包含了一些知識點,認真作題慢慢領悟吧!sql

 

查找出當前用戶模式下,每張表的記錄數,以scott用戶爲例,結果應以下:
DEPT...................................4
EMP...................................14
BONUS.................................0
SALGRADE.............................5 oop

declare
    type tab_name is table of varchar2(80) index by binary_integer;
    t_name tab_name;
    coun number;
    str varchar(200);
begin
    select table_name bulk collect into t_name from user_tables;
    for i in t_name.first .. t_name.last loop
        str := 'select count(*) from '|| t_name(i);
        execute immediate str into coun;
        dbms_output.put_line(t_name(i)||'.....'||coun);
    end loop;
end;

某cc表數據以下:code

c1 c2 blog

--------------it

1 西io

1 安table

1 的ast

2 天class

2 氣date

3 好

„„

轉換爲

1 西安的

2 天氣

3 好 

要求:不能改變表結構及數據內容,僅在最後經過SELECT顯示出這個查詢結果

with temp as 
(select c1,c2,rownum c3 from cc )
select listagg(c2,'') within group (order by c3 ) from temp group by c1

請用一條sql語句查詢出scott.emp表中每一個部門工資前三位的數據,顯示結果以下:
DEPTNO SAL1 SAL2 SAL3
------ ---------- ---------- -------------------------------------
10 5000 2450 1300
20 3000 2975 1100
30 2850 1600 1500
則,該語句爲:

select deptno,max(decode(rn,1,sal)) sal1,
max(decode(rn,2,sal)) sal2,
max(decode(rn,3,sal)) sal3  from 
(
select deptno,sal,
row_number() over (partition by deptno order by sal desc nulls last) rn
from emp
 )
where rn<=3 
group by deptno

表nba記錄了nba(team VARCHAR2(10),y NUMBER(4))奪冠球隊的名稱及年份:
TEAM Y
-------------------- ------------------------------
活塞 1990
公牛 1991
公牛 1992
公牛 1993
火箭 1994
火箭 1995
公牛 1996
公牛 1997
公牛 1998
馬刺 1999
湖人 2000
湖人 2001
湖人 2002
馬刺 2003
活塞 2004
馬刺 2005
熱火 2006
馬刺 2007
凱爾特人 2008
湖人 2009
湖人 2010
請寫出一條SQL語句,查詢出在此期間連續得到冠軍的有哪些,其連續的年份的起止時間是多少,結果以下:
TEAM B E
-------------------- ---------- --------------------------------
公牛 1991 1993
火箭 1994 1995
公牛 1996 1998
湖人 2000 2002
湖人 2009 2010

select max(team) team,min(y) B,min(y)+count(*) E 
from 
(select a.team,a.y,rownum rn from nba a, nba b where a.y=b.y-1 and a.team=b.team) t 
group by (y-rn) order by min(y) 

 

查詢當前員工前一個入職的員工和後一個入職的員工與當前員工的薪資差別:

select ename,hiredate,sal,deptno,sal-pre_sal,sal-nex_sal from 
(
select ename,hiredate,sal,deptno,
lag(sal,1,0) over (order by hiredate) pre_sal,
lead(sal,1,0) over (order by hiredate ) nex_sal 
from emp
)

 

表 B 

c1                   c2

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

2005-01-01     1

2005-01-01     3

2005-01-02     5  

要求處理數據結果爲:

2005-01-01     4

2005-01-02     5

合計                 9   用一句sql完成:

select nvl(c1,'合計') , sum(c2) from B group rollup(c1)

 

有一個表 a ( x number(20) , y number(20) )

用最快高效的sql向該表插入 1 開始的連續的1000萬記錄

declare
    num number;
begin
    insert into a select rownum,rownum from dual connect by level < = 100000 ;
    for i in 1..10 loop
        select max (x) into num from a;
        insert into a select rownum+num,rownum+num from dual 
         connect by level < = 100000*i  ;
   end loop ;
end;
相關文章
相關標籤/搜索