Dbms 3
Dbms 3
Relational Query
Languages
Outline
Looping
• 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
Student Project
EnrollNo Name Branch Sem ProjectID Title EnrollNo
190540107001 Raju CE 3 101 Bank 190540107001
190540107002 Mitesh CE 3 102 College 190540107002
190540107003 Nilesh CE 3 103 School 190540107003
190540107004 Meet CE 3 104 Hospital 190540107001
Section - 3
Relational Algebra Operations
Operator Description
Selection Display particular rows/records/tuples from a relation
Projection Display particular columns from a relation
Cross Product Multiply each tuples of both relations
Combine data or records from two or more tables
1. Natural Join / Inner Join
Joins
2. Outer Join
1. Left Outer Join 2. Right Outer Join 3. Full Outer Join
Combine the results of two queries into a single result.
Set Operators
1. Union 2. Intersection 3. Minus / Set-difference
Division Divides one relation by another
Rename Rename a column or a table
Selection Operator
Section - 3.1
Selection Operator
Symbol: σ (Sigma)
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.
Projection Operator
Section - 3.2
Projection Operator
Symbol: ∏ (Pi)
Notation: ∏ attribute set (Relation)
Operation: Selects specified attributes of a relation.
It removes duplicate tuples (records) from the result.
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
Exercise
Write down the relational algebra for the student table. Student
Display RollNo, Name and SPI of all students. RollNo Name Branch SPI
Display Name and SPI of all students. 101 Raj CE 6
Display the Name of all students. 102 Meet ME 8
Display the Name of all branches.
103 Harsh EE 7
104 Punit CE 9
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
Combined Projection & Selection Operation
Example Display RollNo, Name & Branch of “ME” Branch students.
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
Combined Projection & Selection Operation
Example Display Name of students along with their Branch who belong to either “ME” Branch or “CI” Branch.
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
Exercise
Write down the relational algebra for the student table. Student
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.
Write down the relational algebra for the employee table. Employee
Display the Name of employee belong to “HR” Dept and having salary EmpID Name Dept Salary
more than 20000. 101 Nilesh Sales 10000
Display the Name of all “Admin” and “HR” Dept’s employee.
102 Mayur HR 25000
List the Name of employee with their Salary who belongs to “HR” or
“Admin” Dept having salary more than 15000. 103 Hardik HR 15000
Display the Name of employee along with their Dept name whose 104 Ajay Admin 20000
salary between 15000 and 30000.
Cartesian Product / Cross Product
Section - 3.3
Cartesian Product / Cross Product
Symbol: X (Cross)
Notation: Relation-1 (R1) X Relation-2 (R2) OR Algebra-1 X Algebra-2
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
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
Right Outer Join
Symbol:
Notation: Relation-1 (R1) Relation-2 (R2) OR Algebra-1 Algebra-2
Operation:
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 Perform Right Outer Join between Student and Result. Answer (Student) (Result)
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
Full Outer Join
Symbol:
Notation: Relation-1 (R1) Relation-2 (R2) OR Algebra-1 Algebra-2
Operation:
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 Perform Full Outer Join between Student and Result. Answer (Student) (Result)
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
Set Operators
Section - 3.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).
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).
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
Division Operator Example
Example List the name of students doing a project in all technologies.
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
Rename Operator Example
Example Rename table and attributes both Example Rename particular attributes
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
Rename Operator Example
Example Find out maximum CPI from student table. Step-1 ρA (Student) X ρB (Student)
Student Output-1
Rno Name CPI A.Rno A.Name A.CPI B.Rno B.Name B.CPI
101 Raj 8 101 Raj 8 101 Raj 8
102 Meet 9 101 Raj 8 102 Meet 9
103 Jay 7 101 Raj 8 103 Jay 7
102 Meet 9 101 Raj 8
Step-2 σA.CPI<B.CPI (ρA (Student) X ρB (Student))
102 Meet 9 102 Meet 9
Output-2 102 Meet 9 103 Jay 7
A.Rno A.Name A.CPI B.Rno B.Name B.CPI 103 Jay 7 101 Raj 8
101 Raj 8 102 Meet 9 103 Jay 7 102 Meet 9
103 Jay 7 101 Raj 8 103 Jay 7 103 Jay 7
103 Jay 7 102 Meet 9
Rename Operator Example
Example Find out maximum CPI from student table.
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
Rename Operator Example
Example Find out maximum CPI from student table.