DBMS Unit - 2 Relational - Algebra
DBMS Unit - 2 Relational - Algebra
Relational Model
RDBMS Architecture
How does a SQL engine work ?
Relational
SQL Optimized
Algebra Execution
Query RA Plan
(RA) Plan
Relational
SQL Optimized
Algebra Execution
Query RA Plan
(RA) Plan
Columns (5)
Branch=‘CE’ (Student)
Output
RollNo Name Branch SPI
101 Raj CE 8
104 Punit CE 9
Selection Operator example [ condition (Relation) ]
• Display the detail of students belongs to “CE” branch and having SPI
more than 8.
Student
RollNo Name Branch SPI
101 Raj CE 8
102 Meet ME 9
103 Harsh EE 8
104 Punit CE 9
Student
RollNo Name Branch SPI
101 Raj CE 8
102 Meet ME 9
103 Harsh EE 8
104 Punit CE 9
Employee
EmpID Name Dept Salary
101 Nilesh Sales 10000
102 Mayur HR 25000
103 Hardik HR 15000
104 Ajay Admin 20000
Student
RollNo Name Branch SPI
101 Raj CE 6
102 Meet ME 8
103 Harsh EE 7
104 Punit CE 9
Faculty
FacultyID Name Branch Salary
101 B. M. Patel CE 25000
102 A. M. Shah ME 35000
103 J. B. Katara EE 18000
104 H. N. Shukla CE 50000
105 V. K. Vyas ME 45000
1. Display the name of faculties belong to “CE” branch and having salary more than
25000.
2. Display the name of all “CE” and “ME” branch’s faulty.
3. List the name of faculty with their salary who belongs to “CE” or “ME” branch having
salary more than 35000.
4. Display the name of faculty along with their branch name whose salary between
25000 and 50000.
Cartesian Product / Cross Product
• Symbol: (Cross)
• Notation: Relation-1 (R1) Relation-2 (R2)
• Rare in practice; mainly used to express joins
• Operation: It will multiply each tuples of Relation-1 to each tuples of
Relation-2.
Attributes of Resultant Relation = Attributes of R1 + Attributes of R2
Tuples of Resultant Relation = Tuples of R1 * Tuples of R2
Cartesian Product / Cross Product
• Example:
Student Result
RollNo Name Branch RollNo SPI
101 Raj CE 101 8
102 Meet ME 103 9
Student Result
Student.RollNo Name Branch Result.RollNo SPI
101 Raj CE 101 8
101 Raj CE 103 9
102 Meet ME 101 8
102 Meet ME 103 9
If both relations have some attribute with the same name, it can be
distinguished by combing relation-name.attribute-name.
Natural Join (Inner Join)
• Symbol:
• Notation: Relation-1 (R1) Relation-2 (R2)
• Operation: Natural join will retrieve consistent data from multiple
relations.
• It combines records from different relations that satisfy a given
condition.
Steps
Step – 1 It performs Cartesian Product
Step – 2 Then it deletes inconsistent tuples
Step – 3 Then it removes an attribute from duplicate
attributes
Natural Join (Inner Join) example
Student Result
RollNo Name Branch RollNo SPI
101 Raj CE 101 8
102 Meet ME 103 9
i. List the name of students with their department name having backlog 0.
ii. List the name of faculties with their department name and salary having salary
more than 25000 and belongs to “CE” department.
iii. List the name of all faculties of “CE” and “ME” department whose salary is more
than 50000.
Outer Join
• Operation: In natural join some records are missing, if we want that
missing records than we have to use outer join.
• Types: Three types of Outer Join
1. Left Outer Join
2. Right Outer Join
3. Full Outer Join
Left Outer Join
• Display all the tuples of the left relation even through there is no
matching tuple in the right relation.
• For such kind of tuples having no matching, the attributes of right
relation will be padded with null in resultant relation.
• Example:
Student Result
RollNo Name Branch RollNo SPI
101 Raj CE 101 8
102 Meet ME 103 9
Student Result
RollNo Name Branch SPI
101 Raj CE 8
102 Meet ME null
Left Outer Join example
• Display Name, Salary and Balance of the of all employees.
Employee Customer
ID Name Salary ID Name Balance
2 Meet 15000 1 Raj 5000
3 Jay 20000 2 Meet 8000
Output
Name Salary Balance
Meet 15000 8000
Jay 20000 null
Right Outer Join
• Display all the tuples of right relation even through there is no matching
tuple in the left relation.
• For such kind of tuples having no matching, the attributes of left relation
will be padded with null in resultant relation.
• Example:
Student Result
RollNo Name Branch RollNo SPI
101 Raj CE 101 8
102 Meet ME 103 9
Student Result
RollNo Name Branch SPI
101 Raj CE 8
103 null null 9
Right Outer Join example
• Display Name, Salary and Balance of the of all customers.
Employee Customer
ID Name Salary ID Name Balance
2 Meet 15000 1 Raj 5000
3 Jay 20000 2 Meet 8000
Output
Name Salary Balance
Raj null 5000
Meet 15000 8000
Full Outer Join
• Display all the tuples of both of the relations. It also pads null values
whenever required. (Left outer join + Right outer join)
• For such kind of tuples having no matching, it will be padded with
null in resultant relation.
• Example: Student Result
RollNo Name Branch RollNo SPI
101 Raj CE 101 8
102 Meet ME 103 9
Student Result
RollNo Name Branch SPI
101 Raj CE 8
102 Meet ME null
103 null null 9
Full Outer Join example
• Display Name, Salary and Balance of the of all employee as well as
customers.
Employee Customer
ID Name Salary ID Name Balance
2 Meet 15000 1 Raj 5000
3 Jay 20000 2 Meet 8000
Output
Name Salary Balance
Meet 15000 8000
Jay 20000 null
Raj null 5000
Equi-join (A=B) Students(sid,sname,gpa)
People(ssn,pname,address)
• For whatever JOIN type (INNER, OUTER, etc), if we use ONLY the
equality operator (=), then we say that the JOIN is an EQUI JOIN
• A theta join where q is an equality SQL:
• R1 A=B R2 = s A=B (R1 R2) SELECT *
FROM
• Example: Students S,
• Employee SSN=SSN Dependents People P
WHERE sname = pname;
Most common
join in practice! RA:
𝑆 ⋈ 𝑠 𝑛𝑎𝑚𝑒=𝑝𝑛𝑎𝑚𝑒 𝑃
Theta Join (q) Students(sid,sname,gpa)
People(ssn,pname,address)
• This is same as EQUI JOIN but it allows all other operators like >,
<, >= etc.
• A join that involves a predicate
SQL:
• R1 q R2 = s q (R1 R2) SELECT *
• Here q can be any condition FROM
Students,People
WHERE q;
Note that natural join
is a theta join + a
projection. RA:
𝑆𝑡𝑢𝑑𝑒𝑛𝑡𝑠 ⋈𝜃 𝑃𝑒𝑜𝑝𝑙𝑒
Set Operators
• Set operators combine the results of two or more queries into a single
result.
• Types of set operators
1. Union
2. Intersect (Intersection)
3. Minus (Set Difference)
Union Operator
• Symbol: U
• Notation: Relation1 U Relation2
• Operation: Combine the records from two or more queries in to a
single result.
Employee Customer
ID Name Salary ID Name Balance
2 Meet 15000 1 Raj 5000
3 Jay 20000 2 Meet 8000
Employee Customer
ID Name Salary ID Name Balance
2 Meet 15000 1 Raj 5000
3 Jay 20000 2 Meet 8000
Employee Customer
ID Name Salary ID Name Balance
2 Meet 15000 1 Raj 5000
3 Jay 20000 2 Meet 8000
ρPerson (Student)
Person
Rno Name CPI
101 Raj 8
102 Meet 9
103 Suresh 7
Rename Operator example (Attribute)
Student
Rno Name CPI
101 Raj 8
102 Meet 9
103 Suresh 7
Student
RollNo StudentName CPI
101 Raj 8
102 Meet 9
103 Suresh 7
Rename Operator example (Attribute & Relation both)
Student
Rno Name CPI
101 Raj 8
102 Meet 9
103 Suresh 7
Person
RollNo StudentName
101 Raj
102 Meet
103 Suresh
Rename Operator example
Rno Name CPI
101 Raj 8
Find out maximum CPI from student table.
102 Meet 9 ρA (Student) X ρB (Student)))
103 Suresh 7
A.CPI
8
7
Rename Operator example
sum
• Example: Find out sum of CPI of all students.
73
• G sum(CPI) (Student)
Aggregate Functions example
Student
Rno Name Branch Semester CPI
101 Ramesh CE 3 9
102 Mahesh EC 3 8
103 Suresh ME 4 7
104 Amit EE 4 8
105 Anita CE 4 8
106 Reeta ME 3 7
107 Rohit EE 4 9
108 Chetan CE 3 8
109 Rakesh CE 4 9
max min
• Example: Find out maximum & minimum CPI.
9 7
• G max(CPI), min(CPI) (Student)
Aggregate Functions example
Student
Rno Name Branch Semester CPI
101 Ramesh CE 3 9
102 Mahesh EC 3 8
103 Suresh ME 4 7
104 Amit EE 4 8
105 Anita CE 4 8
106 Reeta ME 3 7
107 Rohit EE 4 9
108 Chetan CE 3 8
109 Rakesh CE 4 9
count
• Example: Count the number of students.
9
• G count(Rno) (Student)
Exercise