1、,高级子查询,目标,通过本章学习,您将可以: 书写多列子查询 子查询对空值的处理 在 FROM 子句中使用子查询 在SQL中使用单列子查询 相关子查询 书写相关子查询 使用子查询更新和删除数据 使用 EXISTS 和 NOT EXISTS 操作符 使用 WITH 子句,子查询,子查询是嵌套在 SQL 语句中的另一个SELECT 语句,SELECT . FROM . WHERE .,(SELECT .FROM .WHERE .),主查询,子查询,子查询,子查询 (内查询) 在主查询执行之前执行 主查询使用子查询的结果 (外查询),SELECT select_list FROM table WHE
2、RE expr operator (SELECT select_listFROM table);,SELECT last_name FROM employees WHERE salary (SELECT salaryFROM employeesWHERE employee_id = 149) ;,子查询应用举例,多列子查询,Main query,WHERE (MANAGER_ID, DEPARTMENT_ID) IN,Subquery,100 90 102 60 124 50,主查询与子查询返回的多个列进行比较,列比较,多列子查询中的比较分为两种:成对比较 不成对比较,成对比较举例,SELEC
3、T employee_id, manager_id, department_id FROM employees WHERE (manager_id, department_id) IN(SELECT manager_id, department_idFROM employeesWHERE employee_id IN (178,174) AND employee_id NOT IN (178,174);,不成对比较举例,SELECT employee_id, manager_id, department_id FROM employees WHERE manager_id IN (SELECT
4、 manager_idFROM employeesWHERE employee_id IN (174,141) AND department_id IN (SELECT department_idFROM employeesWHERE employee_id IN (174,141) AND employee_id NOT IN(174,141);,SELECT a.last_name, a.salary, a.department_id, b.salavg FROM employees a, (SELECT department_id, AVG(salary) salavgFROM empl
5、oyeesGROUP BY department_id) b WHERE a.department_id = b.department_id AND a.salary b.salavg;,在 FROM 子句中使用子查询,单列子查询表达式,单列子查询表达式是在一行中只返回一列的子查询 Oracle8i 只在下列情况下可以使用, 例如: SELECT 语句 (FROM 和 WHERE 子句) INSERT 语句中的VALUES列表中 Oracle9i中单列子查询表达式可在下列情况下使用: DECODE 和 CASE SELECT 中除 GROUP BY 子句以外的所有子句中,单列子查询应用举例,在
6、 CASE 表达式中使用单列子查询,SELECT employee_id, last_name,(CASEWHEN department_id =THEN Canada ELSE USA END) location FROM employees;,(SELECT department_id FROM departments WHERE location_id = 1800),在 ORDER BY 子句中使用单列子查询,SELECT employee_id, last_name FROM employees e ORDER BY,(SELECT department_nameFROM depar
7、tments dWHERE e.department_id = d.department_id);,相关子查询,相关子查询按照一行接一行的顺序执行,主查询的每一行都执行一次子查询,GET 从主查询中获取候选列,EXECUTE 子查询使用主查询的数据,USE 如果满足内查询的条件则返回该行,相关子查询,SELECT column1, column2, .FROM table1WHERE column1 operator (SELECT colum1, column2FROM table2WHERE expr1 = .expr2);,子查询中使用主查询中的列,outer,outer,SELECT
8、last_name, salary, department_id FROM employees outer WHERE salary ,相关子查询举例,(SELECT AVG(salary)FROM employeesWHERE department_id = outer.department_id) ;,相关子查询举例,SELECT e.employee_id, last_name,e.job_id FROM employees e WHERE 2 = (SELECT COUNT(*)FROM job_history WHERE employee_id = e.employee_id);,E
9、XISTS 操作符,EXISTS 操作符检查在子查询中是否存在满足条件的行 如果在子查询中存在满足条件的行: 不在子查询中继续查找 条件返回 TRUE 如果在子查询中不存在满足条件的行: 条件返回 FALSE 继续在子查询中查找,SELECT employee_id, last_name, job_id, department_id FROM employees outer WHERE EXISTS ( SELECT XFROM employeesWHERE manager_id = outer.employee_id);,EXISTS 操作符应用举例,SELECT department_id
10、, department_name FROM departments d WHERE NOT EXISTS (SELECT XFROM employeesWHERE department_id = d.department_id);,NOT EXISTS 操作符应用举例,相关更新,使用相关子查询依据一个表中的数据更新另一个表的数据,UPDATE table1 alias1 SET column = (SELECT expressionFROM table2 alias2WHERE alias1.column = alias2.column);,相关更新应用举例,ALTER TABLE empl
11、oyees ADD(department_name VARCHAR2(14);,UPDATE employees e SET department_name = (SELECT department_name FROM departments dWHERE e.department_id = d.department_id);,DELETE FROM table1 alias1WHERE column operator (SELECT expressionFROM table2 alias2WHERE alias1.column = alias2.column);,相关删除,使用相关子查询依据
12、一个表中的数据删除另一个表的数据,DELETE FROM employees E WHERE employee_id = (SELECT employee_idFROM emp_history WHERE employee_id = E.employee_id);,相关删除应用举例,WITH 子句,使用 WITH 子句, 可以避免在 SELECT 语句中重复书写相同的语句块 WITH 子句将该子句中的语句块执行一次 并存储到用户的临时表空间中 使用 WITH 子句可以提高查询效率,WITH 子句应用举例,WITH dept_costs AS (SELECT d.department_name,
13、 SUM(e.salary) AS dept_totalFROM employees e, departments dWHERE e.department_id = d.department_idGROUP BY d.department_name), avg_cost AS (SELECT SUM(dept_total)/COUNT(*) AS dept_avgFROM dept_costs) SELECT * FROM dept_costs WHERE dept_total (SELECT dept_avg FROM avg_cost) ORDER BY department_name;,总结,通过本章学习,您已经可以: 使用多列子查询 多列子查询的成对和非成对比较 单列子查询 相关子查询 EXISTS 和 NOT EXISTS操作符 相关更新和相关删除 WITH子句,Hidden Slide,Hidden Slide,Hidden Slide,Hidden Slide,Hidden Slide,