Unit-3 Relational Query
Unit-3 Relational Query
Relational Query
Languages
Structure of
Relational Databases
Student Table
Columns (5)
Attributes:
RollNo Name Branch Semester SPI
Title of column
101 Raju CE 3 8
Primary Primary
Key Key
Parent Child
Table Table
Student Project
EnrollNo Name Branch Sem ProjectID Title EnrollNo
170540107001 Jatin CE 3 P01 Bank 170540107001
170540107002 Meet CE 3 P02 College 170540107002
170540107003 Ram CE 3 P03 School 170540107002
Relational Algebra
Operations
Basic Relational Algebra Operations
Operator
Selection Display particular rows/records/tuples from a relation
Projection Display particular columns from a relation
Cross Product Multiply each tuples of both relations
Division Divides one relation by another
Rename Rename a column or a table
Joins Combine data or records from two or more tables
1. Natural Join / Iner Join
2. Outer Join
1. Left Outer Join 2. Right Outer Join 3. Full Outer Join
Set Operators Combine the results of two queries into a single result
1. Union 2. Intersection 3. Minus / Set-difference
Relational Algebra
Operations
Selection Operator
Selection Operator
Symbol: (Sigma)
Notation: condition (Relation)
Operation: Selects tuples from a relation that satisfy a given
condition.
Operators: =, <>, <, >, <=, >=, Λ (AND), V (OR)
Selection Operator example [ ]
condition(Relation)
Student
RollNo Name Branch SPI
101 Raj CE 8
102 Meet ME 9
103 Harsh EE 8
104 Punit CE 9
Branch=‘CE’ (Student)
Output
RollNo Name Branch SPI
101 Raj CE 8
104 Punit CE 9
Selection Operator example [ ]
condition(Relation)
Student
RollNo Name Branch SPI
101 Raj CE 8
102 Meet ME 9
103 Harsh EE 8
104 Punit CE 9
Output
Name Branch SPI
Meet ME 9
Punit CE 9
Combined Projection & Selection Operation
Example: Display name, branch and SPI of students who
belongs to “CE” Branch and SPI is more than 8.
Student
RollNo Name Branch SPI
101 Raj CE 8
102 Meet ME 9
104 Punit CE 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.
Cartesian Product / Cross Product
Example:
Student Result
RollNo Name Branch Sem RollNo SPI BL Rank
101 Raj CE 3 101 8 1 2
102 Meet ME 5 103 9 0 1
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
• 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 and
SPI of all student belong to “CE” department.
• 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)
• Display the name of students with their project name whose
guide is “A. J. Shah”.
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
Relational Algebra
Operations
Set Operators
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
Student Project
Rno Name Technology TID Technology
101 Raj .NET 1 .NET
101 Raj iPhone 2 PHP
102 Meet .NET 3 iPhone
102 Meet PHP 4 Android
102 Meet iPhone
102 Meet Android
103 Vijay PHP Output
104 Jay .NET Name
104 Jay PHP Meet
104 Jay iPhone Jay
104 Jay Android
ρ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
Find out maximum CPI from student table.
101 Raj 8
102 Meet 9 ρA (Student) X ρB (Student)))
103 Suresh 7
A.CPI
8
7
Rename Operator example