0% found this document useful (0 votes)
14 views4 pages

Unions Intersection Minus Assignment 5

Uploaded by

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

Unions Intersection Minus Assignment 5

Uploaded by

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

Unions, Intersection, Minus

1. UNION
• Definition: Combines the results of two SELECT statements, removing duplicate rows by
default.
• Key Points:

o Columns in both queries must have the same number and compatible data types.
o Use UNION ALL to include duplicates.

• Syntax:

SELECT column1, column2, ...


FROM table1
UNION
SELECT column1, column2, ...
FROM table2;

• Example:

[Assume 1st table name “Emplyees_2023” and 2nd “Employees_2024”]

SELECT emp_id, emp_name FROM Employees_2023


UNION
SELECT emp_id, emp_name FROM Employees_2024;

2. INTERSECTION

• Definition: Returns the common rows between two SELECT statements.

• Key Points:

o Columns in both queries must have the same number and compatible data types.
o Removes duplicates by default.
• Syntax:

SELECT column1, column2, ...


FROM table1
INTERSECT
SELECT column1, column2, ...
FROM table2;

• Example:

SELECT emp_id, emp_name FROM Employees_2023


INTERSECT
SELECT emp_id, emp_name FROM Employees_2024;

3. MINUS

• Definition: Returns the rows that are in the first SELECT statement but not in the second.

• Key Points:

o Columns in both queries must have the same number and compatible data types.
o Removes duplicates by default.

• Syntax:

SELECT column1, column2, ...


FROM table1
MINUS
SELECT column1, column2, ...
FROM table2;

• Example:

SELECT emp_id, emp_name FROM Employees_2023


MINUS
SELECT emp_id, emp_name FROM Employees_2024;
Q1. Create the following table “Employees_2023”

Column Name Datatype Size

emp_id (PRIMARY KEY) NUMBER 10


emp_name VARCHAR2 20
dept_id NUMBER 10

Q2. Insert the following data in the Employees_2023 table

emp_id emp_name dept_id


1 Alice 10
2 Bob 20
3 Charlie 30
4 David 40

Q3. Create the following table “Employees_2024”

Column Name Datatype Size

emp_id (PRIMARY KEY) NUMBER 10


emp_name VARCHAR2 20
dept_id NUMBER 10

Q4. Insert the following data in the Departments table

emp_id emp_name dept_id


2 Bob 20
3 Charlie 30
5 Eve 50
6 Frank 60
Q5. Based on above two tables answer the following Questionaries: -
I. Find all unique employees from both tables.
II. Find employees common to both tables.
III. Find employees who are in Employees_2023 but not in Employees_2024.
IV. List employees who belong to department 20 from either table.
V. Find employees in department 10 who are not in Employees_2024.
VI. List all employees sorted by emp_id.
VII. Find employees who are only in one table but not both.
VIII. Find employees whose emp_id is greater than 3 from both tables.
IX. Find employees who are in Employees_2023 but not in Employees_2024, and sort them by
dept_id.
X. Retrieve all employees from both tables, including duplicates.

You might also like