19
15 Copyright © Oracle Corporation, 2001. All rights reserved. Using SET Operators

15 Copyright © Oracle Corporation, 2001. All rights reserved. Using SET Operators

Embed Size (px)

Citation preview

Page 1: 15 Copyright © Oracle Corporation, 2001. All rights reserved. Using SET Operators

15Copyright © Oracle Corporation, 2001. All rights reserved.

Using SET Operators

Page 2: 15 Copyright © Oracle Corporation, 2001. All rights reserved. Using SET Operators

15-2 Copyright © Oracle Corporation, 2001. All rights reserved.

Objectives

After completing this lesson, you should be able to do the following:

• Describe SET operators

• Use a SET operator to combine multiple queries into a single query

• Control the order of rows returned

Page 3: 15 Copyright © Oracle Corporation, 2001. All rights reserved. Using SET Operators

15-3 Copyright © Oracle Corporation, 2001. All rights reserved.

The SET OperatorsA B

UNION/UNION ALL

A B

A B

INTERSECT

A B

MINUS

Page 4: 15 Copyright © Oracle Corporation, 2001. All rights reserved. Using SET Operators

15-4 Copyright © Oracle Corporation, 2001. All rights reserved.

Tables Used in This Lesson

The tables used in this lesson are:

• EMPLOYEES: Provides details regarding allcurrent employees

• JOB_HISTORY: Records the details of the start date and end date of the former job, and the job identification number and department when an employee switches jobs

Page 5: 15 Copyright © Oracle Corporation, 2001. All rights reserved. Using SET Operators

15-7 Copyright © Oracle Corporation, 2001. All rights reserved.

The UNION Operator

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

A B

Page 6: 15 Copyright © Oracle Corporation, 2001. All rights reserved. Using SET Operators

15-8 Copyright © Oracle Corporation, 2001. All rights reserved.

Using the UNION Operator

Display the current and previous job details of allemployees. Display each employee only once.

SELECT employee_id, job_idFROM employeesUNIONSELECT employee_id, job_idFROM job_history;

Page 7: 15 Copyright © Oracle Corporation, 2001. All rights reserved. Using SET Operators

15-10 Copyright © Oracle Corporation, 2001. All rights reserved.

The UNION ALL Operator

A B

The The UNION ALLUNION ALL operator returns results from both operator returns results from both queries, including all duplications.queries, including all duplications.The The UNION ALLUNION ALL operator returns results from both operator returns results from both queries, including all duplications.queries, including all duplications.

Page 8: 15 Copyright © Oracle Corporation, 2001. All rights reserved. Using SET Operators

15-11 Copyright © Oracle Corporation, 2001. All rights reserved.

Using the UNION ALL Operator

Display the current and previous departments ofall employees.

SELECT employee_id, job_id, department_idFROM employeesUNION ALLSELECT employee_id, job_id, department_idFROM job_historyORDER BY employee_id;

Page 9: 15 Copyright © Oracle Corporation, 2001. All rights reserved. Using SET Operators

15-12 Copyright © Oracle Corporation, 2001. All rights reserved.

The INTERSECT Operator

A B

Page 10: 15 Copyright © Oracle Corporation, 2001. All rights reserved. Using SET Operators

15-13 Copyright © Oracle Corporation, 2001. All rights reserved.

Using the INTERSECT Operator

Display the employee IDs and job IDs of employees who currently have a job title that they held before beginning their tenure with the company.

SELECT employee_id, job_idFROM employeesINTERSECTSELECT employee_id, job_idFROM job_history;

Page 11: 15 Copyright © Oracle Corporation, 2001. All rights reserved. Using SET Operators

15-14 Copyright © Oracle Corporation, 2001. All rights reserved.

The MINUS Operator

A B

Page 12: 15 Copyright © Oracle Corporation, 2001. All rights reserved. Using SET Operators

15-15 Copyright © Oracle Corporation, 2001. All rights reserved.

The MINUS Operator

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

SELECT employee_id,job_idFROM employeesMINUSSELECT employee_id,job_idFROM job_history;

Page 13: 15 Copyright © Oracle Corporation, 2001. All rights reserved. Using SET Operators

15-16 Copyright © Oracle Corporation, 2001. All rights reserved.

SET Operator Guidelines

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

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

• The ORDER BY clause:

– Can appear only at the very end of the statement

– Will accept the column name, aliases from the first SELECT statement, or the positional notation

Page 14: 15 Copyright © Oracle Corporation, 2001. All rights reserved. Using SET Operators

15-17 Copyright © Oracle Corporation, 2001. All rights reserved.

The Oracle Server and SET Operators

• Duplicate rows are automatically eliminated except in UNION ALL.

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

• The output is sorted in ascending order by default except in UNION ALL.

Page 15: 15 Copyright © Oracle Corporation, 2001. All rights reserved. Using SET Operators

15-18 Copyright © Oracle Corporation, 2001. All rights reserved.

Matching the SELECT Statements

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

SELECT department_id, TO_NUMBER(null) location, hire_dateFROM employeesUNIONSELECT department_id, location_id, TO_DATE(null)FROM departments;

Page 16: 15 Copyright © Oracle Corporation, 2001. All rights reserved. Using SET Operators

15-19 Copyright © Oracle Corporation, 2001. All rights reserved.

Matching the SELECT Statement

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

SELECT employee_id, job_id,salaryFROM employeesUNIONSELECT employee_id, job_id,0FROM job_history;

Page 17: 15 Copyright © Oracle Corporation, 2001. All rights reserved. Using SET Operators

15-20 Copyright © Oracle Corporation, 2001. All rights reserved.

Controlling the Order of Rows

Produce an English sentence using twoUNION operators.

COLUMN a_dummy NOPRINTSELECT 'sing' AS "My dream", 3 a_dummyFROM dualUNIONSELECT 'I''d like to teach', 1FROM dualUNION SELECT 'the world to', 2FROM dualORDER BY 2;

Page 18: 15 Copyright © Oracle Corporation, 2001. All rights reserved. Using SET Operators

15-21 Copyright © Oracle Corporation, 2001. All rights reserved.

Summary

In this lesson, you should have learned how to:

• Use UNION to return all distinct rows

• Use UNION ALL to returns all rows, including duplicates

• Use INTERSECT to return all rows shared byboth queries

• Use MINUS to return all distinct rows selected by the first query but not by the second

• Use ORDER BY only at the very end ofthe statement

Page 19: 15 Copyright © Oracle Corporation, 2001. All rights reserved. Using SET Operators

15-22 Copyright © Oracle Corporation, 2001. All rights reserved.

Practice 15 Overview

This practice covers using the Oracle9i datetime functions.