ORACLE_SQL 練手 part4

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

1.萬事俱備之ORACLE_SQL 練手 part1

--用到的表
select * from regions;
select * from countries;
select * from locations;
select * from departments;
select * from jobs;

update countries set country_name = 'Others' where country_id='ZZ';

--1. 各個部門平均、最大、最小工資、人數,按照部門號升序排列。
select department_id,count(*) nums,max(salary) maxsal,min(salary) minsal
from employees
group by department_id  
order by department_id ASC;

--2. 各個部門中工資大於5000 的員工人數。
SELECT department_id,COUNT(*) nums_good  from employees
where salary>5000
group by department_id 
order by department_id;

--3. 各個部門平均工資和人數,按照部門名字升序排列。
select department_name,empp.department_id, round(avg(salary)) avgsal,count(*) nums
from employees empp left join departments d
on empp.department_id = d.department_id
group by empp.department_id,department_name
order by department_name;

---一種答案,用where連接
SELECT DPTNAME,AVG(SALARY),COUNT(*) FROM
           (SELECT
               (SELECT DEPT.DEPARTMENT_NAME FROM DEPARTMENTS DEPT
               WHERE DEPT.DEPARTMENT_ID = EMP.DEPARTMENT_ID) DPTNAME,
               EMP.SALARY
    FROM EMPLOYEES EMP)
    GROUP BY DPTNAME
    ORDER BY DPTNAME;

--4. 列出每個部門中有同樣工資的員工的統計信息,列出他們的部門號,工資,人數。
---where
select a.department_id, a.salary ,count(*) nums_same 
from employees a , employees b
where a.salary =b.salary
and a.employee_id!=b.employee_id
and a.department_id=b.department_id
group by a.department_id,a.salary;
---join(默認inner join) 因爲表相同,所以取都有的那部分
select a.department_id, a.salary ,count(*) nums_same 
from employees a   join employees b
on a.salary =b.salary
and a.employee_id!=b.employee_id
and a.department_id=b.department_id
group by a.department_id,a.salary;



--5. 列出同部門中工資高於1000 的員工數量超過2 人的部門,顯示部門名字、地區名稱。
---兩表顯連接+子查詢
select d.department_name,l.city
from departments d left join locations l
on d.location_id=l.location_id
where d.department_id in
(select department_id from  
employees 
where salary>1000
group by department_id
having  count(*)>2);
---三表隱形連接
SELECT D.DEPARTMENT_NAME,L.CITY,COUNT(*)
     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(*) > 2;

--6. 哪些員工的工資,高於整個公司的平均工資,列出員工的名字和工資(降序) 。
---單獨不分組的聚合函數,需要單獨寫子語句
select  first_name||' '||last_name ename, salary  from employees
where salary>(select round(avg(salary)) from employees) 
order by salary desc;

--7. 哪些員工的工資,介於50 號和80 號部門平均工資之間。
select first_name||' '||last_name ename, salary  from employees
where salary between 
(select round(avg(salary)) from employees where department_id=50) 
and (select round(avg(salary)) from employees where department_id=80);

--8. 所在部門平均工資高於5000 的員工名字。
select first_name||' '||last_name ename,department_id,salary from employees
where department_id in
(select department_id from employees group by department_id having  avg(salary)>5000 );

--9. 列出各個部門中工資最高的員工的信息:名字、部門號、工資。
---報錯~~ not properly ended
select employee_id from employees
group by department_id,employee_id
where salary =(select  max(salary) from employees);

---報錯~~ not a group expression ended
select employee_id from employees
group by department_id,employee_id
having salary =(select  max(salary) from employees);

--- good! but not enough only employee_id
select DEPARTMENT_ID,employee_id,FIRST_NAME || ' ' || LAST_NAME ename from employees
group by department_id,employee_id,salary
having (salary,department_Id) in(select  max(salary),department_Id from employees group by department_id );

--答案 (X,Y) IN (select x,y from z...)
SELECT FIRST_NAME || ' ' || LAST_NAME AS NAME
            ,SALARY,DEPARTMENT_ID
     FROM EMPLOYEES
     WHERE (DEPARTMENT_ID,SALARY) IN
           (SELECT DEPARTMENT_ID,MAX(SALARY)
            FROM EMPLOYEES
            GROUP BY DEPARTMENT_ID); 

--10. 最高的部門平均工資是多少。
---max((avg))
select max(avsal)maxsal from 
(select round(avg(salary)) avsal,department_id from employees 
group by department_id);

---想多拿個部門ID,搞不定-  - 窘 - -
select department_id,max(avsal) from
(select round(avg(salary)) avsal,department_id from employees 
group by department_id );

--多拿個部門ID (avg order) rownum
select  * from 
(select  round(avg(salary)) avsal,department_id  from employees 
group by department_id
order by avsal desc) 
where rownum =1;

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