lesson06 使用子查询

20
6-1 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved. Oracle OCP Oracle OCP Oracle OCP Oracle OCP 考试系列培训 1Z0-007 Lesson6 1Z0-007 Lesson6 1Z0-007 Lesson6 1Z0-007 Lesson6 www www www www . OracleOnLinux OracleOnLinux OracleOnLinux OracleOnLinux . cn cn cn cn

Upload: renguzi

Post on 11-Nov-2014

369 views

Category:

Technology


3 download

DESCRIPTION

Oracle OCP考试之007第6章,学会使用子查询

TRANSCRIPT

6-1 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.

Oracle OCPOracle OCPOracle OCPOracle OCP 考试系列培训之

1Z0-007 Lesson61Z0-007 Lesson61Z0-007 Lesson61Z0-007 Lesson6wwwwwwwwwwww....OracleOnLinuxOracleOnLinuxOracleOnLinuxOracleOnLinux....cncncncn

6666Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.

Using Subqueries to Solve QueriesUsing Subqueries to Solve QueriesUsing Subqueries to Solve QueriesUsing Subqueries to Solve Queries

6-3 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.

ObjectivesObjectivesObjectivesObjectives

After completing this lesson, you should be able to do After completing this lesson, you should be able to do After completing this lesson, you should be able to do After completing this lesson, you should be able to do the following:the following:the following:the following:• Define subqueries Define subqueries Define subqueries Define subqueries • Describe the types of problems that subqueries Describe the types of problems that subqueries Describe the types of problems that subqueries Describe the types of problems that subqueries

can solvecan solvecan solvecan solve• List the types of subqueriesList the types of subqueriesList the types of subqueriesList the types of subqueries• Write single-row and multiple-row subqueriesWrite single-row and multiple-row subqueriesWrite single-row and multiple-row subqueriesWrite single-row and multiple-row subqueries

6-4 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.

Using a SubqueryUsing a SubqueryUsing a SubqueryUsing a Subqueryto Solve a Problemto Solve a Problemto Solve a Problemto Solve a Problem

Who has a salary greater than AbelWho has a salary greater than AbelWho has a salary greater than AbelWho has a salary greater than Abel’’’’s?s?s?s?

Which employees have salaries greater Which employees have salaries greater Which employees have salaries greater Which employees have salaries greater than Abelthan Abelthan Abelthan Abel’’’’s salary?s salary?s salary?s salary?

Main query:Main query:Main query:Main query:

What is AbelWhat is AbelWhat is AbelWhat is Abel’’’’s salary?s salary?s salary?s salary?

Subquery:Subquery:Subquery:Subquery:

6-5 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.

• The subquery (inner query) executes once before The subquery (inner query) executes once before The subquery (inner query) executes once before The subquery (inner query) executes once before the main query (outer query).the main query (outer query).the main query (outer query).the main query (outer query).

• The result of the subquery is used by the main The result of the subquery is used by the main The result of the subquery is used by the main The result of the subquery is used by the main query.query.query.query.

SELECTSELECTSELECTSELECT select_listselect_listselect_listselect_listFROMFROMFROMFROM tabletabletabletableWHEREWHEREWHEREWHERE expr operatorexpr operatorexpr operatorexpr operator

(SELECT(SELECT(SELECT(SELECT select_listselect_listselect_listselect_list FROM FROM FROM FROM tabletabletabletable););););

Subquery SyntaxSubquery SyntaxSubquery SyntaxSubquery Syntax

6-6 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.

SELECT last_nameSELECT last_nameSELECT last_nameSELECT last_nameFROM employeesFROM employeesFROM employeesFROM employeesWHERE salary >WHERE salary >WHERE salary >WHERE salary > (SELECT salary (SELECT salary (SELECT salary (SELECT salary FROM employees FROM employees FROM employees FROM employees WHERE last_name = 'Abel'); WHERE last_name = 'Abel'); WHERE last_name = 'Abel'); WHERE last_name = 'Abel');

Using a SubqueryUsing a SubqueryUsing a SubqueryUsing a Subquery

11000110001100011000

6-7 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.

Guidelines for Using SubqueriesGuidelines for Using SubqueriesGuidelines for Using SubqueriesGuidelines for Using Subqueries

• Enclose subqueries in parentheses.Enclose subqueries in parentheses.Enclose subqueries in parentheses.Enclose subqueries in parentheses.• Place subqueries on the right side of the Place subqueries on the right side of the Place subqueries on the right side of the Place subqueries on the right side of the

comparison condition.comparison condition.comparison condition.comparison condition.• The The The The ORDER BYORDER BYORDER BYORDER BY clause in the subquery is not clause in the subquery is not clause in the subquery is not clause in the subquery is not

