oracle--查詢練習(xí)

查詢練習(xí)

1.查詢工資大于12000的員工姓名和工資

select last_name, salary
from employees
where salary > 12000

2.查詢員工號為176的員工的姓名和部門號

select last_name, department_id
from employees
where employee_id = 176

3.選擇工資不在5000到12000的員工的姓名和工資

select last_name, salary
from employees
where salary not between 5000 and 12000

4.選擇雇用時間在1998-02-01到1998-05-01之間的員工姓名搜变,job_id和雇用時間

select last_name, job_id, hire_date
from employees
where hire_date between '1-2月 -1998' and '1-5月 -1998'

5.選擇在20或50號部門工作的員工姓名和部門號

select last_name, department_id
from employees
where department_id in (20, 50)

6.選擇在1994年雇用的員工的姓名和雇用時間

select last_name, hire_date
from employees
where hire_date like '% -94'

7.選擇公司中沒有管理者的員工姓名及job_id

select last_name, job_id
from employees
where manager_id is null

8.選擇公司中有獎金的員工姓名,工資和獎金級別

select last_name, salary, commission_pct
from employees
where commission_pct is not null

9.選擇員工姓名的第三個字母是a的員工姓名

select last_name 
from employees
where last_name like '__a%'

10.選擇姓名中有字母a和e的員工姓名

select last_name
from employees
where last_name like '%a%' and last_name like '%e%'

11.顯示系統(tǒng)時間

select to_char(sysdate,'yyyy-mm-dd hh:mi:ss') from dual;

12.查詢員工號,姓名,工資,以及工資提高百分之20%后的結(jié)果(new salary)

select employee_id,last_name,salary,salary*1.2 "new salary" from employees;

13.將員工的姓名按首字母排序惫确,并寫出姓名的長度(length)

select last_name,length(last_name)
from employees
order by last_name

14.查詢各員工的姓名,并顯示出各員工在公司工作的月份數(shù)(worked_month)。

select last_name,months_between(sysdate,hire_date) worked month from employees

15.查詢員工的姓名固蛾,以及在公司工作的月份數(shù)(worked_month)结执,并按月份數(shù)降序排列

select last_name, months_between(sysdate, hire_date) worked_month
from employees
order by worked_month desc

16.做一個查詢,產(chǎn)生下面的結(jié)果

<last_name> earns <salary> monthly but wants <salary*3>

結(jié)果.PNG
select last_name || ' earns ' || salary || ' monthly but wants ' || salary * 3
from employees

17.使用decode函數(shù)艾凯,按照下面的條件:

decode函數(shù).PNG
select last_name, job_id, decode(job_id, 'AD_PRES', 'A',
                                         'ST_MAN', 'B',
                                         'IT_PROG', 'C',
                                         'SA_REP', 'D',
                                         'ST_CLERK', 'E',
                                         'F') GRADE
from employees

18.將第7題的查詢用case函數(shù)再寫一遍献幔。

select last_name, job_id, case job_id when 'AD_PRES' then 'A'
                                      when 'ST_MAN' then 'B'
                                      when 'IT_PROG' then 'C'
                                      when 'SA_REP' then 'D'
                                      when 'ST_CLERK' then 'E'
                                      else 'F'
                           end            
from employees

19.查詢公司員工工資的最大值,最小值趾诗,平均值蜡感,總和

Select max(salary), min(salary), avg(salary), sum(salary)
From employees

20.查詢各job_id的員工工資的最大值,最小值恃泪,平均值郑兴,總和

Select job_id, max(salary), min(salary), avg(salary), sum(salary)
From employees
Group by job_id

21.選擇具有各個job_id的員工人數(shù)

Select job_id, count(employee_id)
From employees
Group by job_id;

22.查詢員工最高工資和最低工資的差距(DIFFERENCE)

Select max(salary) – min(salary) difference
From employees

23.查詢各個管理者手下員工的最低工資,其中最低工資不能低于6000贝乎,沒有管理者的員工不計算在內(nèi)

Select manager_id, min(salary)
From employees
Where manager_id is not null
Group by manager_id
Having min(salary) >= 6000

24.查詢所有部門的名字情连,location_id,員工數(shù)量和工資平均值

Select department_name, location_id, count(employee_id), avg(salary)
From employees e join departments d
On e.department_id = d.department_id
Group by department_name, location_id

一览效、找到員工表中工資最高的前三名:

關(guān)于行號
1. rownum永遠按照默認的順序生成
2. rownum只能使用 < <=; 不能使用> >=

SQL> select rownum,empno,ename,sal
  2  from (select * from emp order by sal desc)
  3  where rownum<=3;

    ROWNUM      EMPNO ENAME             SAL                                                                                                                                                             
