1. 程式人生 > >SQL高階部分一(SET運算子 && 高階子查詢)

SQL高階部分一(SET運算子 && 高階子查詢)

一、SET運算子

將多個查詢用 SET 操作符連線組成一個新的查詢
select employee_id , department_id
from emp01
union all --相當於兩個集合相加, union A並B ,intersect ,A交B,MINUS 差集,A-B
select employee_id , department_id
from emp02

SET操作的注意事項

在SELECT 列表中的列名和表示式在數量和資料型別上要相對應
括號可以改變執行的順序
ORDER BY 子句:
只能在語句的最後出現
可以使用第一個查詢中的列名, 別名或相對位置

注意:
除 UNION ALL之外,系統會自動將重複的記錄刪除
系統將第一個查詢的列名顯示在輸出中
除 UNION ALL之外,系統自動按照第一個查詢中的第一個列的升序排列 
SELECT department_id, TO_NUMBER(null) location, hire_date
FROM   employees
UNION
SELECT department_id, location_id,  TO_DATE(null)

FROM   departments;
所查詢的列一定要一一對應,沒有的可以用null來代替

SELECT employee_id, job_id,salary
FROM   employees
UNION
SELECT employee_id, job_id,0 --salary可以用0代替
FROM   job_history;


指定列不顯示column 指定列的名字 noprint;

實現按
I'd like to teach
the world to
sing 
顯示 的SQL語句
SELECT 'sing'  "My dream", 3 a_nu
FROM dual
UNION
SELECT 'I`d like to teach',1
FROM dual
UNION 
SELECT 'the world to',1
FROM dual
order by 2
SQL> column a_nu noprint;--指定a_nu列不顯示

二、高階子查詢

多列子查詢
主查詢與子查詢返回的多個列進行比較
舉例:
查詢與141號或174號員工的manager_id和department_id相同的其他員工
的employee_id, manager_id, department_id  
1)成對比較舉例
select manager_id , employee_id,department_id
from employees
where (manager_id,department_id) in (
                                   select manager_id,department_id
                                   from employees
                                   where employee_id in (141,174)
                                    )
and employee_id not in(141,174)
2)不成對比較
SELECT  employee_id, manager_id, department_id
FROM    employees
WHERE   manager_id IN  (
			SELECT  manager_id
 
	                FROM    employees
	
                WHERE   employee_id IN (174,141)
			)
AND   department_id IN  (
			SELECT  department_id
                        FROM    employees
		        WHERE   employee_id IN (174,141)
			)
AND employee_id NOT IN(174,141);
注意主查詢的列 和 內查詢的列,一定要一一對應




在 FROM 子句中使用子查詢
問題:返回比本部門平均工資高的員工的last_name, department_id, salary及平均工資

1)已學知識解決:
select last_name,department_id,salary,
(select avg(salary)from employees e3 
where e1.department_id = e3.department_id 
group by department_id) avg_salary
from employees e1
where salary >     
	(select avg(salary)
          from employees e2
          where e1.department_id = e2.department_id
          group by department_id
          )
此方法進行了重複性操作

2)from字句
select employee_id , e1.department_id , salary ,e2.avg_sal
from employees e1 , (select department_id , avg(salary) avg_sal
                    from employees
                    group by department_id
                    ) e2 --以子查詢的形式,形成了一個新表
where e1.department_id = e2.department_id
and e1.salary > e2.avg_sal
單列子查詢應用舉例
在 CASE 表示式中使用單列子查詢
問題:顯式員工的employee_id,last_name和location。其中,若員工department_id與location_id為1800的department_id相同,則location為’Canada’,其餘則為’USA’。
/*select employee_id , last_name , (
                                 case department_id 
                                 when (select department_id
                                       from departments
                                       where location_id = 1800
                                       )
                                 then 'Canada' else 'Usa'
                                 end
                                 ) location
from employees
*/
SELECT employee_id, last_name,
       (CASE
        WHEN department_id =
                (select department_id 
                from departments
                where location_id = 1800
                 )
        THEN 'Canada' ELSE 'USA' END) location
FROM   employees
在 ORDER BY 子句中使用單列子查詢
問題:查詢員工的employee_id,last_name,要求按照員工的department_name排序
SELECT   employee_id, last_name
FROM     employees e
ORDER BY (SELECT department_name 
	FROM departments d 
	WHERE e.department_id = d.department_id
	);

三、相關子查詢
相關子查詢按照一行接一行的順序執行,主查詢的每一行都執行一次子查詢
外層的表在內層中使用,就是相關子查詢
SELECT column1, column2, ...
FROM   table1
 outer
WHERE  column1 operator
			(SELECT  colum1, column2
                      FROM    table2
                       WHERE   expr1 = outer.expr2);
子查詢中使用主查詢中的列
問題:若employees表中employee_id與job_history表中employee_id相同的數目不小於2,
輸出這些相同id的員工的employee_id,last_name和其job_id
select employee_id , last_name , job_id 
from employees e
where 2 <= (
               select count(*)
               from job_history j
               where e.employee_id = j.employee_id 
           )

EXISTS 操作符檢查在子查詢中是否存在滿足條件的行
如果在子查詢中存在滿足條件的行:
不在子查詢中繼續查詢,條件返回 TRUE
如果在子查詢中不存在滿足條件的行:
條件返回 FALSE,繼續在子查詢中查詢

問題:查詢公司管理者的employee_id,last_name,job_id,department_id資訊
/*
select employee_id , last_name , salary
from employees e1
where e1.employee_id in(
                   select manager_id
                   from employees e2
                   )
*/
/*
select distinct e1.employee_id , e1.last_name , e1.salary
from employees e1,employees e2
where e1.employee_id = e2.manager_id
*/
SELECT employee_id, last_name, job_id, department_id
FROM   employees outer
WHERE  EXISTS ( SELECT 'X'
                 FROM   employees
                 WHERE  manager_id = outer.employee_id     
		);
問題:查詢departments表中,不存在於employees表中的部門的department_id和department_name
/*
select d1.department_id
from departments d1
minus
SELECT department_id
 FROM   employees
*/
SELECT department_id, department_name
FROM departments d
WHERE NOT EXISTS (
		SELECT 'X'
                  FROM   employees
                  WHERE  department_id  = d.department_id
		);