needed unless you are performing Top-N analysis.needed unless you are performing Top-N analysis.needed unless you are performing Top-N analysis.needed unless you are performing Top-N analysis.• Use single-row operators with single-row Use single-row operators with single-row Use single-row operators with single-row Use single-row operators with single-row

subqueries, and use multiple-row operators withsubqueries, and use multiple-row operators withsubqueries, and use multiple-row operators withsubqueries, and use multiple-row operators withmultiple-row subqueries.multiple-row subqueries.multiple-row subqueries.multiple-row subqueries.

6-8 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.

Types of SubqueriesTypes of SubqueriesTypes of SubqueriesTypes of Subqueries

• Single-row subquerySingle-row subquerySingle-row subquerySingle-row subquery

• Multiple-row subqueryMultiple-row subqueryMultiple-row subqueryMultiple-row subquery

Main queryMain queryMain queryMain query

SubquerySubquerySubquerySubquery returnsreturnsreturnsreturns

ST_CLERKST_CLERKST_CLERKST_CLERK

ST_CLERKST_CLERKST_CLERKST_CLERKSA_MANSA_MANSA_MANSA_MAN

Main queryMain queryMain queryMain query

SubquerySubquerySubquerySubquery returnsreturnsreturnsreturns

6-9 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.

Single-Row SubqueriesSingle-Row SubqueriesSingle-Row SubqueriesSingle-Row Subqueries

• Return only one rowReturn only one rowReturn only one rowReturn only one row• Use single-row comparison operatorsUse single-row comparison operatorsUse single-row comparison operatorsUse single-row comparison operators

Greater than or equal to Greater than or equal to Greater than or equal to Greater than or equal to >= >= >= >=

Less than Less than Less than Less than < < < <

Less than or equal toLess than or equal toLess than or equal toLess than or equal to <= <= <= <=

Equal toEqual toEqual toEqual to = = = =

Not equal toNot equal toNot equal toNot equal to <> <> <> <>

Greater than Greater than Greater than Greater than > > > >

MeaningMeaningMeaningMeaningOperatorOperatorOperatorOperator

6-10 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.

SELECT last_name, job_id, salarySELECT last_name, job_id, salarySELECT last_name, job_id, salarySELECT last_name, job_id, salaryFROM employeesFROM employeesFROM employeesFROM employeesWHERE job_id = WHERE job_id = WHERE job_id = WHERE job_id = (SELECT job_id (SELECT job_id (SELECT job_id (SELECT job_id FROM employees FROM employees FROM employees FROM employees WHERE employee_id = 141) WHERE employee_id = 141) WHERE employee_id = 141) WHERE employee_id = 141)AND salary >AND salary >AND salary >AND salary > (SELECT salary (SELECT salary (SELECT salary (SELECT salary FROM employees FROM employees FROM employees FROM employees WHERE employee_id = 143); WHERE employee_id = 143); WHERE employee_id = 143); WHERE employee_id = 143);

Executing Single-Row SubqueriesExecuting Single-Row SubqueriesExecuting Single-Row SubqueriesExecuting Single-Row Subqueries

ST_CLERKST_CLERKST_CLERKST_CLERK

2600260026002600

6-11 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.

SELECT last_name, job_id, salarySELECT last_name, job_id, salarySELECT last_name, job_id, salarySELECT last_name, job_id, salaryFROM employeesFROM employeesFROM employeesFROM employeesWHERE salary = WHERE salary = WHERE salary = WHERE salary = (SELECT MIN(salary) (SELECT MIN(salary) (SELECT MIN(salary) (SELECT MIN(salary) FROM employees); FROM employees); FROM employees); FROM employees);

Using Group Functions in a SubqueryUsing Group Functions in a SubqueryUsing Group Functions in a SubqueryUsing Group Functions in a Subquery

2222111100000000

6-12 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.

SELECT department_id, MIN(salary)SELECT department_id, MIN(salary)SELECT department_id, MIN(salary)SELECT department_id, MIN(salary)FROM employeesFROM employeesFROM employeesFROM employeesGROUP BY department_idGROUP BY department_idGROUP BY department_idGROUP BY department_idHAVING MIN(salary) >HAVING MIN(salary) >HAVING MIN(salary) >HAVING MIN(salary) > (SELECT MIN(salary) (SELECT MIN(salary) (SELECT MIN(salary) (SELECT MIN(salary) FROM employees FROM employees FROM employees FROM employees WHERE department_id = 50); WHERE department_id = 50); WHERE department_id = 50); WHERE department_id = 50);

