複雜業務 有點難度 目標熟能生巧 信手拈來函數
使用Oracle自帶的hr用戶ui
表結構:spa
表間關係code
問題:blog
個人解答:ip
1 --1. 各個部門平均、最大、最小工資、人數,按照部門號升序排列。
2 -- 既然是各個部門 分組便可 分組後使用聚合函數
3 SELECT ROUND(AVG(e.SALARY)) 平均工資,MAX(e.SALARY) 最大工資,MIN(e.SALARY) 最小工資,count(1) 人數 FROM EMPLOYEES e GROUP BY e.DEPARTMENT_ID ORDER BY e.DEPARTMENT_ID ASC;
--2. 各個部門中工資大於5000的員工人數。 -- 先過濾掉工資大於5000的 -- 再分組
SELECT e.DEPARTMENT_ID 部門編號,count(1) 人數 FROM EMPLOYEES e WHERE e.SALARY>5000 GROUP BY e.DEPARTMENT_ID;
1 --3. 各個部門平均工資和人數,按照部門名字升序排列。
2 -- 要顯示部門名稱 須要employees和departments作關聯
3 -- 我根據部門編號分組 在查詢列中作了一個子查詢
4 --(SELECT d.DEPARTMENT_NAME FROM DEPARTMENTS d WHERE d.DEPARTMENT_ID=e.DEPARTMENT_ID)
5
6 SELECT count(1) cnt,AVG(e.SALARY) agv,(SELECT d.DEPARTMENT_NAME FROM DEPARTMENTS d WHERE d.DEPARTMENT_ID=e.DEPARTMENT_ID) department_name FROM EMPLOYEES e GROUP BY e.DEPARTMENT_ID; 7
8 -- 結果還要按照部門名字升序排列 全部把上面的查詢做爲查詢源表 繼續操做
9 SELECT s.cnt,s.agv,s.department_name FROM(SELECT count(1) cnt,AVG(e.SALARY) agv,(SELECT d.DEPARTMENT_NAME FROM DEPARTMENTS d WHERE d.DEPARTMENT_ID=e.DEPARTMENT_ID) department_name FROM EMPLOYEES e GROUP BY e.DEPARTMENT_ID) s 10 ORDER BY s.department_name;
1 --4. 列出每一個部門中有一樣工資的員工的統計信息,列出他們的部門號,工資,人數。 2 -- 沒啥思路 既然是每一個部門 按照department_id分組 過濾條件工資相同 部門相同 外加僱員編號不能同樣 這個千萬不能忘記 否則就不是同部門 同工資的員工 沒有把本身給過濾掉 3 -- 兩表自關聯 4 SELECT e.DEPARTMENT_ID,COUNT(1),e.SALARY FROM EMPLOYEES e,EMPLOYEES ee WHERE e.SALARY=ee.SALARY AND e.DEPARTMENT_ID=ee.DEPARTMENT_ID AND e.EMPLOYEE_ID<>ee.EMPLOYEE_ID GROUP BY e.DEPARTMENT_ID, e.SALARY ORDER BY e.DEPARTMENT_ID;
DEPARTMENT_ID COUNT(1) SALARY
------------- ---------- ----------
50 2 2200
50 2 2400
50 20 2500
50 6 2600
50 2 2700
50 6 2800
50 2 2900
50 2 3000
50 6 3100
50 12 3200
50 2 3300
50 2 3600
60 2 4800
80 2 6200
80 2 7000
80 2 7500
80 2 8000
80 2 9000
80 6 9500
80 6 10000
80 2 10500
80 2 11000
90 2 17000
選定了 23 行
1 --5. 列出同部門中工資高於1000 的員工數量超過2 人的部門,顯示部門名字、地區名稱。 2 -- 工資大於1000 count(1)>2 3 -- 部門名稱 地區 須要 departments和locations表關聯 4 --先把employees departments locations三表關聯起來 5 -- 分組使用department_id和city 6 -- having 過濾分組後的結果 7 SELECT d.DEPARTMENT_NAME,l.CITY,count(1) FROM EMPLOYEES e,DEPARTMENTS d,LOCATIONS l WHERE e.DEPARTMENT_ID=d.DEPARTMENT_ID AND d.LOCATION_ID=l.LOCATION_ID AND e.SALARY>1000 GROUP BY d.DEPARTMENT_NAME,l.CITY HAVING COUNT(1)>2;
DEPARTMENT_NAME CITY COUNT(1)
------------------------------ ------------------------------ ----------
IT Southlake 5
Sales Oxford 34
Shipping South San Francisco 45
Purchasing Seattle 6
Executive Seattle 3
Finance Seattle 6
選定了 6 行
1 --6. 哪些員工的工資,高於整個公司的平均工資,列出員工的名字和工資(降序) 2 -- 算出平均工資 3 -- 公司平均工資 4 SELECT AVG(e.SALARY) FROM EMPLOYEES e; 5 -- 高於 6 SELECT ee.FIRST_NAME||' '||ee.LAST_NAME,ee.SALARY FROM EMPLOYEES ee WHERE ee.SALARY >(SELECT AVG(e.SALARY) FROM EMPLOYEES e) ORDER BY ee.SALARY desc;
EE.FIRST_NAME||''||EE.LAST_NAME SALARY
---------------------------------------------- ----------
Steven King 24000
Neena Kochhar 17000
Lex De Haan 17000
John Russell 14000
Karen Partners 13500
Michael Hartstein 13000
Shelley Higgins 12008
Nancy Greenberg 12008
Alberto Errazuriz 12000
Lisa Ozer 11500
Gerald Cambrault 11000
Ellen Abel 11000
Den Raphaely 11000
Clara Vishney 10500
Eleni Zlotkey 10500
Peter Tucker 10000
Janette King 10000
Hermann Baer 10000
Harrison Bloom 10000
Tayler Fox 9600
Danielle Greene 9500
Patrick Sully 9500
David Bernstein 9500
Allan McEwen 9000
Daniel Faviet 9000
Alexander Hunold 9000
Peter Hall 9000
Alyssa Hutton 8800
Jonathon Taylor 8600
Jack Livingston 8400
William Gietz 8300
John Chen 8200
Adam Fripp 8200
Christopher Olsen 8000
Matthew Weiss 8000
Lindsey Smith 8000
Payam Kaufling 7900
Jose Manuel Urman 7800
Ismael Sciarra 7700
Nanette Cambrault 7500
Louise Doran 7500
William Smith 7400
Elizabeth Bates 7300
Mattea Marvins 7200
Sarath Sewall 7000
Oliver Tuvault 7000
Kimberely Grant 7000
Luis Popp 6900
David Lee 6800
Susan Mavris 6500
Shanta Vollman 6500
選定了 51 行
1 --7. 哪些員工的工資,介於50號 和80號部門平均工資之間 2 -- 50部門平均工資 3 SELECT AVG(e.SALARY) min FROM EMPLOYEES e WHERE e.DEPARTMENT_ID=50; 4 --80部門平均工資 5 SELECT AVG(e.SALARY) max FROM EMPLOYEES e WHERE e.DEPARTMENT_ID=80; 6 7 -- 使用between and 8 SELECT ee.FIRST_NAME||' '||ee.LAST_NAME,ee.SALARY FROM EMPLOYEES ee WHERE ee.SALARY BETWEEN (SELECT AVG(e.SALARY) min FROM EMPLOYEES e WHERE e.DEPARTMENT_ID=50) AND (SELECT AVG(e.SALARY) max FROM EMPLOYEES e WHERE e.DEPARTMENT_ID=80);
EE.FIRST_NAME||''||EE.LAST_NAME SALARY
---------------------------------------------- ----------
Jennifer Whalen 4400
Pat Fay 6000
Susan Mavris 6500
William Gietz 8300
Bruce Ernst 6000
David Austin 4800
Valli Pataballa 4800
Diana Lorentz 4200
John Chen 8200
Ismael Sciarra 7700
Jose Manuel Urman 7800
Luis Popp 6900
Matthew Weiss 8000
Adam Fripp 8200
Payam Kaufling 7900
Shanta Vollman 6500
Kevin Mourgos 5800
Renske Ladwig 3600
Trenna Rajs 3500
Christopher Olsen 8000
Nanette Cambrault 7500
Oliver Tuvault 7000
Lindsey Smith 8000
Louise Doran 7500
Sarath Sewall 7000
Mattea Marvins 7200
David Lee 6800
Sundar Ande 6400
Amit Banda 6200
William Smith 7400
Elizabeth Bates 7300
Sundita Kumar 6100
Alyssa Hutton 8800
Jonathon Taylor 8600
Jack Livingston 8400
Kimberely Grant 7000
Charles Johnson 6200
Nandita Sarchand 4200
Alexis Bull 4100
Kelly Chung 3800
Jennifer Dilly 3600
Sarah Bell 4000
Britney Everett 3900
選定了 43 行
1 --8. 所在部門平均工資高於5000 的員工名字 2 -- 先算出平均工資高於5000的部門 3 SELECT AVG(e.SALARY),e.DEPARTMENT_ID FROM EMPLOYEES e GROUP BY e.DEPARTMENT_ID HAVING AVG(e.SALARY)>5000; 4 -- 再跟employees作關聯 算出對應部門的員工 5 -- 注意 不要再過濾工資大於5000 由於部門平均工資大於5000 不表示員工的工資大於5000 也能夠小於5000 6 SELECT ee.FIRST_NAME||' '||ee.LAST_NAME,ee.SALARY FROM EMPLOYEES ee,(SELECT AVG(e.SALARY) agv,e.DEPARTMENT_ID FROM EMPLOYEES e GROUP BY e.DEPARTMENT_ID HAVING AVG(e.SALARY)>5000) v 7 WHERE ee.DEPARTMENT_ID=v.DEPARTMENT_ID;
EE.FIRST_NAME||''||EE.LAST_NAME SALARY
---------------------------------------------- ----------
Nancy Greenberg 12008
Daniel Faviet 9000
John Chen 8200
Ismael Sciarra 7700
Jose Manuel Urman 7800
Luis Popp 6900
Michael Hartstein 13000
Pat Fay 6000
Hermann Baer 10000
Steven King 24000
Neena Kochhar 17000
Lex De Haan 17000
Shelley Higgins 12008
William Gietz 8300
Susan Mavris 6500
John Russell 14000
Karen Partners 13500
Alberto Errazuriz 12000
Gerald Cambrault 11000
Eleni Zlotkey 10500
Peter Tucker 10000
David Bernstein 9500
Peter Hall 9000
Christopher Olsen 8000
Nanette Cambrault 7500
Oliver Tuvault 7000
Janette King 10000
Patrick Sully 9500
Allan McEwen 9000
Lindsey Smith 8000
Louise Doran 7500
Sarath Sewall 7000
Clara Vishney 10500
Danielle Greene 9500
Mattea Marvins 7200
David Lee 6800
Sundar Ande 6400
Amit Banda 6200
Lisa Ozer 11500
Harrison Bloom 10000
Tayler Fox 9600
William Smith 7400
Elizabeth Bates 7300
Sundita Kumar 6100
Ellen Abel 11000
Alyssa Hutton 8800
Jonathon Taylor 8600
Jack Livingston 8400
Charles Johnson 6200
Alexander Hunold 9000
Bruce Ernst 6000
David Austin 4800
Valli Pataballa 4800
Diana Lorentz 4200
選定了 54 行
1 --9. 列出各個部門中工資最高的員工的信息:名字、部門號、工資 2 -- 先找出部門最高工資 部門號 3 SELECT MAX(e.SALARY) max_money,e.DEPARTMENT_ID FROM EMPLOYEES e GROUP BY e.DEPARTMENT_ID; 4 -- 做爲子表和員工表作關聯 5 SELECT ee.FIRST_NAME||' '||ee.LAST_NAME,ee.DEPARTMENT_ID,ee.SALARY FROM EMPLOYEES ee,(SELECT MAX(e.SALARY) max_money,e.DEPARTMENT_ID FROM EMPLOYEES e GROUP BY e.DEPARTMENT_ID) e WHERE ee.SALARY=e.max_money AND ee.DEPARTMENT_ID=e.DEPARTMENT_ID; 6 -- 或者使用in 工資和部門在... 這種思路是我還不知道的 7 SELECT ee.FIRST_NAME||' '||ee.LAST_NAME,ee.DEPARTMENT_ID,ee.SALARY FROM EMPLOYEES ee WHERE (ee.SALARY,ee.DEPARTMENT_ID) IN (SELECT MAX(e.SALARY) max_money,e.DEPARTMENT_ID FROM EMPLOYEES e GROUP BY e.DEPARTMENT_ID);
EE.FIRST_NAME||''||EE.LAST_NAME DEPARTMENT_ID SALARY
---------------------------------------------- ------------- ----------
Nancy Greenberg 100 12008
Den Raphaely 30 11000
Michael Hartstein 20 13000
Hermann Baer 70 10000
Steven King 90 24000
Shelley Higgins 110 12008
Adam Fripp 50 8200
Susan Mavris 40 6500
John Russell 80 14000
Jennifer Whalen 10 4400
Alexander Hunold 60 9000
選定了 11 行
1 --10. 最高的部門平均工資是多少。
2 -- 按部門分組 就平均工資
3 -- 求完了 直接使用max
4 SELECT max(AVG(e.SALARY)) FROM EMPLOYEES e GROUP BY e.DEPARTMENT_ID; 5 --或者作一個子查詢
6 SELECT MAX(s.avgsalary) FROM ( SELECT AVG(e.SALARY) avgsalary FROM EMPLOYEES e GROUP BY e.DEPARTMENT_ID) s
總結:沒有複雜的查詢 也沒有獨特的技巧 須要理解一些概念 好比4 一樣工資的員工 要過濾掉相同employee_idci