博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Oracle_071_lesson_p16
阅读量:5954 次
发布时间:2019-06-19

本文共 3663 字,大约阅读时间需要 12 分钟。

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;

转载于:https://blog.51cto.com/3938853/2157280

你可能感兴趣的文章
Eclipse Java @Override 报错
查看>>
知道双字节码, 如何获取汉字 - 回复 "pinezhou" 的问题
查看>>
linux中cacti和nagios整合
查看>>
Parallels Desktop12推出 新增Parallels Toolbox
查看>>
Python高效编程技巧
查看>>
Kafka服务端脚本详解(1)一topics
查看>>
js中var self=this的解释
查看>>
js--字符串reverse
查看>>
面试题
查看>>
Facebook 接入之获取各个配置参数
查看>>
linux的日志服务器关于屏蔽一些关键字的方法
查看>>
事情的两面性
查看>>
只要会营销,shi都能卖出去?
查看>>
sed单行处理命令奇偶行输出
查看>>
走向DBA[MSSQL篇] 从SQL语句的角度 提高数据库的访问性能
查看>>
VC++深入详解学习笔记1
查看>>
安装配置discuz
查看>>
CentOS7 64位小型操作系统的安装
查看>>
线程互互斥锁
查看>>
KVM虚拟机&openVSwitch杂记(1)
查看>>