The The The The HAVINGHAVINGHAVINGHAVING Clause with Subqueries Clause with Subqueries Clause with Subqueries Clause with Subqueries

• The Oracle server executes subqueries first.The Oracle server executes subqueries first.The Oracle server executes subqueries first.The Oracle server executes subqueries first.• The Oracle server returns results into the The Oracle server returns results into the The Oracle server returns results into the The Oracle server returns results into the HAVINGHAVINGHAVINGHAVING

clause of the main query.clause of the main query.clause of the main query.clause of the main query.

2222111100000000

6-13 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.

SELECT employee_id, last_nameSELECT employee_id, last_nameSELECT employee_id, last_nameSELECT employee_id, last_nameFROM employeesFROM employeesFROM employeesFROM employeesWHERE salary =WHERE salary =WHERE salary =WHERE salary = (SELECT MIN(salary) (SELECT MIN(salary) (SELECT MIN(salary) (SELECT MIN(salary) FROM employees FROM employees FROM employees FROM employees GROUP BY department_id); GROUP BY department_id); GROUP BY department_id); GROUP BY department_id);

What Is Wrong with This Statement?What Is Wrong with This Statement?What Is Wrong with This Statement?What Is Wrong with This Statement?

ERROR at line 4:ERROR at line 4:ERROR at line 4:ERROR at line 4:ORA-01427: single-row subquery returns more thanORA-01427: single-row subquery returns more thanORA-01427: single-row subquery returns more thanORA-01427: single-row subquery returns more thanone rowone rowone rowone row

Single-row operator with multiple-row subquerySingle-row operator with multiple-row subquerySingle-row operator with multiple-row subquerySingle-row operator with multiple-row subquery

6-14 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.

SELECT last_name, job_idSELECT last_name, job_idSELECT last_name, job_idSELECT last_name, job_idFROM employeesFROM employeesFROM employeesFROM employeesWHERE job_id =WHERE job_id =WHERE job_id =WHERE job_id = (SELECT job_id (SELECT job_id (SELECT job_id (SELECT job_id FROM employees FROM employees FROM employees FROM employees WHERE last_name = 'Haas'); WHERE last_name = 'Haas'); WHERE last_name = 'Haas'); WHERE last_name = 'Haas');

Will This Statement Return Rows?Will This Statement Return Rows?Will This Statement Return Rows?Will This Statement Return Rows?

no rows selectedno rows selectedno rows selectedno rows selected

Subquery returns no values.Subquery returns no values.Subquery returns no values.Subquery returns no values.

6-15 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.

Multiple-Row SubqueriesMultiple-Row SubqueriesMultiple-Row SubqueriesMultiple-Row Subqueries

• Return more than one rowReturn more than one rowReturn more than one rowReturn more than one row• Use multiple-row comparison operatorsUse multiple-row comparison operatorsUse multiple-row comparison operatorsUse multiple-row comparison operators

Compare value to every value returned by Compare value to every value returned by Compare value to every value returned by Compare value to every value returned by the subquery the subquery the subquery the subquery

ALLALLALLALL

Equal to any member in the listEqual to any member in the listEqual to any member in the listEqual to any member in the listININININ

Compare value to each value returned by the Compare value to each value returned by the Compare value to each value returned by the Compare value to each value returned by the subquery subquery subquery subquery

ANYANYANYANY

MeaningMeaningMeaningMeaningOperatorOperatorOperatorOperator

6-16 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.

SELECT employee_id, last_name, job_id, salarySELECT employee_id, last_name, job_id, salarySELECT employee_id, last_name, job_id, salarySELECT employee_id, last_name, job_id, salaryFROM employeesFROM employeesFROM employeesFROM employeesWHERE salary < ANYWHERE salary < ANYWHERE salary < ANYWHERE salary < ANY (SELECT salary (SELECT salary (SELECT salary (SELECT salary FROM employees FROM employees FROM employees FROM employees WHERE job_id = 'IT_PROG') WHERE job_id = 'IT_PROG') WHERE job_id = 'IT_PROG') WHERE job_id = 'IT_PROG')AND job_id <> 'IT_PROG';AND job_id <> 'IT_PROG';AND job_id <> 'IT_PROG';AND job_id <> 'IT_PROG';

Using the Using the Using the Using the ANYANYANYANY Operator Operator Operator Operator in Multiple-Row Subqueriesin Multiple-Row Subqueriesin Multiple-Row Subqueriesin Multiple-Row Subqueries

9000, 6000, 42009000, 6000, 42009000, 6000, 42009000, 6000, 4200

…………

6-17 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.

