Lesson 05 A
Lesson 05 A
1 of 20
Objectives
1 Describe Set Operators
2 of 20
Topics Covered
Set Operator Types and rules
UNION
UNION ALL
INTERSECT
MINUS
3 of 20
Types - Union
UNION
UNION of all
the rows in A
With ALL the
rows in B
With NO RESULT is the Yellow- but duplicates not showing twice
DUPLICATES
4 of 20
EXAMPLE:
JOB_HISTORY Table keeps history of when an employee changes jobs
Records start date and end date of employees that switch jobs
Employees who are still in the same job will not appear here
The current job is shown in the EMPLOYEE table. Again, this shows history.
10 rows selected
This example, only show the employee_id, job_id, department_id the common attributes.
5 of 20
Result of the UNION of both tables using just
employee_id and job_id
SELECT employee_id, job_id
FROM employee
UNION
SELECT employee_id, job_id
FROM job_history
ORDER BY job_id; added order by for readability
EMPLOYEE_ID JOB_ID
---------------------- ----------
100 AD_PRES
101 AC_ACCOUNT
101 AC_MGR
101 AD_VP
102 AD_VP
102 IT_PROG
103 IT_PROG
104 IT_PROG
107 IT_PROG
114 ST_CLERK
122 ST_CLERK
124 ST_MAN
141 ST_CLERK
142 ST_CLERK
143 ST_CLERK
144 ST_CLERK
149 SA_MAN
174 SA_REP
176 SA_MAN
176 SA_REP
178 SA_REP
200 AC_ACCOUNT
200 AD_ASST
201 MK_MAN
201 MK_REP
202 MK_REP
205 AC_MGR
206 AC_ACCOUNT
Employee 20
Job_history 10
TOTAL 30 rows
Since only produced 28 rows, then 2 rows must be duplicates and not shown.
UNION of ALL
the rows in A
and B
including
duplicates
ENTER the code using the same 2 tables to see the result.
RESULT is all
the Yellow
and duplicates
showing twice
7 of 20
SELECT employee_id, job_id
FROM employee
UNION ALL
SELECT employee_id, job_id
FROM job_history
ORDER BY employee_id;
EMPLOYEE_ID JOB_ID
---------------------- ----------
100 AD_PRES
101 AD_VP
101 AC_ACCOUNT
101 AC_MGR
102 IT_PROG
102 AD_VP
103 IT_PROG
104 IT_PROG
107 IT_PROG
114 ST_CLERK
122 ST_CLERK
124 ST_MAN
141 ST_CLERK
142 ST_CLERK
143 ST_CLERK
144 ST_CLERK
149 SA_MAN
174 SA_REP
176 SA_REP Was a Sales Representative
176 SA_MAN Became a Sales Manager
176 SA_REP Went back to a Sales Rep
178 SA_REP
200 AD_ASST Looks like the same here
200 AD_ASST
200 AC_ACCOUNT
201 MK_REP
201 MK_MAN
202 MK_REP
205 AC_MGR
206 AC_ACCOUNT
30 rows selected
8 of 20
Change the code and add in DEPARTMENT_ID
SELECT employee_id, job_id, department_id
FROM employee
UNION
SELECT employee_id, job_id, department_id
FROM job_history
ORDER BY employee_id;
Why?
ANSWER:
9 of 20
Types – Intersect
INTERSECT
10 of 20
The rows in common to both tables only
A intersect B
same as
B intersect A
11 of 20
Do the same but without department_id
SELECT employee_id, job_id
FROM employee
INTERSECT
SELECT employee_id, job_id
FROM job_history;
EMPLOYEE_ID JOB_ID
----------- ----------
176 SA_REP
200 AD_ASST
12 of 20
TITLES and ORDER BY
SQL Error: ORA-01789: query block has incorrect number of result columns
01789. 00000 - "query block has incorrect number of result columns"
13 of 20
Types – Minus
8-4
MINUS
Rows in the
first query A
That are not in
second query
B
PRECEDENCE – equal – evaluated left to right
14 of 20
Table A or employees has 20-0 means all 20 rows with no duplicates
The intersect of A and B is 2 rows duplicated result so far is 20 – 0 – 2 = 18
15 of 20
A bit more
Give a list of department_id, location_id, hire_date.
Using a JOIN
19 rows selected
16 of 20
SAME EXAMPLE but using UNION
Display department ID, location ID and hire date for all members
PROBLEM:
Need location_id from departments but it doesn't have a date to match with
17 of 20
SOLUTION
Because the expressions in the SELECT lists of the queries must match in number,
- use the dummy columns and the data type conversion functions to comply with this rule.
You must match the data type when columns do not exist in one or the other table
- use the TO_CHAR or any other conversion function to get the same data type
27 rows selected.
18 of 20
Matching SELECT statements
8-25
EXAMPLE 1:
Display all employees their job id and salary.
30 rows 20 + 10 - 0
19 of 20
Rules or Guidelines
8-5
- The data type of each column in the second query must match the data type
of its corresponding column in the first query.
- ORDER BY clause can appear only at the very end of the statement.
Other
• Column names from the first query are the ones that appear in the result.
20 of 20