PracticalNo10: Use following Set operators to perform different
operations.
Step 1: Create Two Tables
First, create two simple tables, EMPLOYEES_A and EMPLOYEES_B, with
some common and some unique names.
CREATE TABLE EMPLOYEES_A (
EMPLOYEE_ID INT PRIMARY KEY,
FIRST_NAME VARCHAR2(50),
DEPARTMENT_ID NUMBER
);
CREATE TABLE EMPLOYEES_B (
EMPLOYEE_ID INT PRIMARY KEY,
FIRST_NAME VARCHAR2(50),
DEPARTMENT_ID NUMBER
);
Step 2: Insert Data into the Tables
INSERT INTO EMPLOYEES_A VALUES (1, 'Amit', 10);
INSERT INTO EMPLOYEES_A VALUES (2, 'Rohit', 20);
INSERT INTO EMPLOYEES_A VALUES (3, 'Neha', 30);
INSERT INTO EMPLOYEES_B VALUES (3, 'Neha', 30);
INSERT INTO EMPLOYEES_B VALUES (4, 'Pooja', 40);
INSERT INTO EMPLOYEES_B VALUES (5, 'Raj', 10);
Step 3: Apply Set Operators
Now, let's apply the set operators one by one.
1)UNION
1) Purpose: Combines the results from both tables and removes
duplicates.
2) Query:
SELECT FIRST_NAME FROM EMPLOYEES_A
UNION
SELECT FIRST_NAME FROM EMPLOYEES_B;
Result: Amit, Rohit, Neha, Pooja, Raj
Output:
UNION ALL
• Purpose: Combines the results from both tables, including
duplicates.
• Query:
SELECT FIRST_NAME FROM EMPLOYEES_A
UNION ALL
SELECT FIRST_NAME FROM EMPLOYEES_B;
Result: Amit, Rohit, Neha, Neha, Pooja, Raj
Output:
INTERSECT
• Purpose: Returns only the common names between both tables.
• Query:
SELECT FIRST_NAME FROM EMPLOYEES_A
INTERSECT
SELECT FIRST_NAME FROM EMPLOYEES_B;
• Result: Neha
Output:
MINUS
• Purpose: Returns names in the first table that are not in the second
table.
• Query:
SELECT FIRST_NAME FROM EMPLOYEES_A
MINUS
SELECT FIRST_NAME FROM EMPLOYEES_B;
Result: Amit, Rohit
Output:
• UNION: Combines and removes duplicates.
• UNION ALL: Combines all, including
duplicates.
• INTERSECT: Returns only common entries.
• MINUS: Returns entries in the first table not
found in the second.