Unit 1.7
Unit 1.7
Unit 1.7
Section - 3.6
Set Operators
Set operators combine the results of two or more queries into a single result.
Conditions Set operators will take two or more queries as input, which must be union-compatible.
2
Conditions to perform Set Operators
Conditions-1 Both queries should have same (equal) number of columns.
3
Set Operators [Exercise]
4
Union Operator
Symbol: U
Notation: Relation-1 (R1) U Relation-2 (R2) OR Algebra-1 U Algebra-2
Operation:
It displays all the tuples/records belonging to the first relation (left relation) or the second relation (right
relation) or both.
It also eliminates duplicate tuples (tuples present in both relations appear once).
Example Perform Union between Customer and Employee. Answer (Customer) U (Employee)
Customer Employee Output
Name Name Name
Raju Meet Manoj
Suresh Suresh Meet
Meet Manoj Raju
Suresh
Exercise Is there any difference in the output if we swap the tables in Union operator. (Employee) U (Customer).
5
Intersect/ Intersection Operator
Symbol: ∩
Notation: Relation-1 (R1) ∩ Relation-2 (R2) OR Algebra-1 ∩ Algebra-2
Operation:
It displays all the tuples/records belonging to both relations. OR
It displays all the tuples/records which are common from both relations.
Example Perform Intersection between Customer and Employee. Answer (Customer) ∩ (Employee)
Customer Employee Output
Name Name Name
Raju Meet Meet
Suresh Suresh Suresh
Meet Manoj
Exercise Is there any difference in the output if we swap the tables in Intersection. (Employee) ∩ (Customer).
6
Minus/ Set difference Operator
Symbol: −
Notation: Relation-1 (R1) − Relation-2 (R2) OR Algebra-1 − Algebra-2
Operation:
It displays all the tuples/records belonging to the first relation (left relation) but not in the second relation
(right relation).
Example Perform Set difference between Customer and Employee. Answer (Customer) − (Employee)
Customer Employee Output
Name Name Name
Raju Meet Raju
Suresh Suresh
Meet Manoj
Exercise Is there any difference in the output if we swap the tables in Set difference. (Employee) − (Customer).
7
Union Operators Example
8
Intersect/ Intersection Operators Example
9
Minus/ Set difference Operators Example
Example Display Name of person who are employee but not customer.
Customer Employee
ID Name Balance ID Name Salary
1 Raju 10000 2 Suresh 8000
2 Suresh 20000 3 Manoj 9000
10
Minus/ Set difference Operators Example
Example Display Name of person who are customer but not employee.
Customer Employee
ID Name Balance ID Name Salary
1 Raju 10000 2 Suresh 8000
2 Suresh 20000 3 Manoj 9000
11
Set Operators [Exercise]
Exercise What is the output of following relational algebra for the below mentioned tables:
Customer Employee
ID Name Balance ID Name Salary
1 Raju 10000 2 Suresh 8000
2 Suresh 20000 3 Manoj 9000
12
Set Operators [Exercise]
Exercise What is the output of following relational algebra for the below mentioned tables:
Customer Employee
ID Name Balance ID Name Salary
1 Raju 10000 2 Suresh 8000
2 Suresh 20000 3 Manoj 9000
13
Division Operator
Section - 3.7
Division Operator
Symbol: ÷ (Division)
Notation: Relation1 (R1) ÷ Relation2 (R2) OR Algebra1 ÷ Algebra2
Condition:
Attributes of relation2/algebra2 must be a proper subset of attributes of relation1/algebra1.
Operation:
The output of the division operator will have attributes =
All attributes of relation1 – All attributes of relation2
The output of the division operator will have tuples =
Tuples in relation1, which are associated with the all tuples of relation2.
15
Division Operator Example
Example Perform Division operation between Student and Subject. Answer (Student) ÷ (Subject)
16
Division Operator Example
A B1 B2 B3 B4
Sno PNo PNo PNo PNo PNo
S1 P1 P2 P2 P1 P2
S1 P2 P4 P2 P5
S1 P3 P4
S1 P4 Algebra (A) ÷ (B1) Algebra (A) ÷ (B2) Algebra (A) ÷ (B3) Algebra (A) ÷ (B4)
S2 P1
Output Output Output Output
S2 P2
SNo SNo SNo SNo
S3 P2
S1 S1 S1
S4 P2
S2 S4
S4 P4
S3
S5 P4
S4
17
Division Operator Example
Example List the name of students doing a project in all technologies.
Student Project
RNo Name Technology TID Technology
101 Raj .NET 1 .NET
101 Raj PHP 2 PHP
102 Meet .NET 3 Android
102 Meet PHP 4 iPhone
102 Meet iPhone
Answer ∏ Name, Technology (Student) ÷ ∏ Technology (Project)
102 Meet Android
103 Rohit Android Output
18
Rename Operator
Section - 3.8
Rename Operator
Symbol: ρ (Rho)
Notation: ρA (X1,X2….Xn) (Relation)
Operation:
The rename operation is used to rename the output relation.
The result of rename operator are also relations with new name.
How to use:
ρ x (E)
Returns a relation E under a new name X.
ρ A1, A2. …,An (E)
Returns a relation E with the attributes renamed to A1, A2, …., An.
ρ x(A1, A2. …,An) (E)
Returns a relation E under a new name X with the attributes renamed to A1, A2, …., An.
20
Rename Operator Example
Example Rename Example Rename attributes
table
Student Student
RNo Name CPI Rno Name CPI
101 Raj 8 101 Raj 8
102 Meet 9 102 Meet 9
103 Jay 7 103 Jay 7
Person Student
RNo Name CPI RollNo StudentName SPI
101 Raj 8 101 Raj 8
102 Meet 9 102 Meet 9
103 Jay 7 103 Jay 7
21
Rename Operator Example
Example Rename table and attributes both Example Rename particular attributes
Student Student
Rno Name CPI Rno Name CPI
101 Raj 8 101 Raj 8
102 Meet 9 102 Meet 9
103 Jay 7 103 Jay 7
Algebra ρPerson (RollNo, StudentName) (∏ RNo, Name (Student)) Algebra ρStudentName / Name (Student)
Person Student
RollNo StudentName Rno StudentName CPI
101 Raj 101 Raj 8
102 Meet 102 Meet 9
103 Jay 103 Jay 7
22
Aggregate Functions
Section - 3.9
Aggregate Functions
Symbol: g or G
Notation: g function-name(column), function-name(column), …, function-name(column) (Relation)
Operation:
It takes a more than one value as input and returns a single value as output (result).
Aggregate functions are:
Sum (It returns the sum (addition) of the values of a column.)
Max (It returns the maximum value for a column.)
Min (It returns the minimum value for a column.)
Avg (It returns the average of the values for a column.)
Count (It returns total number of values in a given column.)
24
Aggregate Functions Example
Student
Example Find out sum of CPI of all students. Output
Rno Name Branch Semester CPI
sum
101 Ramesh CE 3 9 Answer g sum(CPI) (Student)
73
102 Mahesh EC 3 8
103 Suresh ME 4 7 Example Find out maximum & minimum CPI. Output
104 Amit EE 4 8 max min
Answer g max(CPI), min(CPI) (Student)
105 Anita CE 4 8 9 7
25
Relational Algebra [Exercise]
Write down relational algebras for the following table:
▪ Employee (person-name, street, city)
▪ Works (person-name, company-name, salary)
▪ Company (company-name, city)
▪ Managers (person-name, manager-name)
Find the names of all employees who work for “TCS”.
Find the names and cities of residence of all employees who work for “Infosys”.
Find the names, street and city of residence of all employees who work for “ITC” and earn more than $10,000
per annum.
Find the names of all employees in this database who live in the same city as the company for which they
work.
Find the names of all employees working in “TCS” who earn more than 25000 and less than 40000.
Find the name of employee whose manager is “Ajay Patel” and salary is more than 50000.
Display the name of employee with street, city, company name, salary and manager name staying in “Rajkot”
and working in “Ahmedabad”.
Find maximum, minimum and average salary of all employee.
Find out the total number of employee.
26