Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Unit 1.7

Download as pdf or txt
Download as pdf or txt
You are on page 1of 26

Set Operators

Section - 3.6
Set Operators
 Set operators combine the results of two or more queries into a single result.

Three types of Set Operators


Sr. Set Operator Symbol
1 Union U
2 Intersect / Intersection ∩
3 Minus / Set difference −

Conditions Set operators will take two or more queries as input, which must be union-compatible.

• Both queries should have same (equal) number of columns


• Corresponding attributes should have the same data type or domain

2
Conditions to perform Set Operators
Conditions-1 Both queries should have same (equal) number of columns.

Student Faculty Student Faculty


RNo Name Dept SPI FId Name Dept RNo Name Dept FId Name Dept
101 Raj CE 8 101 Patel CE 101 Raj CE 101 Patel CE
102 Meet ME 9 102 Shah ME 102 Meet ME 102 Shah ME
103 Jay CE 9 103 Dave ME 103 Jay CE 103 Dave ME

Conditions-2 Corresponding attributes should have the same data type.

Student Faculty Student Faculty


RNo Name Dept SPI FId Name Dept Sub RNo Name Dept SPI FId Name Dept Exp
101 Raj CE 8 101 Patel CE DS 101 Raj CE 8 101 Patel CE 5
102 Meet ME 9 102 Shah ME DBMS 102 Meet ME 9 102 Shah ME 3
103 Jay CE 9 103 Dave ME DF 103 Jay CE 9 103 Dave ME 4

3
Set Operators [Exercise]

Exercise Check whether following tables are compatible or not:

• A: (First_name(char), Last_name(char), Date_of_Birth(date))


• B: (FName(char), LName(char), PhoneNumber(number))
Χ (Not compatible) Both tables have 3 attributes but third attributes datatype is different.

• A: (First_name(char), Last_name(char), Date_of_Birth(date))


• B: (FName(char), LName(char), DOB(date))
✓ (Compatible) Both tables have 3 attributes and of same data type.

• Person (PersonID, Name, Address, Hobby)


• Professor (ProfessorID, Name, OfficeAddress, Salary)
• (Not compatible) Both tables have 4 attributes but forth attributes datatype is different.
∏ Name, Address (Person) & ∏ Name, OfficeAddress (Professor)
• (Compatible) Both tables have 2 attributes and of same data type.

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

Example Display Name of person who are either employee or customer.


Customer Employee
ID Name Balance ID Name Salary
1 Raju 10000 2 Suresh 8000
2 Suresh 20000 3 Manoj 9000

Answer ∏ Name (Customer) U ∏ Name (Employee)


Output
Name
Manoj
Raju
Suresh

8
Intersect/ Intersection Operators Example

Example Display Name of person who are employee as well as customer.


Customer Employee
ID Name Balance ID Name Salary
1 Raju 10000 2 Suresh 8000
2 Suresh 20000 3 Manoj 9000

Answer ∏ Name (Customer) ∩ ∏ Name (Employee)


Output
Name
Suresh

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

Answer ∏ Name (Employee) − ∏ Name (Customer)


Output
Name
Manoj

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

Answer ∏ Name (Customer) − ∏ Name (Employee)


Output
Name
Raju

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

Algebra-1 ∏ ID, Name (Customer) U ∏ ID, Name (Employee)

Algebra-2 ∏ ID, Name, Balance (Customer) U ∏ ID, Name, Salary (Employee)

Algebra-3 ∏ ID, Name (Customer) ∩ ∏ ID, Name (Employee)

Algebra-4 ∏ ID, Name, Balance (Customer) ∩ ∏ ID, Name, Salary (Employee)

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

Algebra-1 ∏ ID, Name (Customer) − ∏ ID, Name (Employee)

Algebra-2 ∏ ID, Name, Balance (Customer) − ∏ ID, Name, Salary (Employee)

Algebra-3 ∏ ID, Name (Employee) − ∏ ID, Name (Customer)

Algebra-4 ∏ ID, Name, Balance (Employee) − ∏ ID, Name, Salary (Customer)

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)

Student Subject Output


Name Subject Subject Name
Raj DBMS DBMS Rohit
Raj DS DS Suresh
Meet DS DF
Meet DF
Rohit DBMS
Rohit DS
Rohit DF
Suresh DBMS
Suresh DF
Suresh DS

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

104 Suresh .NET Name


Meet
104 Suresh iPhone
104 Suresh Android

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

Algebra ρPerson (Student) Algebra ρ(RollNo, StudentName, SPI) (Student)

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

106 Reeta ME 3 7 Example Count the number of students. Output


107 Rohit EE 4 9 count
108 Chetan CE 3 8
Answer g count(Rno) (Student)
9
109 Rakesh CE 4 9
Example Find out average of CPI of all students. Output
avg
Answer g avg(CPI) (Student)
8.11

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

You might also like