Professional Documents
Culture Documents
EX.NO.7
USING THE SET OPERATORS
DATE:
Aim:
To Use a set operator to combine multiple queries into a single query and describe set operators
Procedure:
1. The UNION operator returns all rows selected by either query. Use the UNION operator to
return all rows from multiple tables and eliminate any duplicate rows.
2. To use the UNION ALL operator to return all rows from multiple queries. Unlike the case
with the UNION operator, duplicate rows are not eliminated and the output is not sorted by
default.
3. To use the INTERSECT operator to return all rows that are common to multiple queries.
4. To Use the MINUS operator to return rows returned by the first query that are not present in
the second query.
5. To use the ORDER BY clause only at the very end of the compound statement
717821f253 – S.siva
21ID05/21PD05/21DD05/21YD05/21SD05/21LE02/21EE02/21TE02//21FD05
Database Management Systems
Queries:
1. Write a query that displays the employee_id, job_id, and salary of ALL present and past
employees. If a salary is not found, then just display a 0 (zero) in its place.
Query: select employee_id,job_id,NVL(salary,0) from hr.employees union select '1',
employee_id ,job_id ,0 from hr.job_history
Output:
2. Display how many employees are in the employees table and not in job_history.
Query: select employee_id from hr.employees minus select employee_id from
hr.job_history
Output:
717821f253 – S.siva
21ID05/21PD05/21DD05/21YD05/21SD05/21LE02/21EE02/21TE02//21FD05
Database Management Systems
3. Display the employee IDs and job IDs of those employees who currently have a job
title that is the same as a previous job title
Query: select employee_id ,job_id from hr.employees intersect select employee_id ,job_id
from hr.job_history
Output:
4. Display the employee ID, job ID, and salary of all employees.
Query: select employee_id,job_id ,salary from hr.employees
Output:
717821f253 – S.siva
21ID05/21PD05/21DD05/21YD05/21SD05/21LE02/21EE02/21TE02//21FD05
Database Management Systems
5. Display the Department Number that do not contain the job id IT_PROG.
Query: select Department_id ,job_id from hr.employees minus select
department_id,job_id from hr.job_history where job_id ='IT-PROG'
Output:
6. The HR department needs a list of department IDs for departments that do not contain
the job ID ST_CLERK. Use set operators to create this report.
Query: select job_id ,department_id from hr.employees minus select job_id ,department_id from
hr.employees where job_id ='ST_CLERK'
Output:
717821f253 – S.siva
21ID05/21PD05/21DD05/21YD05/21SD05/21LE02/21EE02/21TE02//21FD05
Database Management Systems
8. Produce a list of jobs for departments 10, 50, and 20, in that order. Display the job ID
and department ID using set operators.
Query: select job_id ,department_id from hr.employees where department_id in(10) union
select job_id,department_id from hr.job_history where department_id in(50) union select
job_id,department_id from hr.job_history where department_id in(20)
Output:
717821f253 – S.siva
21ID05/21PD05/21DD05/21YD05/21SD05/21LE02/21EE02/21TE02//21FD05
Database Management Systems
9. Create a report that lists the employee ID and job ID of those employees who currently have a job
title that is the same as their job title when they were initially hired by the company (that is, they
changed jobs but have now gone back to doing their original job).
QUERY: select employee_id ,job_id from hr.employees intersect select
employee_id ,job_id from hr.job_history
OUTPUT:
10. The HR department needs a report with the following specifications: - The last name and department ID of
all employees from the EMPLOYEES table, regardless of whether or not they belong to a department - The
department ID and department name of all departments from the DEPARTMENTS table, regardless of
whether or not they have employees working in them Write a compound query to accomplish this.
QUERY: select last_name,department_id,to_char('null') from hr.employees union select
to_char('null'),department_id,department_name from hr.departments
OUTPUT:
717821f253 – S.siva
21ID05/21PD05/21DD05/21YD05/21SD05/21LE02/21EE02/21TE02//21FD05
Database Management Systems
RESULT:
Thus the queries using set operators had been completed successfully.
717821f253 – S.siva
21ID05/21PD05/21DD05/21YD05/21SD05/21LE02/21EE02/21TE02//21FD05
Database Management Systems
717821f253 – S.siva