Download - Lesson07

Transcript
Page 1: Lesson07

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

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

1Z0-007 Lesson71Z0-007 Lesson71Z0-007 Lesson71Z0-007 Lesson7wwwwwwwwwwww....OracleOnLinuxOracleOnLinuxOracleOnLinuxOracleOnLinux....cncncncn

Page 2: Lesson07

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

Using the Set OperatorsUsing the Set OperatorsUsing the Set OperatorsUsing the Set Operators

Page 3: Lesson07

7-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:• Describe set operatorsDescribe set operatorsDescribe set operatorsDescribe set operators• Use a set operator to combine multiple queries Use a set operator to combine multiple queries Use a set operator to combine multiple queries Use a set operator to combine multiple queries

into a single queryinto a single queryinto a single queryinto a single query• Control the order of rows returnedControl the order of rows returnedControl the order of rows returnedControl the order of rows returned

Page 4: Lesson07

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

Set OperatorsSet OperatorsSet OperatorsSet Operators

UNIONUNIONUNIONUNION////UNION ALLUNION ALLUNION ALLUNION ALL

AAAA BBBB AAAA BBBB

AAAA BBBB

INTERSECTINTERSECTINTERSECTINTERSECT

AAAA BBBB

MINUSMINUSMINUSMINUS

Page 5: Lesson07

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

Tables Used in This LessonTables Used in This LessonTables Used in This LessonTables Used in This Lesson

The tables used in this lesson are:The tables used in this lesson are:The tables used in this lesson are:The tables used in this lesson are:• EMPLOYEESEMPLOYEESEMPLOYEESEMPLOYEES: Provides details regarding all: Provides details regarding all: Provides details regarding all: Provides details regarding all

current employeescurrent employeescurrent employeescurrent employees• JOB_HISTORYJOB_HISTORYJOB_HISTORYJOB_HISTORY: Records the details of the start : Records the details of the start : Records the details of the start : Records the details of the start

date and end date of the former job, and the job date and end date of the former job, and the job date and end date of the former job, and the job date and end date of the former job, and the job identification number and department when an identification number and department when an identification number and department when an identification number and department when an employee switches jobsemployee switches jobsemployee switches jobsemployee switches jobs

Page 6: Lesson07

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

Page 7: Lesson07

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

Page 8: Lesson07

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

Page 9: Lesson07

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

UNIONUNIONUNIONUNION Operator Operator Operator Operator

AAAA BBBB

The The The The UNIONUNIONUNIONUNION operator returns results from both operator returns results from both operator returns results from both operator returns results from both queries after eliminating duplications.queries after eliminating duplications.queries after eliminating duplications.queries after eliminating duplications.

Page 10: Lesson07

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

Using the Using the Using the Using the UNIONUNIONUNIONUNION Operator Operator Operator Operator

Display the current and previous job details of all Display the current and previous job details of all Display the current and previous job details of all Display the current and previous job details of all employees. Display each employee only once.employees. Display each employee only once.employees. Display each employee only once.employees. Display each employee only once.SELECT employee_id, job_idSELECT employee_id, job_idSELECT employee_id, job_idSELECT employee_id, job_idFROM employeesFROM employeesFROM employeesFROM employeesUNIONUNIONUNIONUNIONSELECT employee_id, job_idSELECT employee_id, job_idSELECT employee_id, job_idSELECT employee_id, job_idFROM job_history;FROM job_history;FROM job_history;FROM job_history;

…………

…………

Page 11: Lesson07

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

Page 12: Lesson07

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

UNIONUNIONUNIONUNION ALLALLALLALL Operator Operator Operator Operator

AAAA BBBB

The The The The UNIONUNIONUNIONUNION ALLALLALLALL operator returns results from both operator returns results from both operator returns results from both operator returns results from both queries, including all duplications.queries, including all duplications.queries, including all duplications.queries, including all duplications.

Page 13: Lesson07

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

Using the Using the Using the Using the UNIONUNIONUNIONUNION ALLALLALLALL Operator Operator Operator Operator

Display the current and previous departments of all Display the current and previous departments of all Display the current and previous departments of all Display the current and previous departments of all employees.employees.employees.employees.SELECT employee_id, job_id, department_idSELECT employee_id, job_id, department_idSELECT employee_id, job_id, department_idSELECT employee_id, job_id, department_idFROM employeesFROM employeesFROM employeesFROM employeesUNION ALLUNION ALLUNION ALLUNION ALLSELECT employee_id, job_id, department_idSELECT employee_id, job_id, department_idSELECT employee_id, job_id, department_idSELECT employee_id, job_id, department_idFROM job_historyFROM job_historyFROM job_historyFROM job_historyORDER BY employee_id;ORDER BY employee_id;ORDER BY employee_id;ORDER BY employee_id;

…………

…………

Page 14: Lesson07

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

