Oracle_071_lesson_p16

Retrieving Data by Using Subqueries 子查詢

you should be able to:
1、Write a multiple-column subquery 多列子查詢
2、Use scalar subqueries in SQL 標量子查詢
3、Solve problems with correlated subqueries 關聯子查詢 ,子查詢子句與主查詢相互利用
4、Use the EXISTS and NOT EXISTS operators
EXISTS 子句有1條及以上的值輸出,則主查詢執行。NOT EXISTS 無值則匹配輸出。
方法:先執行主查詢的結果,再找一行值帶入子查詢來理解子查詢的意思。
5、Use the WITH clause 類似視圖

SELECT department_name, city
FROM departments
NATURAL JOIN (SELECT l.location_id, l.city, l.country_id
FROM locations l
JOIN countries c
ON(l.country_id = c.country_id)
JOIN regions
USING(region_id)
WHERE region_name = 'Europe');

SELECT employee_id, manager_id, department_id
FROM employees
WHERE (manager_id, department_id) IN
(SELECT manager_id, department_id
FROM employees
WHERE employee_id IN (174, 199))
AND employee_id NOT IN (174,199);

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);

A scalar subquery expression is a subquery thatreturns exactly one column value from one row.
Scalar subqueries can be used in:
The condition and expression part of DECODE and CASE
All clauses of SELECT except GROUP BY
The SET clause and WHERE clause of an UPDATE statement

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;

select department_id, department_name,
(select count(*)
from employees e
where e.department_id = d.department_id) as emp_count
from departments d;

SELECT column1, column2, ...
FROM table1 outertable
WHERE column1 operator
(SELECT column1, column2
FROM table2
WHERE expr1 = outertable.expr2);

SELECT last_name, salary, department_id
FROM employees outer_table
WHERE salary > (SELECT AVG(salary)
FROM employees inner_table
WHERE inner_table.department_id =
outer_table.department_id);

SELECT department_id, employee_id, salary
FROM EMPLOYEES e
WHERE 1 =
(SELECT COUNT(DISTINCT salary)
FROM EMPLOYEES
WHERE e.department_id = department_id
AND e.salary <= salary)

The EXISTS operator tests for existence of rows in the results set of the subquery.
If a subquery row value is found:
The search does not continue in the inner query
The condition is flagged TRUE
If a subquery row value is not found:
The condition is flagged FALSE
The search continues in the inner query

SELECT employee_id, last_name, job_id, department_id
FROM employees outer
WHERE EXISTS ( SELECT NULL
FROM employees
WHERE manager_id =
outer.employee_id);

SELECT department_id, department_name
FROM departments d
WHERE NOT EXISTS (SELECT NULL
FROM employees
WHERE department_id = d.department_id);

Using the WITH clause, you can use the same query block in a SELECT statement when it occurs more than once within a complex query.
The WITH clause retrieves the results of a query block and stores it in the user’s temporary tablespace.
The WITH clause may improve performance.

WITH CNT_DEPT AS
(
SELECT department_id,
COUNT(1) NUM_EMP
FROM EMPLOYEES
GROUP BY department_id
)
SELECT employee_id,
SALARY/NUM_EMP
FROM EMPLOYEES E
JOIN CNT_DEPT C
ON (e.department_id = c.department_id);

WITH Reachable_From (Source, Destin, TotalFlightTime) AS
(
SELECT Source, Destin, Flight_time
FROM Flights
UNION ALL
SELECT incoming.Source, outgoing.Destin,
incoming.TotalFlightTime+outgoing.Flight_time
FROM Reachable_From incoming, Flights outgoing
WHERE incoming.Destin = outgoing.Source
)
SELECT Source, Destin, TotalFlightTime
FROM Reachable_From;

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