SlidePub
Home
Categories
Login
Register
Home
General
Les08 set operators by Szabist for the MS and MPM
Les08 set operators by Szabist for the MS and MPM
mayaali60
17 views
26 slides
Oct 20, 2024
Slide
1
of 26
Previous
Next
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
About This Presentation
Set operators related details provided
Size:
146.65 KB
Language:
en
Added:
Oct 20, 2024
Slides:
26 pages
Slide Content
Slide 1
8
Copyright © 2007, Oracle. All rights reserved.
Using the Set Operators
Slide 2
Copyright © 2007, Oracle. All rights reserved.8 - 2
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
Slide 3
Copyright © 2007, Oracle. All rights reserved.8 - 3
Lesson Agenda
•Set Operators: Types and guidelines
•Tables used in this lesson
•UNION and UNION ALL operator
•INTERSECT operator
•MINUS operator
•Matching the SELECT statements
•Using the ORDER BY clause in set operations
Slide 4
Copyright © 2007, Oracle. All rights reserved.8 - 4
Set Operators
UNION/UNION ALL
A B A B
A B
INTERSECT
A B
MINUS
Slide 5
Copyright © 2007, Oracle. All rights reserved.8 - 5
Set Operator Guidelines
•The expressions in the SELECT lists must match in number.
•The data type of each column in the second query must
match the data type of its corresponding column in the first
query.
•Parentheses can be used to alter the sequence of execution.
•ORDER BY clause can appear only at the very end of the
statement.
Slide 6
Copyright © 2007, Oracle. All rights reserved.8 - 6
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.
Slide 7
Copyright © 2007, Oracle. All rights reserved.8 - 7
Lesson Agenda
•Set Operators: Types and guidelines
•Tables used in this lesson
•UNION and UNION ALL operator
•INTERSECT operator
•MINUS operator
•Matching the SELECT statements
•Using the ORDER BY clause in set operations
Slide 8
Copyright © 2007, Oracle. All rights reserved.8 - 8
Tables Used in This Lesson
The tables used in this lesson are:
•EMPLOYEES: Provides details regarding all current
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
Slide 9
Copyright © 2007, Oracle. All rights reserved.8 - 12
Lesson Agenda
•Set Operators: Types and guidelines
•Tables used in this lesson
•UNION and UNION ALL operator
•INTERSECT operator
•MINUS operator
•Matching the SELECT statements
•Using the ORDER BY clause in set operations
Slide 10
Copyright © 2007, Oracle. All rights reserved.8 - 13
UNION Operator
A B
The UNION operator returns rows from both queries after eliminating
duplications.
Slide 11
Copyright © 2007, Oracle. All rights reserved.8 - 14
Using the UNION Operator
Display the current and previous job details of all employees.
Display each employee only once.
SELECT employee_id, job_id
FROM employees
UNION
SELECT employee_id, job_id
FROM job_history;
…
…
Slide 12
Copyright © 2007, Oracle. All rights reserved.8 - 16
UNION ALL Operator
The UNION ALL operator returns rows from both queries, including all
duplications.
A B
Slide 13
Copyright © 2007, Oracle. All rights reserved.8 - 17
Using the UNION ALL Operator
Display the current and previous departments of all employees.
SELECT employee_id, job_id, department_id
FROM employees
UNION ALL
SELECT employee_id, job_id, department_id
FROM job_history
ORDER BY employee_id;
…
…
Slide 14
Copyright © 2007, Oracle. All rights reserved.8 - 18
Lesson Agenda
•Set Operators: Types and guidelines
•Tables used in this lesson
•UNION and UNION ALL operator
•INTERSECT operator
•MINUS operator
•Matching the SELECT statements
•Using ORDER BY clause in set operations
Slide 15
Copyright © 2007, Oracle. All rights reserved.8 - 19
INTERSECT Operator
A B
The INTERSECT operator returns rows that are common to both queries.
Slide 16
Copyright © 2007, Oracle. All rights reserved.8 - 20
Using the INTERSECT Operator
Display the employee IDs and job IDs of those employees who
currently have a job title that is the same as their previous one
(that is, they changed jobs but have now gone back to doing
the same job they did previously).
SELECT employee_id, job_id
FROM employees
INTERSECT
SELECT employee_id, job_id
FROM job_history;
Slide 17
Copyright © 2007, Oracle. All rights reserved.8 - 21
Lesson Agenda
•Set Operators: Types and guidelines
•Tables used in this lesson
•UNION and UNION ALL operator
•INTERSECT operator
•MINUS operator
•Matching the SELECT statements
•Using the ORDER BY clause in set operations
Slide 18
Copyright © 2007, Oracle. All rights reserved.8 - 22
MINUS Operator
A B
The MINUS operator returns all the distinct rows selected by the first
query, but not present in the second query result set.
Slide 19
Copyright © 2007, Oracle. All rights reserved.8 - 23
Using the MINUS Operator
Display the employee IDs of those employees who have not
changed their jobs even once.
SELECT employee_id
FROM employees
MINUS
SELECT employee_id
FROM job_history;
…
Slide 20
Copyright © 2007, Oracle. All rights reserved.8 - 24
Lesson Agenda
•Set Operators: Types and guidelines
•Tables used in this lesson
•UNION and UNION ALL operator
•INTERSECT operator
•MINUS operator
•Matching the SELECT statements
•Using ORDER BY clause in set operations
Slide 21
Copyright © 2007, Oracle. All rights reserved.8 - 25
Matching the SELECT Statements
•Using the UNION operator, display the location ID,
department name, and the state where it is located.
•You must match the data type (using the TO_CHAR function
or any other conversion functions) when columns do not
exist in one or the other table.
SELECT location_id, department_name "Department",
TO_CHAR(NULL) "Warehouse location"
FROM departments
UNION
SELECT location_id, TO_CHAR(NULL) "Department",
state_province
FROM locations;
Slide 22
Copyright © 2007, Oracle. All rights reserved.8 - 26
Matching the SELECT Statement: Example
Using the UNION operator, display the employee ID, job ID, and
salary of all employees.
SELECT employee_id, job_id,salary
FROM employees
UNION
SELECT employee_id, job_id,0
FROM job_history;
…
Slide 23
Copyright © 2007, Oracle. All rights reserved.8 - 27
Lesson Agenda
•Set Operators: Types and guidelines
•Tables used in this lesson
•UNION and UNION ALL operator
•INTERSECT operator
•MINUS operator
•Match the SELECT statements
•Using the ORDER BY clause in set operations
Slide 24
Copyright © 2007, Oracle. All rights reserved.8 - 28
Using the ORDER BY Clause in Set Operations
•The ORDER BY clause can appear only once at the end of the
compound query.
•Component queries cannot have individual ORDER BY
clauses.
•ORDER BY clause recognizes only the columns of the first
SELECT query.
•By default, the first column of the first SELECT query is used
to sort the output in an ascending order.
Slide 25
Copyright © 2007, Oracle. All rights reserved.8 - 29
Summary
In this lesson, you should have learned how to use:
•UNION to return all distinct rows
•UNION ALL to return all rows, including duplicates
•INTERSECT to return all rows that are shared by both
queries
•MINUS to return all distinct rows that are selected by the first
query, but not by the second
•ORDER BY only at the very end of the statement
Slide 26
Copyright © 2007, Oracle. All rights reserved.8 - 30
Practice 8: Overview
In this practice, you create reports by using:
•The UNION operator
•The INTERSECTION operator
•The MINUS operator
Tags
Categories
General
Download
Download Slideshow
Get the original presentation file
Quick Actions
Embed
Share
Save
Print
Full
Report
Statistics
Views
17
Slides
26
Age
408 days
Related Slideshows
22
Pray For The Peace Of Jerusalem and You Will Prosper
RodolfoMoralesMarcuc
30 views
26
Don_t_Waste_Your_Life_God.....powerpoint
chalobrido8
32 views
31
VILLASUR_FACTORS_TO_CONSIDER_IN_PLATING_SALAD_10-13.pdf
JaiJai148317
30 views
14
Fertility awareness methods for women in the society
Isaiah47
29 views
35
Chapter 5 Arithmetic Functions Computer Organisation and Architecture
RitikSharma297999
26 views
5
syakira bhasa inggris (1) (1).pptx.......
ourcommunity56
28 views
View More in This Category
Embed Slideshow
Dimensions
Width (px)
Height (px)
Start Page
Which slide to start from (1-26)
Options
Auto-play slides
Show controls
Embed Code
Copy Code
Share Slideshow
Share on Social Media
Share on Facebook
Share on Twitter
Share on LinkedIn
Share via Email
Or copy link
Copy
Report Content
Reason for reporting
*
Select a reason...
Inappropriate content
Copyright violation
Spam or misleading
Offensive or hateful
Privacy violation
Other
Slide number
Leave blank if it applies to the entire slideshow
Additional details
*
Help us understand the problem better