INTERSECTINTERSECTINTERSECTINTERSECT Operator Operator Operator Operator

AAAA BBBB

The The The The INTERSECTINTERSECTINTERSECTINTERSECT operator returns rows that are operator returns rows that are operator returns rows that are operator returns rows that are common to both queries.common to both queries.common to both queries.common to both queries.

Page 15: Lesson07

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

Using the Using the Using the Using the INTERSECTINTERSECTINTERSECTINTERSECT Operator Operator Operator Operator

Display the employee IDs and job IDs of those Display the employee IDs and job IDs of those Display the employee IDs and job IDs of those Display the employee IDs and job IDs of those employees who currently have a job title that is the employees who currently have a job title that is the employees who currently have a job title that is the employees who currently have a job title that is the same as their job title when they were initially hired same as their job title when they were initially hired same as their job title when they were initially hired same as their job title when they were initially hired (that is, they changed jobs but have now gone back to (that is, they changed jobs but have now gone back to (that is, they changed jobs but have now gone back to (that is, they changed jobs but have now gone back to doing their original job).doing their original job).doing their original job).doing their original job).

SELECT employee_id, job_idSELECT employee_id, job_idSELECT employee_id, job_idSELECT employee_id, job_idFROM employeesFROM employeesFROM employeesFROM employeesINTERSECTINTERSECTINTERSECTINTERSECTSELECT employee_id, job_idSELECT employee_id, job_idSELECT employee_id, job_idSELECT employee_id, job_idFROM job_history;FROM job_history;FROM job_history;FROM job_history;

Page 16: Lesson07

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

MINUSMINUSMINUSMINUS Operator Operator Operator Operator

AAAA BBBB

The The The The MINUSMINUSMINUSMINUS operator returns rows in the first query operator returns rows in the first query operator returns rows in the first query operator returns rows in the first query that are not present in the second query.that are not present in the second query.that are not present in the second query.that are not present in the second query.

Page 17: Lesson07

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

MINUSMINUSMINUSMINUS Operator Operator Operator Operator

Display the employee IDs of those employees who have not Display the employee IDs of those employees who have not Display the employee IDs of those employees who have not Display the employee IDs of those employees who have not changed their jobs even once.changed their jobs even once.changed their jobs even once.changed their jobs even once.

SELECT employee_id,job_idSELECT employee_id,job_idSELECT employee_id,job_idSELECT employee_id,job_idFROM employeesFROM employeesFROM employeesFROM employeesMINUSMINUSMINUSMINUSSELECT employee_id,job_idSELECT employee_id,job_idSELECT employee_id,job_idSELECT employee_id,job_idFROM job_history;FROM job_history;FROM job_history;FROM job_history;

…………

Page 18: Lesson07

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

Set Operator GuidelinesSet Operator GuidelinesSet Operator GuidelinesSet Operator Guidelines

• The expressions in the The expressions in the The expressions in the The expressions in the SELECTSELECTSELECTSELECT lists must match in lists must match in lists must match in lists must match in number and data type.number and data type.number and data type.number and data type.

• Parentheses can be used to alter the sequence of Parentheses can be used to alter the sequence of Parentheses can be used to alter the sequence of Parentheses can be used to alter the sequence of execution.execution.execution.execution.

• The The The The ORDERORDERORDERORDER BYBYBYBY clause: clause: clause: clause:– Can appear only at the very end of the statementCan appear only at the very end of the statementCan appear only at the very end of the statementCan appear only at the very end of the statement– Will accept the column name, aliases from the first Will accept the column name, aliases from the first Will accept the column name, aliases from the first Will accept the column name, aliases from the first

SELECTSELECTSELECTSELECT statement, or the positional notation statement, or the positional notation statement, or the positional notation statement, or the positional notation

Page 19: Lesson07

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

The Oracle Server and Set OperatorsThe Oracle Server and Set OperatorsThe Oracle Server and Set OperatorsThe Oracle Server and Set Operators

• Duplicate rows are automatically eliminated Duplicate rows are automatically eliminated Duplicate rows are automatically eliminated Duplicate rows are automatically eliminated except in except in except in except in UNIONUNIONUNIONUNION ALLALLALLALL....

• Column names from the first query appear in the Column names from the first query appear in the Column names from the first query appear in the Column names from the first query appear in the result.result.result.result.

• The output is sorted in ascending order by default The output is sorted in ascending order by default The output is sorted in ascending order by default The output is sorted in ascending order by default except in except in except in except in UNIONUNIONUNIONUNION ALLALLALLALL....

Page 20: Lesson07

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

Matching the Matching the Matching the Matching the SELECTSELECTSELECTSELECT Statements Statements Statements Statements

Using the Using the Using the Using the UNIONUNIONUNIONUNION operator, display the department ID, operator, display the department ID, operator, display the department ID, operator, display the department ID, location, and hire date for all employees.location, and hire date for all employees.location, and hire date for all employees.location, and hire date for all employees.

