Relational Algebra
Relational Algebra
Relational Algebra
Relational
Query
Languages
Outline
• Structure of Relational Databases
• Key
• Relational Algebra
• Fundamental Operators and Syntax
Selection
Projection
Cross Product OR Cartesian Product
Joins
Set Operators
Division
Rename
Aggregate Functions
• Open Source and Commercial DBMS
Section - 1
Structure of Relational Databases
Columns (5) Table (Relation): A database object that holds
a collection of data for a specific topic.
Student Table consist of rows and columns.
Attributes: RollNo Name Branch Semester SPI
Title of column Column (Attribute): The vertical component of a
101 Raju CE 3 8
table. A column has a name and a particular data
Primary
Key
Candidate Key Candidate Key
EnrollNo (RollNo, Branch, Sem)
Primary Alternate
Key Key
Candidate Key Candidate Key
EnrollNo (RollNo, Branch, Sem)
10
11
13
Example Display the detail of students belongs to “CE” Branch and having SPI more than 8.
Student
RollNo Name Branch SPI
101 Raju CE 8
102 Mitesh ME 9
103 Nilesh CI 9
104 Meet CE 9
Answer
σBranch=‘CE’ Λ SPI>8 (Student)
Output
RollNo Name Branch SPI
104 Meet CE 9
Example Display the detail of students belongs to either “CI” or “ME” Branch.
Student
RollNo Name Branch SPI
101 Raju CE 8
102 Mitesh ME 9
103 Nilesh CI 9
104 Meet CE 9
Answer
σBranch=‘CI’ V Branch=‘ME’ (Student)
Output
RollNo Name Branch SPI
102 Mitesh ME 9
103 Nilesh CI 9
Answer
σSPI>7 Λ SPI<9 (Student)
Output
RollNo Name Branch SPI
101 Raju CE 8
Write down the relational algebra for the employee table. EmpID Name Dept Salary
Display the detail of all employee. 101 Nilesh Sales 10000
Display the detail of employee whose Salary more than 10000. 102 Mayur HR 25000
Display the detail of employee belongs to “HR” Dept having Salary more
103 Hardik HR 15000
than 20000.
Display the detail of employee belongs to either “HR” or “Admin” Dept. 104 Ajay Admin 20000
Display the detail of employee whose Salary between 10000 and 25000
and belongs to “HR” Dept.
21
Employee
Write down the relational algebra for the employee
table. EmpID Name Dept Salary
Display EmpID with Name of all employee. 101 Nilesh Sales 10000
Display Name and Salary of all employee. 102 Mayur HR 25000
Display the Name of all employee. 103 Hardik HR 15000
Display the Name of all departments.
104 Ajay Admin 20000
Step-1 Answer
σBranch=‘ME’ (Student) ∏ RollNo, Name, Branch (σBranch=‘ME’ (Student))
Output-1 Output-2
RollNo Name Branch SPI RollNo Name Branch
102 Mitesh ME 9 102 Mitesh ME
Student
RollNo Name Branch SPI
101 Raju CE 8
102 Mitesh ME 9
103 Nilesh CI 9
104 Meet CE 7
Step-1 Answer
σBranch=‘CE’ Λ SPI>7 (Student) ∏ Name, Branch, SPI (σBranch=‘CE’ Λ SPI>7 (Student))
Output-1 Output-2
RollNo Name Branch SPI Name Branch SPI
101 Raju CE 8 Raju CE 8
Step-1 Answer
σBranch=‘ME’ V Branch=‘CI’ (Student) ∏ Name, Branch (σBranch=‘ME’ V Branch=‘CI’ (Student))
Output-1 Output-2
RollNo Name Branch SPI Name Branch
102 Mitesh ME 9 Mitesh ME
103 Nilesh CI 9 Nilesh CI
Display Rollno, Name and SPI of all students belongs to “CE” Branch. RollNo Name Branch SPI
List the Name of students with their Branch whose SPI is more than 8 101 Raj CE 6
and belongs to “CE” Branch.
102 Meet ME 8
List the Name of students along with their Branch and SPI who belongs
to either “CE” or “ME” Branch and having SPI more than 8. 103 Harsh EE 7
Display the Name of students with their Branch name whose SPI 104 Punit CE 9
between 7 and 9.
Employee
Write down the relational algebra for the employee table. EmpID Name Dept Salary
Display the Name of employee belong to “HR” Dept and having salary 101 Nilesh Sales 10000
more than 20000.
102 Mayur HR 25000
Display the Name of all “Admin” and “HR” Dept’s employee.
List the Name of employee with their Salary who belongs to “HR” or 103 Hardik HR 15000
“Admin” Dept having salary more than 15000. 104 Ajay Admin 20000
Display the Name of employee along with their Dept name whose salary
between 15000 and 30000.
29
Example Perform Cross Product between Student and Result. Consider only selected attributes
• Student – RNo, Name and Branch
Student Result • Result – RNo, SPI and BL
RNo Name Branch Sem RNo SPI BL Rank
101 Raju CE 3 101 8 1 2
102 Mitesh ME 5 103 9 0 1
Answer
∏ RNo, Name, Branch (Student) X ∏ RNo, SPI, BL (Result)
Output
Student.RNo Name Branch Result.RNo SPI BL
101 Raju CE 101 8 1
101 Raju CE 103 9 0
102 Mitesh ME 101 8 1
102 Mitesh ME 103 9 0
30
Example Perform Cross Product between Student and Result. Consider only selected tuples
• Student – Branch=‘CE’ and Sem=3
Student Result • Result – SPI>7 and BL<1
RNo Name Branch Sem RNo SPI BL Rank
101 Raju CE 3 101 8 1 2
102 Mitesh ME 5 103 9 0 1
103 Om CE 3 105 7 2 3
104 Dhara CE 5
Answer
σBranch=‘CE’ Λ Sem=3 (Student) X σSPI>7 Λ BL<1 (Result)
Output
Student.RNo Name Branch Sem Result.RNo SPI BL Rank
101 Raju CE 3 103 9 0 1
103 OM CE 3 103 9 0 1
31
Example List the name of students with their department name and SPI of all student belong to “CE” department.
Example Display the name of students with their project name whose guide is “A. J. Shah”.
Relations
Student (Rno, Sname, Address, City, Mobile)
Department (Did, Dname)
Academic (Rno, Did, SPI, CPI, Backlog)
Guide (Rno, PName, Fid)
Faculty (Fid, Fname, Subject, Did, Salary)
List the name of students with their department name having backlog 0.
List the name of faculties with their department name and salary having salary more than 25000 and
belongs to “CE” department.
List the name of all faculties of “CE” and “ME” department whose salary is more than 50000.
Display the students name with their project name of all “CE” department’s students whose guide is “Z.Z.
Patel”.
Display the name of faculties with their department name who belongs to “CE” department and tough
“CPU” subject having salary more than 25000.
List the name of students with their department name doing project “Hackathon” under guide “I. I. Shah”.
39
Example Perform Left Outer Join between Student and Result. Answer (Student) (Result)
40
Example Perform Left Outer Join between Student and Result. (Display RollNo, Name and SPI)
Student Result
RollNo Name Branch RollNo SPI BL
101 Raj CE 101 8 1
102 Meet ME 103 9 0
Answer (Result))
∏ RollNo, Name, SPI ((Student)
Output
RollNo Name SPI
101 Raj 8
102 Meet NULL
Example Perform Right Outer Join between Student and Result. Answer (Student) (Result)
Example Perform Right Outer Join between Student and Result. (Display RollNo, Name and SPI)
Student Result
RollNo Name Branch RollNo SPI BL
101 Raj CE 101 8 1
102 Meet ME 103 9 0
Answer (Result))
∏ RollNo, Name, SPI ((Student)
Output
RollNo Name SPI
101 Raj 8
103 NULL 9
Example Perform Full Outer Join between Student and Result. Answer (Student) (Result)
Example Perform Full Outer Join between Student and Result. (Display RollNo, Name and SPI)
Student Result
RollNo Name Branch RollNo SPI BL
101 Raj CE 101 8 1
102 Meet ME 103 9 0
Conditions Set operators will take two or more queries as input, which must be union-compatible.
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).
50
Exercise Is there any difference in the output if we swap the tables in Intersection. (Employee) ∩ (Customer).
51
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).
Example Display Name of person who are employee but not customer.
Customer Employee
ID Name Balance ID Name Dept Salary
1 Raju 10000 2 Suresh CE 8000
2 Suresh 20000 3 Manoj ME 9000
Example Display Name of person who are customer but not employee.
Customer Employee
ID Name Balance ID Name Dept Salary
1 Raju 10000 2 Suresh CE 8000
2 Suresh 20000 3 Manoj ME 9000
Algebra-4
∏ ID, Name, Balance (Customer) ∩ ∏ ID, Name, Salary (Employee)
Algebra-4
∏ ID, Name, Balance (Employee) − ∏ ID, Name, Salary (Customer)
60
Raj DS DS Suresh
Meet DS DF
Meet DF
Rohit DBMS
Rohit DS
Rohit DF
Suresh DBMS
Suresh DF
Suresh DS
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 Outp Outp Outp
S2 P2 ut ut ut
SNo SN SN SN
S3 P2 o o o
S1 S1 S1
S4 P2
S2 S4
S4 P4
S3
S5 P4
S4
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
Algebra ρ Algebra ρ
Person (RollNo, StudentName) (∏ RNo, Name (Student)) 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
Output-2
A.Rno A.Name A.CPI B.Rno B.Name B.CPI
101 Raj 8 102 Meet 9
103 Jay 7 101 Raj 8
103 Jay 7 102 Meet 9
70
103 Suresh ME 4 7 Example Find out maximum & minimum CPI. Output
81