Node
Node
Chapter Six-
Relational Query
Languages
Information Technology
Jimma Institute of Technology
Outline
Looping
• Basic of Relational Algebra and calculus
• Fundamental Operators and Syntax
▪ Selection
▪ Projection
▪ Cross Product OR Cartesian Product
▪ Joins
▪ Set Operators
▪ Division
▪ Rename
▪ Aggregate Functions
Basic of Relational Query
Section - 1
Relational Query Languages
Example Display the detail of students belongs to “CE” Branch. Answer σBranch=‘CE’ (Student)
Student Output
RollNo Name Branch SPI RollNo Name Branch SPI
101 Raju CE 8 101 Raju CE 8
102 Mitesh ME 9 104 Meet CE 9
103 Nilesh CI 9
104 Meet CE 9
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
Student
RollNo Name Branch SPI
101 Raju CE 8
102 Mitesh ME 9
103 Nilesh CI 9
104 Meet CE 9
Student
RollNo Name Branch SPI
101 Raju CE 8
102 Mitesh ME 9
103 Nilesh CI 9
104 Meet CE 9
Employee
Write down the relational algebra for the employee table.
Display the detail of all employee. EmpID Name Dept Salary
Display the detail of employee whose Salary more than 10000. 101 Nilesh Sales 10000
Display the detail of employee belongs to “HR” Dept having Salary more 102 Mayur HR 25000
than 20000.
103 Hardik HR 15000
Display the detail of employee belongs to either “HR” or “Admin” Dept.
Display the detail of employee whose Salary between 10000 and 25000 104 Ajay Admin 20000
and belongs to “HR” Dept.
Example Display RollNo, Name and Branch of all students. Answer ∏ RollNo, Name, Branch (Student)
Student Output
RollNo Name Branch SPI RollNo Name Branch
101 Raju CE 8 101 Raju CE
102 Mitesh ME 9 102 Mitesh ME
103 Nilesh CI 9 103 Nilesh CI
104 Meet CE 9 104 Meet CE
Employee
Write down the relational algebra for the employee table.
Display EmpID with Name of all employee. EmpID Name Dept Salary
Display Name and Salary of all employee. 101 Nilesh Sales 10000
Display the Name of all employee. 102 Mayur HR 25000
Display the Name of all departments. 103 Hardik HR 15000
104 Ajay Admin 20000
Student
RollNo Name Branch SPI
101 Raju CE 8
102 Mitesh ME 9
103 Nilesh CI 9
104 Meet CE 7
Student
RollNo Name Branch SPI
101 Raju CE 8
102 Mitesh ME 9
103 Nilesh CI 9
104 Meet CE 7
Student
RollNo Name Branch SPI
101 Raju CE 8
102 Mitesh ME 9
103 Nilesh CI 9
104 Meet CE 7
Step-1 σBranch=‘CE’ Λ SPI>7 (Student) Answer ∏ 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
Student
RollNo Name Branch SPI
101 Raju CE 8
102 Mitesh ME 9
103 Nilesh CI 9
104 Meet CE 7
Step-1 σBranch=‘ME’ V Branch=‘CI’ (Student) Answer ∏ 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
Example Perform Cross Product between Student and Result. Answer (Student) X (Result)
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”.
Student Result
RollNo Name Branch RollNo SPI BL
101 Raj CE 101 8 1
102 Meet ME 103 9 0
Output
RollNo Name SPI
101 Raj 8
102 Meet NULL
Student Result
RollNo Name Branch RollNo SPI BL
101 Raj CE 101 8 1
102 Meet ME 103 9 0
Output
RollNo Name SPI
101 Raj 8
103 NULL 9
Student Result
RollNo Name Branch RollNo SPI BL
101 Raj CE 101 8 1
102 Meet ME 103 9 0
Output
RollNo Name SPI
101 Raj 8
102 Meet NULL
103 NULL 9
Chapter Six- Query Languages and Relational algebra 39
Relational Algebra Operations
Set Operators
Section - 2.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.
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).
Chapter Six- Query Languages and Relational algebra 45
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).
Chapter Six- Query Languages and Relational algebra 46
Union Operators Example
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
Customer Employee
ID Name Balance ID Name Dept Salary
1 Raju 10000 2 Suresh CE 8000
2 Suresh 20000 3 Manoj ME 9000
Customer Employee
ID Name Balance ID Name Dept Salary
1 Raju 10000 2 Suresh CE 8000
2 Suresh 20000 3 Manoj ME 9000
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
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
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
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
Thank
You
Information Technology
Jimma Institute of Technology