SELECT department_id, TO_NUMBER(null) SELECT department_id, TO_NUMBER(null) SELECT department_id, TO_NUMBER(null) SELECT department_id, TO_NUMBER(null) location, hire_date location, hire_date location, hire_date location, hire_dateFROM employeesFROM employeesFROM employeesFROM employeesUNIONUNIONUNIONUNIONSELECT department_id, location_id, TO_DATE(null)SELECT department_id, location_id, TO_DATE(null)SELECT department_id, location_id, TO_DATE(null)SELECT department_id, location_id, TO_DATE(null)FROM departments;FROM departments;FROM departments;FROM departments;

…………

Page 21: Lesson07

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

Matching the Matching the Matching the Matching the SELECTSELECTSELECTSELECT Statement: Statement: Statement: Statement:ExampleExampleExampleExample

Using the Using the Using the Using the UNIONUNIONUNIONUNION operator, display the employee ID, job operator, display the employee ID, job operator, display the employee ID, job operator, display the employee ID, job ID, and salary of all employees.ID, and salary of all employees.ID, and salary of all employees.ID, and salary of all employees.

SELECT employee_id, job_id,salarySELECT employee_id, job_id,salarySELECT employee_id, job_id,salarySELECT employee_id, job_id,salaryFROM employeesFROM employeesFROM employeesFROM employeesUNIONUNIONUNIONUNIONSELECT employee_id, job_id,0SELECT employee_id, job_id,0SELECT employee_id, job_id,0SELECT employee_id, job_id,0FROM job_history;FROM job_history;FROM job_history;FROM job_history;

…………

Page 22: Lesson07

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

Controlling the Order of RowsControlling the Order of RowsControlling the Order of RowsControlling the Order of Rows

Produce an English sentence using two Produce an English sentence using two Produce an English sentence using two Produce an English sentence using two UNION UNION UNION UNION operators.operators.operators.operators.COLUMN a_dummy NOPRINTCOLUMN a_dummy NOPRINTCOLUMN a_dummy NOPRINTCOLUMN a_dummy NOPRINTSELECT 'sing' AS "My dream", 3 a_dummySELECT 'sing' AS "My dream", 3 a_dummySELECT 'sing' AS "My dream", 3 a_dummySELECT 'sing' AS "My dream", 3 a_dummyFROM dualFROM dualFROM dualFROM dualUNIONUNIONUNIONUNIONSELECT 'I''d like to teach', 1 a_dummySELECT 'I''d like to teach', 1 a_dummySELECT 'I''d like to teach', 1 a_dummySELECT 'I''d like to teach', 1 a_dummyFROM dualFROM dualFROM dualFROM dualUNION UNION UNION UNION SELECT 'the world to', 2 a_dummySELECT 'the world to', 2 a_dummySELECT 'the world to', 2 a_dummySELECT 'the world to', 2 a_dummyFROM dualFROM dualFROM dualFROM dualORDER BY a_dummy;ORDER BY a_dummy;ORDER BY a_dummy;ORDER BY a_dummy;

Page 23: Lesson07

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

Page 24: Lesson07

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

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:• Use Use Use Use UNIONUNIONUNIONUNION to return all distinct rows to return all distinct rows to return all distinct rows to return all distinct rows• Use Use Use Use UNIONUNIONUNIONUNION ALLALLALLALL to return all rows, including to return all rows, including to return all rows, including to return all rows, including

duplicatesduplicatesduplicatesduplicates• Use Use Use Use INTERSECTINTERSECTINTERSECTINTERSECT to return all rows that are shared to return all rows that are shared to return all rows that are shared to return all rows that are shared

by both queriesby both queriesby both queriesby both queries• Use Use Use Use MINUSMINUSMINUSMINUS to return all distinct rows that are to return all distinct rows that are to return all distinct rows that are to return all distinct rows that are

selected by the first query but not by the secondselected by the first query but not by the secondselected by the first query but not by the secondselected by the first query but not by the second• Use Use Use Use ORDERORDERORDERORDER BYBYBYBY only at the very end of the only at the very end of the only at the very end of the only at the very end of the

statementstatementstatementstatement

Page 25: Lesson07

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

Practice 7: OverviewPractice 7: OverviewPractice 7: OverviewPractice 7: Overview

In this practice, you use the set operators to create In this practice, you use the set operators to create In this practice, you use the set operators to create In this practice, you use the set operators to create reports:reports:reports:reports:• Using the Using the Using the Using the UNIONUNIONUNIONUNION operator operator operator operator• Using the Using the Using the Using the INTERSECTIONINTERSECTIONINTERSECTIONINTERSECTION operatoroperatoroperatoroperator• Using the Using the Using the Using the MINUSMINUSMINUSMINUS operatoroperatoroperatoroperator

Page 26: Lesson07

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

Page 27: Lesson07

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


Top Related