萬事俱備之ORACLE_SQL 練手 part5

更多參考和數據腳本下載:

1.萬事俱備之ORACLE_SQL 練手 part1

select * from regions;
select * from countries;
select * from locations;
select * from departments;
select * from jobs;
-- 1. 哪些部門的人數比90 號部門的人數多。
---區別count(*)、count(1)、count(column)
----•count(1) 中的 1 並不是表示爲第一個 column
----•count(*) 跟 count(1) 的結果一樣,包括對NULL的統計
----•count(column) 是不包括對NULL的統計
---所以以下包括部分column爲null的項
select department_id,count(department_id) nums
from employees
group by department_id
having count(department_id)>
(select count(*) nums from employees where department_id=90)
;
---count 結果不包括department_id爲null的項
select department_id,count(department_id) nums
from employees
group by department_id
having count(*)>
(select count(department_id) nums from employees where department_id=90)
;

--2. Den (FIRST_NAME) 、Raphaely (LAST_NAME) 的領導是誰(非關聯子查詢in) 。
-- 區別 in 和exists
---in用的是父查詢表的索引,此處父子查詢表相同
select * from employees
where employee_id in (select manager_id from employees 
where FIRST_NAME='Den' and last_name='Raphaely' );
---exists 用到了子查詢表的索引,如果子查詢的表小,索引就小,速度就快
select * from employees empp
where  exists (select manager_id from employees 
where FIRST_NAME='Den' and last_name='Raphaely'and empp.employee_id=manager_id);

--3.Den (FIRST_NAME) 、Raphaely (LAST_NAME) 領導誰(樹形查詢)
---樹形查詢 start with+起點條件 connect by目標條件  
---- prior x_id = y_id (prior 指定目標的對比列 就是找與x_id相同的y_id)
----最大的好處就是不用關聯表,當表大的時候優勢可能比較明顯
select * from employees start with
 FIRST_NAME='Den' and last_name='Raphaely' 
 connect by employee_id = prior manager_id;
----prior employee_id =  manager_id 找對應的子結點
select * from employees start with 
FIRST_NAME='Den' and last_name='Raphaely'  
connect by prior employee_id =  manager_id;

--4. Den (FIRST_NAME) 、Raphaely (LAST_NAME) 的領導是誰(關聯子查詢exists ) 。
select * from employees empp
where  exists
(select manager_id from employees where FIRST_NAME='Den' 
and last_name='Raphaely'and empp.employee_id=manager_id);

---找下屬
select * from employees empp
where  exists
(select null from employees where FIRST_NAME='Den' 
and last_name='Raphaely'and empp.manager_id=employee_id);

--5. 列出在同一部門共事,入職日期晚但工資高於其他同事的員工:名字、工資、入職日期

--not good
select FIRST_NAME || ' ' || LAST_NAME ename ,salary,hire_date from employees empp
,(select department_id,min(salary) minsal from employees
group by department_id) tt
where empp.department_id= tt.department_id
and empp.salary>tt.minsal
and empp.haire_date;

---答案是通過 分析函數把同部門工資比自己低,又比自己先入職的最小工資篩選出來。再做一次查詢,篩選出有比自己工資小但先後入職。
--包含相同日期工資高的
SELECT *
from
(select department_id,first_name || ' ' || last_name AS ename,hire_date,salary,
MIN(salary) over(PARTITION BY department_id ORDER BY hire_date ) AS p_cmin
FROM employees)
where salary>p_cmin;

--work 不包含相同日期工資高的
SELECT *
FROM (SELECT department_id,first_name || ' ' || last_name AS ename,
hire_date AS hdate,
salary AS sal,
MIN(salary) over(PARTITION BY e.department_id ORDER BY e.hire_date RANGE BETWEEN unbounded preceding
AND 1 preceding)as min_salary
FROM    employees e
ORDER BY department_id,hire_date
)
WHERE sal> MIN_salary;
--隱形連接,慢
select distinct e1.first_name||' '||e1.last_name,e1.salary,e1.hire_date 
from employees e1 join employees e2 
on e1.department_id=e2.department_id 
where e1.hire_date> e2.hire_date and  e1.salary>e2.salary;

--6. Finance 部門有哪些職位(非關聯子查詢in) 。
select distinct job_id from employees  
where department_id in(select department_id from departments where departments.department_name ='Finance');

--7. Finance 部門有哪些職位(關聯子查詢exists) 。
select distinct job_id from employees  empp
where exists(select department_id from departments 
where departments.department_name ='Finance' and empp.department_id=department_id
);

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章