SELECT employee_id, last_name, job_id, salarySELECT employee_id, last_name, job_id, salarySELECT employee_id, last_name, job_id, salarySELECT employee_id, last_name, job_id, salaryFROM employeesFROM employeesFROM employeesFROM employeesWHERE salary < ALLWHERE salary < ALLWHERE salary < ALLWHERE salary < ALL (SELECT salary (SELECT salary (SELECT salary (SELECT salary FROM employees FROM employees FROM employees FROM employees WHERE job_id = 'IT_PROG') WHERE job_id = 'IT_PROG') WHERE job_id = 'IT_PROG') WHERE job_id = 'IT_PROG')AND job_id <> 'IT_PROG';AND job_id <> 'IT_PROG';AND job_id <> 'IT_PROG';AND job_id <> 'IT_PROG';

Using the Using the Using the Using the ALLALLALLALL Operator Operator Operator Operatorin Multiple-Row Subqueriesin Multiple-Row Subqueriesin Multiple-Row Subqueriesin Multiple-Row Subqueries

9000, 6000, 42009000, 6000, 42009000, 6000, 42009000, 6000, 4200

6-18 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.

SELECT emp.last_nameSELECT emp.last_nameSELECT emp.last_nameSELECT emp.last_nameFROM employees empFROM employees empFROM employees empFROM employees empWHERE emp.employee_id NOT INWHERE emp.employee_id NOT INWHERE emp.employee_id NOT INWHERE emp.employee_id NOT IN (SELECT mgr.manager_id (SELECT mgr.manager_id (SELECT mgr.manager_id (SELECT mgr.manager_id FROM employees mgr); FROM employees mgr); FROM employees mgr); FROM employees mgr);

no rows selectedno rows selectedno rows selectedno rows selected

Null Values in a SubqueryNull Values in a SubqueryNull Values in a SubqueryNull Values in a Subquery

SELECT emp.last_nameSELECT emp.last_nameSELECT emp.last_nameSELECT emp.last_nameFROM employees empFROM employees empFROM employees empFROM employees empWHERE emp.employee_id NOT INWHERE emp.employee_id NOT INWHERE emp.employee_id NOT INWHERE emp.employee_id NOT IN (SELECT mgr.manager_id (SELECT mgr.manager_id (SELECT mgr.manager_id (SELECT mgr.manager_id FROM employees mgr FROM employees mgr FROM employees mgr FROM employees mgr WHERE manager_id is not nullWHERE manager_id is not nullWHERE manager_id is not nullWHERE manager_id is not null););););

子查询中:凡是返回结果包含NULLNULLNULLNULL值,其结果一定为NULLNULLNULLNULL

6-19 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.

SELECTSELECTSELECTSELECT select_listselect_listselect_listselect_listFROMFROMFROMFROM tabletabletabletableWHEREWHEREWHEREWHERE expr operatorexpr operatorexpr operatorexpr operator

(SELECT (SELECT (SELECT (SELECT select_listselect_listselect_listselect_list FROM FROM FROM FROM tabletabletabletable););););

SummarySummarySummarySummary

In this lesson, you should have learned how to:In this lesson, you should have learned how to:In this lesson, you should have learned how to:In this lesson, you should have learned how to:• Identify when a subquery can help solve a Identify when a subquery can help solve a Identify when a subquery can help solve a Identify when a subquery can help solve a

questionquestionquestionquestion• Write subqueries when a query is based on Write subqueries when a query is based on Write subqueries when a query is based on Write subqueries when a query is based on

unknown valuesunknown valuesunknown valuesunknown values

6-20 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.

Practice 6: OverviewPractice 6: OverviewPractice 6: OverviewPractice 6: Overview

This practice covers the following topics:This practice covers the following topics:This practice covers the following topics:This practice covers the following topics:• Creating subqueries to query values based on Creating subqueries to query values based on Creating subqueries to query values based on Creating subqueries to query values based on

unknown criteriaunknown criteriaunknown criteriaunknown criteria• Using subqueries to find out which values exist in Using subqueries to find out which values exist in Using subqueries to find out which values exist in Using subqueries to find out which values exist in

one set of data and not in anotherone set of data and not in anotherone set of data and not in anotherone set of data and not in another

– P59 Q92P59 Q92P59 Q92P59 Q92– P61 Q98 ,P62 Q99P61 Q98 ,P62 Q99P61 Q98 ,P62 Q99P61 Q98 ,P62 Q99– P73 Q119P73 Q119P73 Q119P73 Q119,P91 Q151,P92 Q153P91 Q151,P92 Q153P91 Q151,P92 Q153P91 Q151,P92 Q153