0% found this document useful (0 votes)
43 views5 pages

Using The Set Operators

The document discusses various SQL set operators - UNION, UNION ALL, INTERSECT, and MINUS. UNION returns distinct rows combining two queries, UNION ALL returns all rows including duplicates, INTERSECT returns rows common to both queries, and MINUS returns rows in the first query not in the second. Examples are provided for each operator on how to write SQL queries using them to combine or filter employee and job data from tables.

Uploaded by

Blue Devil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views5 pages

Using The Set Operators

The document discusses various SQL set operators - UNION, UNION ALL, INTERSECT, and MINUS. UNION returns distinct rows combining two queries, UNION ALL returns all rows including duplicates, INTERSECT returns rows common to both queries, and MINUS returns rows in the first query not in the second. Examples are provided for each operator on how to write SQL queries using them to combine or filter employee and job data from tables.

Uploaded by

Blue Devil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

USING THE SET OPERATORS

SET OPERATORS

UNION Operator
The UNION operator returns results from both queries after eliminating
duplications.
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;

UNION ALL Operator


The UNION ALL operator returns results from both queries, including all
duplications.
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;

INTERSECT Operator
The INTERSECT operator returns rows that are common to both queries.

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 job title when they were initially hired (that is, they
changed jobs but have now gone back to doing their original job).
MINUS Operator
The MINUS operator returns rows in the first query that are not present in the
second query.

Display the employee IDs of those employees who have not changed their jobs
even once.
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 ORDERBY 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.
The Oracle Server and Set Operators
•Duplicate rows are automatically eliminated except in UNIONALL.
•Column names from the first query appear in the result.
•The output is sorted in ascending order by default except in UNIONALL.

Matching the SELECT Statements


Using the UNION operator, display the department ID, location, and hire date for all
employees.
CONTROLLING THE ORDER OF ROWS
Produce an English sentence using two UNION operators.
COLUMN a_dummy NOPRINT
SELECT 'sing' AS "My dream", 3 a_dummy
FROM dual
UNION
SELECT 'I''d like to teach', 1 a_dummy
FROM dual
UNION
SELECT 'the world to', 2 a_dummy
FROM dual
ORDER BY a_dummy;

You might also like