---------- ---------- ---------- ----------                                                                                                                                                             
         1       7839 KING             5000                                                                                                                                                             
         2       7788 SCOTT            3000                                                                                                                                                             
         3       7902 FORD             3000 

2.rownum只能使用 < <=; 不能使用> >=

SQL>  select *
  2   from  (select rownum r,e1.*
  3      from (select * from emp order by sal) e1
  4      where rownum <=8
  5     )
  6   where r >=5;

臨時表:

1. create global temporary table *****
2.自動創(chuàng)建: order by
特點:當事務(wù)或者會話結(jié)束的時候却舀,表中的數(shù)據(jù)自動刪除

所以 oracle中事務(wù)提交了 數(shù)據(jù)不一定保存下來了(臨時表),數(shù)據(jù)不在锤灿,但表還在

SQL> create global temporary table test2
  2  (tid number,tname varchar2(20))
  3  on commit delete rows;

表已創(chuàng)建禁筏。

SQL> insert into test2 values(1,'Tom');

已創(chuàng)建 1 行。

SQL> select * from test2;

       TID TNAME                                                                                                                                                                                        
---------- --------------------                                                                                                                                                                         
         1 Tom                                                                                                                                                                                          

SQL> commit;

提交完成衡招。

SQL> select * from test2;

未選定行

SQL> desc test2
 名稱                                                                                                              是否為空? 類型
 ----------------------------------------------------------------------------------------------------------------- -------- ----------------------------------------------------------------------------
 TID                                                                                                                        NUMBER
 TNAME                                                                                                                      VARCHAR2(20)

二篱昔、找到員工表中薪水大于本部門平均薪水的員工:

相關(guān)子查詢:將主查詢中的值作為參數(shù)傳遞給子查詢:

SQL> select empno,ename,sal,(select avg(sal) from emp where deptno=e.deptno) avgsal
  2  from emp e
  3  where sal > (select avg(sal) from emp where deptno=e.deptno);

     EMPNO ENAME             SAL     AVGSAL                                                                                                                                                             
---------- ---------- ---------- ----------                                                                                                                                                             
      7499 ALLEN            1600 1566.66667                                                                                                                                                             
      7566 JONES            2975       2175                                                                                                                                                             
      7698 BLAKE            2850 1566.66667                                                                                                                                                             
      7788 SCOTT            3000       2175                                                                                                                                                             
      7839 KING             5000 2916.66667                                                                                                                                                             
      7902 FORD             3000       2175  

解:

SQL> select e.empno,e.ename,e.sal,d.avgsal
  2  from emp e,(select deptno,avg(sal) avgsal from emp group by deptno) d
  3  where e.deptno=d.deptno and e.sal > d.avgsal;

     EMPNO ENAME             SAL     AVGSAL                                                                                                                                                             
---------- ---------- ---------- ----------                                                                                                                                                             
      7698 BLAKE            2850 1566.66667                                                                                                                                                             
      7499 ALLEN            1600 1566.66667                                                                                                                                                             
      7902 FORD             3000       2175                                                                                                                                                             
      7788 SCOTT            3000       2175                                                                                                                                                             
      7566 JONES            2975       2175                                                                                                                                                             
      7839 KING             5000 2916.66667  

三、統(tǒng)計每年入職的員工個數(shù)

每年入職的員工個數(shù).PNG
SQL> select count(*) Total,
  2         sum(decode(to_char(hiredate,'yyyy'),'1980',1,0)) "1980",
  3         sum(decode(to_char(hiredate,'yyyy'),'1981',1,0)) "1981",
  4         sum(decode(to_char(hiredate,'yyyy'),'1982',1,0)) "1982",
  5         sum(decode(to_char(hiredate,'yyyy'),'1987',1,0)) "1987"
  6  from emp;

     TOTAL       1980       1981       1982       1987                          
---------- ---------- ---------- ---------- ----------                          
        14          1         10          1          2

1.查詢和Zlotkey相同部門的員工姓名和雇用日期

select last_name, hire_date
from employees
where department_id = (
      select department_id
      from employees
      where last_name = 'Zlotkey'
)

2.查詢工資比公司平均工資高的員工的員工號始腾,姓名和工資州刽。

select employee_id, last_name, salary
from employees
where salary > (
      select avg(salary)
      from employees
)

3.查詢各部門中工資比本部門平均工資高的員工的員工號, 姓名和工資

select employee_id, last_name, salary
from employees e
where salary > (
      select avg(salary)
      from employees
      where department_id = e.department_id
)

4.查詢和姓名中包含字母u的員工在相同部門的員工的員工號和姓名

select employee_id, last_name
from employees
where department_id in (
      select department_id
      from employees
      where last_name like '%u%'
)

5.查詢在部門的location_id為1700的部門工作的員工的員工號,

