Extended Operators in Relational Algebra - GeeksforGeeks
Extended Operators in Relational Algebra
- Difficulty Level : Easy
- Last Updated : 28 Jun, 2021
Basic idea about relational model and basic operators in Relational Algebra:
Basic Operators in Relational Algebra
Extended operators are those operators which can be derived from basic operators.There are mainly three types of extended operators in Relational Algebra:
- Join
- Intersection
- Divide
The relations used to understand extended operators are STUDENT, STUDENT_SPORTS, ALL_SPORTS and EMPLOYEE which are shown in Table 1, Table 2, Table 3 and Table 4 respectively.
STUDENT
ROLL_NONAMEADDRESSPHONEAGE1RAMDELHI9455123451182RAMESHGURGAON9652431543183SUJITROHTAK9156253131204SURESHDELHI915676897118
Table 1
STUDENT_SPORTS
ROLL_NOSPORTS1Badminton2Cricket2Badminton4Badminton
Table 2
ALL_SPORTS
SPORTSBadmintonCricket
Table 3
EMPLOYEE
EMP_NONAMEADDRESSPHONEAGE1RAMDELHI9455123451185NARESHHISAR9782918192226SWETARANCHI9852617621214SURESHDELHI915676897118
Table 4
Intersection (∩): Intersection on two relations R1 and R2 can only be computed if R1 and R2 are union compatible (These two relation should have same number of attributes and corresponding attributes in two relations have same domain). Intersection operator when applied on two relations as R1∩R2 will give a relation with tuples which are in R1 as well as R2. Syntax:
Relation1 ∩ Relation2
Example: Find a person who is student as well as employee- STUDENT ∩ EMPLOYEE
In terms of basic operators (union and minus) :
STUDENT ∩ EMPLOYEE = STUDENT + EMPLOYEE - (STUDENT U EMPLOYEE)
RESULT:
ROLL_NONAMEADDRESSPHONEAGE1RAMDELHI9455123451184SURESHDELHI915676897118
Conditional Join(c): Conditional Join is used when you want to join two or more relation based on some conditions. Example: Select students whose ROLL_NO is greater than EMP_NO of employees
STUDENTc STUDENT.ROLL_NO>EMPLOYEE.EMP_NOEMPLOYEE
In terms of basic operators (cross product and selection) :
σ (STUDENT.ROLL_NO>EMPLOYEE.EMP_NO)(STUDENT×EMPLOYEE)
RESULT:
ROLL_NONAMEADDRESSPHONEAGEEMP_NONAMEADDRESSPHONEAGE2RAMESHGURGAON9652431543181RAMDELHI9455123451183SUJITROHTAK9156253131201RAMDELHI9455123451184SURESHDELHI9156768971181RAMDELHI945512345118
Equijoin(): Equijoin is a special case of conditional join where only equality condition holds between a pair of attributes. As values of two attributes will be equal in result of equijoin, only one attribute will be appeared in result.
Example:Select students whose ROLL_NO is equal to EMP_NO of employees
STUDENTSTUDENT.ROLL_NO=EMPLOYEE.EMP_NOEMPLOYEE
In terms of basic operators (cross product, selection and projection) :
∏(STUDENT.ROLL_NO, STUDENT.NAME, STUDENT.ADDRESS, STUDENT.PHONE, STUDENT.AGE EMPLOYEE.NAME, EMPLOYEE.ADDRESS, EMPLOYEE.PHONE, EMPLOYEE>AGE)(σ (STUDENT.ROLL_NO=EMPLOYEE.EMP_NO) (STUDENT×EMPLOYEE))
RESULT:
ROLL_NONAMEADDRESSPHONEAGENAMEADDRESSPHONEAGE1RAMDELHI945512345118RAMDELHI9455123451184SURESHDELHI915676897118SURESHDELHI915676897118
Natural Join(): It is a special case of equijoin in which equality condition hold on all attributes which have same name in relations R and S (relations on which join operation is applied). While applying natural join on two relations, there is no need to write equality condition explicitly. Natural Join will also return the similar attributes only once as their value will be same in resulting relation.
Example: Select students whose ROLL_NO is equal to ROLL_NO of STUDENT_SPORTS as:
STUDENTSTUDENT_SPORTS
In terms of basic operators (cross product, selection and projection) :
∏(STUDENT.ROLL_NO, STUDENT.NAME, STUDENT.ADDRESS, STUDENT.PHONE, STUDENT.AGE STUDENT_SPORTS.SPORTS)(σ (STUDENT.ROLL_NO=STUDENT_SPORTS.ROLL_NO) (STUDENT×STUDENT_SPORTS))
RESULT:
ROLL_NONAMEADDRESSPHONEAGESPORTS1RAMDELHI945512345118Badminton2RAMESHGURGAON965243154318Cricket2RAMESHGURGAON965243154318Badminton4SURESHDELHI915676897118Badminton
Natural Join is by default inner join because the tuples which does not satisfy the conditions of join does not appear in result set. e.g.; The tuple having ROLL_NO 3 in STUDENT does not match with any tuple in STUDENT_SPORTS, so it has not been a part of result set.
Left Outer Join(): When applying join on two relations R and S, some tuples of R or S does not appear in result set which does not satisfy the join conditions. But Left Outer Joins gives all tuples of R in the result set. The tuples of R which do not satisfy join condition will have values as NULL for attributes of S.
Example:Select students whose ROLL_NO is greater than EMP_NO of employees and details of other students as well
STUDENT⟕STUDENT.ROLL_NO>EMPLOYEE.EMP_NOEMPLOYEE
RESULT
ROLL_NONAMEADDRESSPHONEAGEEMP_NONAMEADDRESSPHONEAGE2RAMESHGURGAON9652431543181RAMDELHI9455123451183SUJITROHTAK9156253131201RAMDELHI9455123451184SURESHDELHI9156768971181RAMDELHI9455123451181RAMDELHI945512345118NULLNULLNULLNULLNULL
Right Outer Join(): When applying join on two relations R and S, some tuples of R or S does not appear in result set which does not satisfy the join conditions. But Right Outer Joins gives all tuples of S in the result set. The tuples of S which do not satisfy join condition will have values as NULL for attributes of R.
Example: Select students whose ROLL_NO is greater than EMP_NO of employees and details of other Employees as well
STUDENTSTUDENT.ROLL_NO>EMPLOYEE.EMP_NOEMPLOYEE
RESULT:
ROLL_NONAMEADDRESSPHONEAGEEMP_NONAMEADDRESSPHONEAGE2RAMESHGURGAON9652431543181RAMDELHI9455123451183SUJITROHTAK9156253131201RAMDELHI9455123451184SURESHDELHI9156768971181RAMDELHI945512345118NULLNULLNULLNULLNULL5NARESHHISAR978291819222NULLNULLNULLNULLNULL6SWETARANCHI985261762121NULLNULLNULLNULLNULL4SURESHDELHI915676897118
Full Outer Join(): When applying join on two relations R and S, some tuples of R or S does not appear in result set which does not satisfy the join conditions. But Full Outer Joins gives all tuples of S and all tuples of R in the result set. The tuples of S which do not satisfy join condition will have values as NULL for attributes of R and vice versa.
Example:Select students whose ROLL_NO is greater than EMP_NO of employees and details of other Employees as well and other Students as well
STUDENTSTUDENT.ROLL_NO>EMPLOYEE.EMP_NOEMPLOYEE
RESULT:
ROLL_NONAMEADDRESSPHONEAGEEMP_NONAMEADDRESSPHONEAGE2RAMESHGURGAON9652431543181RAMDELHI9455123451183SUJITROHTAK9156253131201RAMDELHI9455123451184SURESHDELHI9156768971181RAMDELHI945512345118NULLNULLNULLNULLNULL5NARESHHISAR978291819222NULLNULLNULLNULLNULL6SWETARANCHI985261762121NULLNULLNULLNULLNULL4SURESHDELHI9156768971181RAMDELHI945512345118NULLNULLNULLNULLNULL
Division Operator (÷): Division operator A÷B can be applied if and only if:
- Attributes of B is proper subset of Attributes of A.
- The relation returned by division operator will have attributes = (All attributes of A – All Attributes of B)
- The relation returned by division operator will return those tuples from relation A which are associated to every B’s tuple.
Consider the relation STUDENT_SPORTS and ALL_SPORTS given in Table 2 and Table 3 above.
To apply division operator as
STUDENT_SPORTS÷ ALL_SPORTS
- The operation is valid as attributes in ALL_SPORTS is a proper subset of attributes in STUDENT_SPORTS.
- The attributes in resulting relation will have attributes {ROLL_NO,SPORTS}-{SPORTS}=ROLL_NO
- The tuples in resulting relation will have those ROLL_NO which are associated with all B’s tuple {Badminton, Cricket}. ROLL_NO 1 and 4 are associated to Badminton only. ROLL_NO 2 is associated to all tuples of B. So the resulting relation will be:
ROLL_NO2