Using The Set Operators
Using The Set Operators
Using The Set Operators
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
7-2
Set Operators
A
UNION/UNION ALL
A
INTERSECT
A
MINUS
7-3
7-4
UNION Operator
employee_id, job_id
employees
employee_id, job_id
job_history;
7-6
7-8
INTERSECT Operator
7 - 10
MINUS Operator
MINUS Operator
Display the employee IDs of those employees who have not
changed their jobs even once.
SELECT
FROM
MINUS
SELECT
FROM
employee_id
employees
employee_id
job_history;
7 - 12
7 - 13
7 - 14
QUIZ 2
Which of the following operators is used to return all rows from
multiple queries?
a.
b.
c.
UNION operator
UNION ALL operator
INTERSECT operator
Solution:
7 - 15
QUIZ 2 (Contd.)
Which of the following operators returns rows in the first query
that are not present in the second query?
a. MINUS operator
b. INTERSECT operator
Solution:
7 - 16
MINUS operator
7 - 17
7 - 18
employee_id, job_id,salary
employees
employee_id, job_id,0
job_history;
7 - 19
7 - 20
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
7 - 21