select employee_id
from employees
where department_id in (
      select department_id
      from departments
      where location_id = 1700
)

6.查詢管理者是King的員工姓名和工資

select last_name, salary
from employees
where manager_id in (
      select employee_id
      from employees
      where last_name = 'King'
)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末浪箭,一起剝皮案震驚了整個濱河市穗椅,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌奶栖,老刑警劉巖匹表,帶你破解...
    沈念sama閱讀 210,978評論 6 490
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異宣鄙,居然都是意外死亡袍镀,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 89,954評論 2 384
  • 文/潘曉璐 我一進店門冻晤,熙熙樓的掌柜王于貴愁眉苦臉地迎上來苇羡,“玉大人,你說我怎么就攤上這事鼻弧∩杞” “怎么了锦茁?”我有些...
    開封第一講書人閱讀 156,623評論 0 345
  • 文/不壞的土叔 我叫張陵,是天一觀的道長叉存。 經(jīng)常有香客問我码俩,道長,這世上最難降的妖魔是什么歼捏? 我笑而不...
    開封第一講書人閱讀 56,324評論 1 282
  • 正文 為了忘掉前任稿存,我火速辦了婚禮,結(jié)果婚禮上甫菠,老公的妹妹穿的比我還像新娘挠铲。我一直安慰自己,他們只是感情好寂诱,可當我...
    茶點故事閱讀 65,390評論 5 384
  • 文/花漫 我一把揭開白布拂苹。 她就那樣靜靜地躺著,像睡著了一般痰洒。 火紅的嫁衣襯著肌膚如雪瓢棒。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,741評論 1 289
  • 那天丘喻,我揣著相機與錄音脯宿,去河邊找鬼。 笑死泉粉,一個胖子當著我的面吹牛连霉,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播嗡靡,決...
    沈念sama閱讀 38,892評論 3 405
  • 文/蒼蘭香墨 我猛地睜開眼跺撼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了讨彼?” 一聲冷哼從身側(cè)響起歉井,我...
    開封第一講書人閱讀 37,655評論 0 266
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎哈误,沒想到半個月后哩至,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,104評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡蜜自,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,451評論 2 325
  • 正文 我和宋清朗相戀三年菩貌,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片袁辈。...
    茶點故事閱讀 38,569評論 1 340
  • 序言:一個原本活蹦亂跳的男人離奇死亡菜谣,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出晚缩,到底是詐尸還是另有隱情尾膊,我是刑警寧澤,帶...
    沈念sama閱讀 34,254評論 4 328
  • 正文 年R本政府宣布荞彼,位于F島的核電站冈敛,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏鸣皂。R本人自食惡果不足惜抓谴,卻給世界環(huán)境...
    茶點故事閱讀 39,834評論 3 312
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望寞缝。 院中可真熱鬧癌压,春花似錦、人聲如沸荆陆。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,725評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽被啼。三九已至帜消,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間浓体,已是汗流浹背泡挺。 一陣腳步聲響...
    開封第一講書人閱讀 31,950評論 1 264
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留命浴,地道東北人娄猫。 一個月前我還...
    沈念sama閱讀 46,260評論 2 360
  • 正文 我出身青樓,卻偏偏與公主長得像生闲,于是被迫代替她去往敵國和親媳溺。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 43,446評論 2 348

推薦閱讀更多精彩內(nèi)容

  • 查詢是數(shù)據(jù)的一個重要操作跪腹。用戶發(fā)送查詢請求褂删,經(jīng)編譯軟件變異成二進制文件供服務(wù)器查詢,后返回查詢結(jié)果集給用戶冲茸,查詢會...
    產(chǎn)品小正閱讀 1,372評論 0 2
  • SQL語言基礎(chǔ) 本章屯阀,我們將會重點探討SQL語言基礎(chǔ),學(xué)習(xí)用SQL進行數(shù)據(jù)庫的基本數(shù)據(jù)查詢操作轴术。另外請注意本章的S...
    厲鉚兄閱讀 5,314評論 2 46
  • 漸漸的習(xí)慣了坐在圖書館靠窗的位置难衰,習(xí)慣了看著窗外晚霞慢慢褪去了顏色,習(xí)慣了看著遠處的建筑和更遠處的高山逗栽,漸...
    我家的狗狗叫小黑閱讀 290評論 0 0
  • 文章系列《RACSignal 》《RACDisposable》《RACSubject盖袭、RACReplaySubje...
    Codepgq閱讀 7,368評論 6 18
  • 今天和朋友一起吃飯,突然聊到孩子,朋友說鳄虱,感覺我對孩子保護有點過于小心了弟塞,建議我可以放手讓孩子學(xué)會更獨立一...
    云沐媽媽閱讀 142評論 0 0