0% found this document useful (0 votes)
73 views92 pages

5KS01 DBMS Unit II Relational Algebra SQL

Uploaded by

Anagha Badhe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
73 views92 pages

5KS01 DBMS Unit II Relational Algebra SQL

Uploaded by

Anagha Badhe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 92

Database Management Systems

(DBMS)
5KS01

Unit-II
Relational
Algebra , SQL

Mr. J. M. Patil
Department of Computer Science and Engineering
SSGMCE,Shegaon
[email protected]
9921860806
 Looping
Outline
• Structure of Relational Databases
• Key
• Why We Need Keys in DBMS
• Types of 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
Structure of Relational
Databases
Section - 1
Structure of Relational Databases
Columns Table (Relation): A database object
(5) that holds a collection of data for a
specific topic.
Stud Table consist of rows and columns.
Attributes:
ent
Roll Branc Semes SP
Title of Name Column (Attribute): The vertical
column No h ter I
component of a table. A column has a
101 Raju CE 3 8 name and a particular data type; e.g.
Mites varchar, decimal, integer, datetime etc.

Cardinality = No of
Rows or 102 CI 3 7 Record (Tuple): The horizontal
h
Tuples or component of a table, consisting of a
Mayu
Records 103 CE 3 6 sequence of values, one for each
r

tuples (7)
(7) column of the table. It is also known as
Niles row.
104 EE 3 9
h
A database consists of a collection of
Hites tables (relations), each having a unique
105 CI 3 7
h name.
Degree = No of
Domain is106a setTarun ME (5)unique
columns
of all possible 3 8
values for
a specific column.
Sures
107
Domain of Branch CE 3 9
h attribute is (CE, CI, ME, EE)
5KS01 (DBMS)  Unit II – Relational Algebra ,
Mr. Jaikumar M.Patil 4
Key
Section - 2
What is Key and Why We Need DBMS Keys?

 Key is an attribute or set of attribute which helps you to identify

uniquely rows of data or records in a table or relation.

Why We Need Keys in DBMS :-

 For identifying uniquely any row of data in a table.

 We can force identify of data and ensure integrity of data is

maintained.

 To establish relationship between tables and identifying relationship

between tables.
Mr. Jaikumar M.Patil
5KS01 (DBMS)  Unit II – Relational Algebra ,
6
Types of Key
Super Key

Candidate Key

Primary Key

Alternate Key

Foreign Key

Composite key or Compound Key

Unique Key

Surrogate Key
Super Key
 A super key is a combination of all possible attributes whose values
uniquely identifies each record within a relation (table).
 A super key may contain additional attributes that are not necessary for
unique identification.
 Super key is a superset of a candidate key.
 Every candidate key is a super key, but not every super key is a
Super Key Super Key
candidate key. Super Key
(RollNo, Branch, (SPI, Name,
 Table can have many super key.
EnrollNo
Sem) BL)

Roll Branc Se SP B
EnrollNo Name
No h m I L
190540107 101 CE 3 8 Raju 0
001
190540107 102 CE 3 7 Mites 1
002 h
190540106 101 CI 3 6 Mayu 2
001 r
5KS01 (DBMS)  Unit II – Relational Algebra ,
Mr. Jaikumar M.Patil 190540106 102 CI 3 9 Niles 0 8
Candidate Key
 A candidate key is a column, or set of columns, that can uniquely identify a
row in a table. There can be multiple candidate keys in a table.
 A candidate key is a minimal subset of a super key.
 If any proper subset of a super key is a super key then that key cannot be a
candidate key.
 Every candidate key is a super key but every super key is not a candidate
key. Candidate Candidate Key
 Candidate keys are Keynot allowed
(RollNo,
toBranch,
have null values.
EnrollNo Sem)

Roll Branc Se SP B
EnrollNo Name
No h m I L
190540107 101 CE 3 8 Raju 0
001
180540107 101 CE 5 7 Mites 1
002 h
190540106 101 CI 3 6 Mayu 2
Mr. Jaikumar M.Patil 001 5KS01 (DBMS)  r
Unit II – Relational Algebra ,
9
Primary Key
 A primary key is a column or a set of columns that uniquely identifies each row in a table.
Or candidate key that is chosen by database designer to identify tuples uniquely in
a relation (table).
 The value of primary key can never be NULL.
 There is only one primary key in the relation (table).
 The Primary
value of primary key must always be unique and can never be changed. No
Updations
Keyis possible.
Candidate Candidate Key
Key (RollNo, Branch,
EnrollNo Sem)

Roll Branc Se SP B
EnrollNo Name
No h m I L
190540107 101 CE 3 8 Raju 0
001
180540107 101 CE 5 7 Mites 1
002 h
190540106 101 CI 3 6 Mayu 2
Mr. Jaikumar M.Patil 001 5KS01 (DBMS)  r
Unit II – Relational Algebra ,
10
Primary Key rules
 A primary key may have one or more attributes.
 There is only one primary key in the relation (table).
 A primary key attribute value cannot be NULL.
 Generally, the value of a primary key attribute does not change.

5KS01 (DBMS)  Unit II – Relational Algebra ,


Mr. Jaikumar M.Patil 11
Alternate Key
 An alternate key is a candidate key that is not chosen by database designer to
identify tuples uniquely in a relation.
 Out of all candidate keys, only one gets selected as primary key, remaining keys are
known as alternate key.
 Unique but not selected as the primary key.
Primary Alternate
Key Key
Candidate Candidate Key
Key (RollNo, Branch,
EnrollNo Sem)

Roll Branc Se SP B
EnrollNo Name
No h m I L
190540107 101 CE 3 8 Raju 0
001
180540107 101 CE 5 7 Mites 1
002 h
190540106 101 CI 3 6 Mayu 2
Mr. Jaikumar M.Patil 001 5KS01 (DBMS)  r
Unit II – Relational Algebra ,
12
Foreign Key
 A foreign key is an attribute in a table is used to define its relationship with another
table.
 A foreign key is an attribute or collection of attributes in one table that refers to the
primary key in another table.
 Using foreign key helps in maintaining data integrity for tables in relationship.
 A table containing the foreign key is called the child table, and the table containing the
primary key is called the parent table.
Parent Child
Table Table

Stud Proje
ent Nam Branc Se ct
EnrollNo Projec
e h m Title EnrollNo
tID
190540107 Raju CE 3 101 Bank 190540107
001 001
190540107 Mites CE 3 102 College 190540107
002 h 002
190540107 Niles CE 3 103 School 190540107
5KS01 (DBMS)  Unit II – Relational Algebra ,
003
Mr. Jaikumar M.Patil h 003 13
Foreign Key rules
 A foreign key may refer the primary key of the table.
 Foreign key take only those value which are present in the primary key of
the referenced relation.
 Foreign key may have a name other than that of primary key.
 Foreign key can take the null value.
 In fact, foreign key is not unique in all the time.
 Referenced relation may also be called the Master table or Primary
table.
 Referencing relation may also be called the Foreign table.

5KS01 (DBMS)  Unit II – Relational Algebra ,


Mr. Jaikumar M.Patil 14
Composite Key
 A composite key is a key that consists of two or more columns
(attributes) that together uniquely identify a row in a table.
 Used when a single column is not sufficient to uniquely identify a
row.
 In the below example (SID,REG_ID),(REG_ID,EMAIL),(EMAIL,SID),
(SID,REG_ID,EMAIL) etc. all
SI RED_I NAM are Composite key.
BRAN
EMAIL
D D E CH
1 CS- John CS [email protected]
2019
2 CS- Adam CS [email protected]
2018
3 IT- Adam IT [email protected]
2019
4 CE- Denni CE [email protected]
2020 s
5 IT- James IT jamesgos@rediffmail
2021 .com
5KS01 (DBMS)  Unit II – Relational Algebra ,
Mr. Jaikumar M.Patil 15
Compound Key
 If a composite key has at least one attribute which is a foreign key then it is
called as Compound key.
 In the below example if we have a composite key
(REG_ID,BRANCH_CODE) then it will be known as a Compound key
because BRANCH_CODE attribute is foreign key.
SI RED_I NAM BRANCH_C BRANCH_C
Student EMAIL
Table BRANCH_NAME
D D E ODE ODE
1 CS- Branch
John TableCS [email protected] CS [email protected]
2019
CS [email protected]
2 CS- Adam CS [email protected]
2018 IT [email protected]
3 IT- Adam IT [email protected] CE [email protected]
2019
IT jamesgos@rediffmail
4 CE- Denni CE [email protected] .com
2020 s
5 IT- James IT jamesgos@rediffmail
2021 .com
5KS01 (DBMS)  Unit II – Relational Algebra ,
Mr. Jaikumar M.Patil 16
Unique Key

 A unique key is similar to a primary key, but it allows one ‘NULL’ value
in the column..
 Ensures that all values in a column are unique.
 Can have NULL Value.

5KS01 (DBMS)  Unit II – Relational Algebra ,


Mr. Jaikumar M.Patil 17
Surrogate Key
 If a relation has no attribute which can be used to identify the data stored in
it , then we create an attribute for this purpose.
 In this situation there is No natural primary key then, we can create an
attribute to act as an primary key.
 It adds no meaning to the data but serves the sole purpose of identifying
rows uniquely in a table.

5KS01 (DBMS)  Unit II – Relational Algebra ,


Mr. Jaikumar M.Patil 18
Relational Algebra
Operations
Section - 3
Relational Algebra Operations
Operator Description
Selection Display or returns particular rows/records/tuples from a relation
Projection Display or returns 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

5KS01 (DBMS)  Unit II – Relational Algebra ,


Mr. Jaikumar M.Patil 20
Relational Algebra
Operations
Selection Operator
Section - 3.1
Selection Operator
 Symbol: σ (Sigma)
 Notation: σ (Relation)
condition
 Operation: Selects tuples from a relation that satisfy a given
condition.
 Operators: =, <>, <, >, <=, >=, Λ (AND), V (OR)
Exam σ
Display the detail of students belongs to Answ Branch=‘CE’
ple “CE” Branch. er (Student)
Stud Outp
ent
Roll Nam Branc SP ut
Roll Nam Branc SP
No e h I No e h I
101 Raju CE 8 101 Raju CE 8
102 Mites ME 9 104 Meet CE 9
h
103 Niles CI 9
h
104Mr. Jaikumar
MeetM.Patil
CE 9 5KS01 (DBMS)  Unit II – Relational Algebra ,
22
Selection Operator [σ condition (Relation)]

Exam Display the detail of students belongs to “CE” Branch and


ple having SPI more than 8.
Stud
ent
Roll Nam Branc SP
No e h I
101 Raju CE 8
102 Mites ME 9
h
103 Niles CI 9
h

Answ σBranch=‘CE’ Λ SPI>8


104 Meet CE 9

er (Student)
Outp
ut
Roll Nam Branc SP
No e h I
104 Meet CE 9
5KS01 (DBMS)  Unit II – Relational Algebra ,
Mr. Jaikumar M.Patil 23
Selection Operator [σ condition (Relation)]
Exam Display the detail of students belongs to either “CI”
ple or “ME” Branch.
Stud
ent
Roll Nam Branc SP
No e h I
101 Raju CE 8
102 Mites ME 9
h
103 Niles CI 9
h
104
Answ Meet σBranch=‘CI’
CE 9
V Branch=‘ME’
er (Student)
Outp
ut
Roll Nam Branc SP
No e h I
102 Mites ME 9
h
103Mr. Jaikumar
NilesM.Patil
CI 9 5KS01 (DBMS)  Unit II – Relational Algebra ,
24
Selection Operator [σ condition (Relation)]
Exam Display the detail of students whose SPI
ple between 7 and 9.
Stud
ent
Roll Nam Branc SP
No e h I
101 Raju CE 8
102 Mites ME 9
h
103 Niles CI 9
h
104
Answ Meet σSPI>7CEΛ SPI<99
er (Student)
Outp
ut
Roll Nam Branc SP
No e h I
101 Raju CE 8

5KS01 (DBMS)  Unit II – Relational Algebra ,


Mr. Jaikumar M.Patil 25
Exercise
 Write down the relational algebra for the student Stud
ent
table. Roll Nam Branc SP
No e h I
 Display the detail of students whose RollNo is less than
104. 101 Raj CE 6
 Display the detail of students having SPI more than 8. 102 Meet ME 8
 Display the detail of students belongs to “CE” Branch 103 Hars EE 7
having SPI less than 8. h
 Display the detail of students belongs to either “CE” or 104 Punit CE 9
“ME”down
 Write Branch. the relational algebra for the Emplo
 Display the detail of students whose SPI between 6 and yee
EmpI Nam Sala
employee table. Dept
9. D e ry
 Display the detail of all employee.
101 Niles Sales 1000
 Display the detail of employee whose Salary more than
h 0
10000.
 Display the detail of employee belongs to “HR” Dept 102 Mayu HR 2500
r 0
having Salary more than 20000.
 Display the detail of employee belongs to either “HR” 103 Hardi HR 1500
k 0
or “Admin” Dept.
 Display the detail of employee 104 Ajay Admi 2000
whose
5KS01 (DBMS) Salary
 Unit II between
– Relational Algebra ,
Mr. Jaikumar M.Patil n 260
Relational Algebra
Operations
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.
Exam Display RollNo, Name and Branch of Answ ∏ RollNo, Name, Branch
ple all students. er (Student)

Stud Outp
ent
Roll Nam Branc SP ut
Roll Nam Branc
No e h I No e h
101 Raju CE 8 101 Raju CE
102 Mites ME 9 102 Mites ME
h h
103 Niles CI 9 103 Niles CI
h h
104Prof. Firoz
Meet CE
A Sherasiya 9 104
#3130703 (DBMS)  Unit 3 – Relational Query Meet CE 28
Exercise
 Write down the relational algebra for the student Stud
ent
table. Roll Nam Branc SP
No e h I
 Display RollNo, Name and SPI of all students.
 101 Raj CE 6
Display Name and SPI of all students.
 Display the Name of all students. 102 Meet ME 8
 Display the Name of all branches. 103 Hars EE 7
h
104 Punit CE 9
 Write down the Emplo
relational algebra for the yee
employee table. EmpI Nam Sala
Dept
D e ry
 Display EmpID with Name of all employee.
101 Niles Sales 1000
 Display Name and Salary of all employee. h 0
 Display the Name of all employee. 102 Mayu HR 2500
 Display the Name of all departments. r 0
103 Hardi HR 1500
k 0
104 Ajay Admi 2000
5KS01 (DBMS)  Unit II – Relational Algebra , n 0
Mr. Jaikumar M.Patil 29
Combined Projection & Selection Operation
Exam Display RollNo, Name & Branch of “ME”
ple Branch students.
Stud
ent
Roll Nam Branc SP
No e h I
101 Raju CE 8
102 Mites ME 9
h
103 Niles CI 9
h
104
Step- Meet σ
CE
Branch=‘ME’ 7 Answ ∏ RollNo, Name, Branch (σBranch=‘ME’
1 (Student) er (Student))
Outpu Outpu
t-1
Roll Nam Branc SP t-2
Roll Nam Branc
No e h I No e h
102 Mites ME 9 102 Mites ME
h h
#3130703 (DBMS)  Unit 3 – Relational Query
Prof. Firoz A Sherasiya 30
Combined Projection & Selection Operation
Exam Display Name, Branch and SPI of students whose SPI
ple is more than 8.
Stud
ent
Roll Nam Branc SP
No e h I
101 Raju CE 8
102 Mites ME 9
h
103 Niles CI 9
h
104
Step- Meet σSPI>8CE 7 Answ ∏ Name, Branch, SPI (σSPI>8
1 (Student) er (Student))
Outpu Outpu
t-1
Roll Nam Branc SP t-2
Nam Branc
SPI
No e h I e h
102 Mites ME 9 Mites ME 9
h h
103Prof. Firoz
Niles CI
A Sherasiya
9 #3130703 (DBMS)  UnitNiles CI Query
3 – Relational 9 31
Combined Projection & Selection Operation
Exam Display Name, Branch and SPI of students who belongs to “CE” Branch
ple and SPI is more than 7.
Stud
ent
Roll Nam Branc SP
No e h I
101 Raju CE 8
102 Mites ME 9
h
103 Niles CI 9
h
104
Step- Meet σBranch=‘CE’
CE 7
Λ SPI>7 Answ ∏ Name, Branch, SPI (σBranch=‘CE’ Λ SPI>7
1 (Student) er (Student))
Outpu Outpu
t-1
Roll Nam Branc SP t-2
Nam Branc
SPI
No e h I e h
101 Raju CE 8 Raju CE 8

5KS01 (DBMS)  Unit II – Relational Algebra ,


Mr. Jaikumar M.Patil 32
Combined Projection & Selection Operation
Exam Display Name of students along with their Branch who belong to either “ME”
ple Branch or “CI” Branch.
Stud
ent
Roll Nam Branc SP
No e h I
101 Raju CE 8
102 Mites ME 9
h
103 Niles CI 9
h
104
Step- Meet σBranch=‘ME’
CE 7
V Branch=‘CI’ Answ ∏ Name, Branch (σBranch=‘ME’ V Branch=‘CI’
1 (Student) er (Student))
Outpu Outpu
t-1
Roll Nam Branc SP t-2
Nam Branc
No e h I e h
102 Mites ME 9 Mites ME
h h
103Mr. Jaikumar
Niles M.Patil
CI 9 5KS01 (DBMS) Niles CI
Unit II – Relational Algebra ,
33
Exercise
 Write down the relational algebra for the student Stud
ent
table. Roll Nam Branc SP
No e h I
 Display Rollno, Name and SPI of all students belongs to
“CE” Branch. 101 Raj CE 6
 List the Name of students with their Branch whose SPI 102 Meet ME 8
is more than 8 and belongs to “CE” Branch.
103 Hars EE 7
 List the Name of students along with their Branch and h
SPI who belongs to either “CE” or “ME” Branch and
104 Punit CE 9
having SPI more than 8.
 Write Emplo
down the relational algebra for the
 Display the Name of students with their Branch name yee
employee
whose SPI table.
between 7 and 9.
EmpI Nam
Dept
Sala
 Display the Name of employee belong to “HR” Dept D e ry
and having salary more than 20000. 101 Niles Sales 1000
 Display the Name of all “Admin” and “HR” Dept’s h 0
employee. 102 Mayu HR 2500
 List the Name of employee with their Salary who r 0
belongs to “HR” or “Admin” Dept having salary more 103 Hardi HR 1500
than 15000. k 0
 Display the Name of employee along with their Dept 104 Ajay Admi 2000
5KS01 (DBMS)  Unit II – Relational Algebra ,
name
Mr. Jaikumarwhose
M.Patil salary between 15000 and 30000. n 0
34
Relational Algebra
Operations
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
Exam Perform Cross Product between Student Answ (Student) X
ple and Result. er (Result)

Stud Resu Outp


ent
RN Nam Branc lt
RN SP ut
Student. Nam Branc Result. SP
o e h o I RNo e h RNo I
10 Raju CE 10 8 101 Raju CE 101 8
1 1
101 Raju CE 102 9
10 Mites ME 10 9
If both relations have some attribute with the same 102 Mites ME 101 8
2 h 2
name, it can be distinguished by combing relation- h
name.attribute-name.
102
#3130703 (DBMS)  Unit 3 – Relational Query
Mites ME 102 9
Prof. Firoz A Sherasiya 36
Cartesian Product / Cross Product Example
Consider only selected
Exam Perform Cross Product between Student attributes
ple and Result. • Student – RNo, Name and
Stud Resu Branch
ent
RN Nam Branc Se lt
RN SP B Ran • Result – RNo, SPI and BL
o e h m o I L k
10 Raju CE 3 10 8 1 2
1 1
10 Mites ME 5 10 9 0 1
2 h
Answ ∏ RNo, Name, Branch (Student)
3 X ∏ RNo, SPI, BL
er (Result)
Outp
ut
Student. Nam Branc Result. SP B
RNo e h RNo I L
101 Raju CE 101 8 1
101 Raju CE 103 9 0
102 Mites ME 101 8 1
h
102 Mites ME 1035KS01 (DBMS)
9  0Unit II – Relational Algebra ,
Mr. Jaikumar M.Patil 37
Cartesian Product / Cross Product Example
Exam Perform Cross Product between Student Consider only selected tuples
• Student – Branch=‘CE’ and
ple and Result.
Stud Resu Sem=3
ent lt • Result – SPI>7 and BL<1
RN Nam Branc Se RN SP B Ran
o e h m o I L k
10 Raju CE 3 10 8 1 2
1 1
10 Mites ME 5 10 9 0 1
2 h 3
10 Om CE 3 10 7 2 3
Answ σBranch=‘CE’ Λ Sem=3 X σSPI>7 Λ
3 5
(Student)
10
er Dhar CE 5
BL<1 (Result)
4
Outpa
ut
Student. Nam Branc Se Result. SP B Ran
RNo e h m RNo I L k
101 Raju CE 3 103 9 0 1
103 OM CE 3 103 9 0 1
5KS01 (DBMS)  Unit II – Relational Algebra ,
Mr. Jaikumar M.Patil 38
Relational Algebra
Operations
Natural Join / Inner Join
Section - 3.4
Natural Join
 Symbol:
 Notation: Relation-1 (R1) Relation-2 (R2) OR Algebra-1 Algebra-2
 In Natural Join at least one common attributes(column) that exist between two
relations.
 The result of Natural join is the set of all combinations of tuples in two relations R1
and R2 that are equal on their common attributes name.
 Operation: Natural join will retrieve consistent data from multiple relations.
 It combines records from different relations that satisfy a given condition.
 In Resulting relation the common attributes appear only once.
 Natural Join = Cartesian Product + Selection + Projection
Steps performed in
Natural Description
Steps Join
Step – 1 It performs Cartesian Product
Step – 2 Then it deletes inconsistent tuples
Then it removes an attribute from duplicate
Step – 3
attributes #3130703 (DBMS)  Unit 3 – Relational Query
Prof. Firoz A Sherasiya 40
Natural Join Example
Exam Perform Natural Join between Student Answ (Student)
ple and Result. er (Result)
Stud Resu Outp
ent
RN Nam Branc lt
RN SP ut
RN Nam Branc SP
o e h o I o e h I
10 Raju CE 10 8 10 Raju CE 8
1 1 To perform a Natural
1 Join there must be one
10 Mites ME 10 9 common attribute (column) between two
Steps
2 h performed in 3 relations.
Natural Join
Step:1 Perform Cross Step:2 Removes
Product
Student. Nam Branc Result. SP inconsistent
Student. Nam tuples
Branc Result. SP
RNo e h RNo I RNo e h RNo I
101 Raju CE 101 8 101 Raju CE 101 8
Step:3 Removes an attribute
101 Raju CE 103 9
from duplicate
RN Nam Branc SP
102 Mites ME 101 8 o e h I
h
10 Raju CE 8
102 Mites ME 103 9 1 II – Relational Algebra ,
5KS01 (DBMS)  Unit
h
Mr. Jaikumar M.Patil 41
Natural Join Example
Exam Perform Natural Join between Branch Answ (Branch)
ple and Faculty. er (Faculty)
Bran Facul Outp
ch
BI BNa ty
FI FNa BI ut
BI Bnam HO FI FNa
HOD
D me D me D D e D D me
1 CE Shah 10 Raj 1 1 CE Sha 10 Raj
1 h 1
2 ME Patel
10 Meet 2 2 ME Pate 10 Meet
3 l 3
To perform a Natural Join there must be one common
attribute (column) between two relations.

5KS01 (DBMS)  Unit II – Relational Algebra ,


Mr. Jaikumar M.Patil 42
Write down relational algebra for the following
tables/relations
 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)

Exam List the name of students with their department name and SPI of all student
ple belong to “CE” department.
Answ ∏ Sname, Dname, (σDname=‘CE’ (Student (Department
er SPI Academic)))

Exam Display the name of students with their project name whose
ple guide is “A. J. Shah”.
Answ ∏ Sname, (σFname=‘A.J.Shah’ (Student (Guide
er Pname Faculty)))
5KS01 (DBMS)  Unit II – Relational Algebra ,
Mr. Jaikumar M.Patil 43
Exercise: Write down relational algebra for the following tables/relations

 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”
#3130703 (DBMS)  Unit 3 – Relational Query
Prof. Firoz A Sherasiya 44
Inner Join
 Symbol:
 Notation: Relation-1 (R1) Relation-2 (R2) OR Algebra-1 Algebra-2
 An inner join in Database Management Systems (DBMS) is a type of join
operation that combines rows from two tables based on a specified
condition.
 The result set includes only the rows where the join condition is true,
meaning it returns only the matching rows from both tables.

#3130703 (DBMS)  Unit 3 – Relational Query


Prof. Firoz A Sherasiya 45
Inner Join Example
Exam Perform Inner Join between Student and Answ (Student)
ple Enrollment. er (Enrollment)
Stud Enrollment Outp
ent
Student Nam Course_ ut
Student Nam Cousre_I
Student_ID
_ID e ID _ID e D
1 Alice 1 101 1 Alice 101
2 Bob 2 102 2 Bob 102
3 Carol 3 103 3 Carol 103
4 David 5 104

5KS01 (DBMS)  Unit II – Relational Algebra ,


Mr. Jaikumar M.Patil 46
Relational Algebra
Operations
Outer Join
Section - 3.5
Outer Join
 In natural join some records are missing, if we want that missing
records than we have to use outer join.

Three types of
Outer Join Sym
Sr. Outer Join
1 Left Outer Join bol
2 Right Outer Join
3 Full Outer Join

To perform a Outer Join there must be one common


attribute (column) between two relations.

5KS01 (DBMS)  Unit II – Relational Algebra ,


Mr. Jaikumar M.Patil 48
Left Outer Join
 Symbol:
 Notation: Relation-1 (R1) Relation-2 (R2) OR Algebra-1 Algebra-2
 A Left outer join in DBMS is a type of join that returns all records from the left
table(table1) and the matched records from the right table(table2). If no match is
found in the right table, the result is Null for columns from the right table.
 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.
Exam Perform Left Outer Join between Student Answ (Student)
ple and Result. er (Result)
Stud Resu Outp
ent
Roll Nam Branc lt
Roll SP ut
Roll Nam Branc
SPI
No e h No I No e h
101 Raj CE 101 8 101 Raj CE 8
102 Meet ME 103 9 102 Meet ME NUL
L
Exerci What is the output of (Result)
se (Student).
#3130703 (DBMS)  Unit 3 – Relational Query
Prof. Firoz A Sherasiya 49
Left Outer Join Example
Exam Perform Left Outer Join between Student and Result. (Display
ple RollNo, Name and SPI)
Stud Resu
ent
Roll Nam Branc lt
Roll SP B
No e h No I L
101 Raj CE 101 8 1
102 Meet ME 103 9 0

Answ ∏ RollNo, Name, ((Student)


er SPI
(Result))
Outp
ut
Roll Nam
SPI
No e
101 Raj 8
102 Meet NUL
L
5KS01 (DBMS)  Unit II – Relational Algebra ,
Mr. Jaikumar M.Patil 50
Right Outer Join
 Symbol:
 Notation: Relation-1 (R1) Relation-2 (R2) OR Algebra-1 Algebra-2
 A Right outer join in DBMS is a type of join that returns all records from the Right
table(table1) and the matched records from the Left table(table2). If no match is
found in the left table, the result is Null for columns from the left table.
 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.
Exam Perform Right Outer Join between Student Answ (Student)
ple and Result. er (Result)
Stud Resu Outp
ent
Roll Nam Branc lt
Roll SP ut
Roll Nam Branc SP
No e h No I No e h I
101 Raj CE 101 8 101 Raj CE 8
102 Meet ME 103 9 103 NUL NULL 9
L
Exerci What is the output of (Result)
se (Student).
#3130703 (DBMS)  Unit 3 – Relational Query
Prof. Firoz A Sherasiya 51
Right Outer Join Example
Exam Perform Right Outer Join between Student and Result. (Display
ple RollNo, Name and SPI)
Stud Resu
ent
Roll Nam Branc lt
Roll SP B
No e h No I L
101 Raj CE 101 8 1
102 Meet ME 103 9 0

Answ ∏ RollNo, Name, ((Student)


er SPI (Result))
Outp
ut
Roll Nam SP
No e I
101 Raj 8
103 NUL 9
L
5KS01 (DBMS)  Unit II – Relational Algebra ,
Mr. Jaikumar M.Patil 52
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 kindFull
Perform of Outer
tuples having
Join noStudent
between matching, it willAnsw
be padded with NULL in
(Student)
Exam
pleresultant
and relation.
Result. er (Result)
Stud Resu Outp
ent
Roll Nam Branc lt
Roll SP ut
Roll Nam Branc
SPI
No e h No I No e h
101 Raj CE 101 8 101 Raj CE 8
102 Meet ME 103 9 102 Meet ME NUL
L
Exerci What is the output of (Result) 103 NUL NULL 9
se (Student). L
#3130703 (DBMS)  Unit 3 – Relational Query
Prof. Firoz A Sherasiya 53
Full Outer Join Example
Exam Perform Full Outer Join between Student and Result. (Display
ple RollNo, Name and SPI)
Stud Resu
ent
Roll Nam Branc lt
Roll SP B
No e h No I L
101 Raj CE 101 8 1
102 Meet ME 103 9 0

Answ ∏ RollNo, Name, ((Student) (Result))


er SPI

Outp
ut
Roll Nam
SPI
No e
101 Raj 8
102 Meet NUL
L
5KS01 (DBMS)  Unit II – Relational Algebra ,
103 Mr. Jaikumar
NUL M.Patil
9 54
Relational Algebra
Operations
Set Operators
Section - 3.6
Set Operators
 Set operators combine the results of two or more queries into a
single result.

Three types of Set


Operators Sym
Sr. Set Operator
1 Union bol
U
Intersect /
2 ∩
Intersection
Minus / Set
3 −
difference
Conditi Set operators will take two or more queries as input, which must be
• Bothunion-compatible.
ons queries should have same (equal) number of
columns
• Corresponding attributes should have the same
data type or domain
5KS01 (DBMS)  Unit II – Relational Algebra ,
Mr. Jaikumar M.Patil 56
Conditions to perform Set Operators
Conditio Both queries should have same (equal)
ns-1 number of columns.
Stud Facul Stud Facul
ent
RN Nam Dep SP ty
FI Nam Dep ent
RN Nam Dep ty Nam Dep
FId
o e t I d e t o e t e t
10 Raj CE 8 10 Patel CE 10 Raj CE 101 Patel CE
1 1 1
102 Shah ME
10 Meet ME 9 10 Shah ME 10 Meet ME
2 2 2 103 Dav ME
e
10 Jay
Conditio CE 9 10 Dav
Corresponding ME should have
attributes 10theJay CE
3
ns-2 same data3type.
e 3
Stud Facul Stud Facul
ent
RN Nam Dep SP ty
FI Nam Dep ent
RN Nam Dep SP ty
FI Nam Dep Ex
Sub
o e t I d e t o e t I d e t p
10 Raj CE 8 10 Patel CE DS 10 Raj CE 8 10 Patel CE 5
1 1 1 1
10 Meet ME 9 10 Shah ME DBM 10 Meet ME 9 10 Shah ME 3
2 2 S 2 2
10 JayFiroz ACE
Prof. 9
Sherasiya 10 Dav #3130703
ME (DBMS)
DF  Unit 3 – Relational Query
10 Jay CE 9 10 Dav ME 57 4
Set Operators [Exercise]

Exerci Check whether following tables are


se compatible or not:
• A: (First_name(char), Last_name(char), Date_of_Birth(date))
• B: (FName(char), LName(char), PhoneNumber(number))
Χ (Not compatible) Both tables have 3 attributes but third attributes datatype is
different.

• A: (First_name(char), Last_name(char), Date_of_Birth(date))


• B: (FName(char), LName(char), DOB(date))
 (Compatible) Both tables have 3 attributes and of same data type.

• Person (PersonID, Name, Address, Hobby)


• Professor (ProfessorID, Name, OfficeAddress, Salary)
• (Not compatible) Both tables have 4 attributes but forth attributes datatype is
∏ Name, Address
different.
& ∏ Name, OfficeAddress
(Person) (Professor)

• (Compatible) Both tables #3130703


have 2(DBMS)
attributes and of same data type.
 Unit 3 – Relational Query
Prof. Firoz A Sherasiya 58
Union Operator
 Symbol: U
 Notation: Relation-1 (R1) U Relation-2 (R2) OR Algebra-1 U Algebra-2
 Operation:
 It displays all the tuples/records belonging to the first relation (left relation) or the
second relation (right relation) or both.
 It also eliminates duplicate tuples (tuples present in both relations appear once).
Exam Perform Union between Customer and Answ (Customer) U
ple Employee. er (Employee)
Custo Emplo Outp
mer yee ut
Name Name Name
Raju Meet Manoj
Suresh Suresh Meet
Meet Manoj Raju
Suresh
Exerci Is there any difference in the output if we swap the tables in Union operator.
se (Employee) U (Customer).
#3130703 (DBMS)  Unit 3 – Relational Query
Prof. Firoz A Sherasiya 59
Intersect/ Intersection Operator
 Symbol: ∩
 Notation: Relation-1 (R1) ∩ Relation-2 (R2) OR Algebra-1 ∩ Algebra-2
 Operation:
 It displays all the tuples/records which are common from both relations.

Exam Perform Intersection between Customer Answ (Customer) ∩


ple and Employee. er (Employee)
Custo Emplo Outp
mer yee ut
Name Name Name
Raju Meet Meet
Suresh Suresh Suresh
Meet Manoj

Exerci Is there any difference in the output if we swap the tables in Intersection.
se (Employee) ∩ (Customer).
#3130703 (DBMS)  Unit 3 – Relational Query
Prof. Firoz A Sherasiya 60
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).
Rules for Set Difference:
 Same Number of Columns: Both tables involved in the operation must have the same

number of columns.

 Compatible Data Types: The corresponding columns must have the same or

compatible data types.

 Order Does Not Matter: The order of the rows does not affect the result.

 Eliminates Duplicates: The#3130703


Prof. Firoz A Sherasiya
(DBMS)  Unit 3 – Relational Query
set difference operation automatically removes duplicates
61
Minus/ Set difference Operator
 Symbol: −
 Notation: Relation-1 (R1) − Relation-2 (R2) OR Algebra-1 − Algebra-2

Exam Perform Set difference between Customer Answ (Customer) −


ple and Employee. er (Employee)
Custo Emplo Outp
mer yee ut
Name Name Name
Raju Meet Raju
Suresh Suresh
Meet Manoj

Exerci Is there any difference in the output if we swap the tables in Set difference.
se (Employee) − (Customer).

#3130703 (DBMS)  Unit 3 – Relational Query


Prof. Firoz A Sherasiya 62
Union Operators Example

Exam Display Name of person who are either


ple employee or customer.
Custo Emplo
mer
I Nam Balan yee
I Nam De
D e ce D e pt
1 Raju 10000 2 Sures CE
h
2 Sures 20000
h 3 Mano ME
j
Answ ∏ Name (Customer) U ∏ Name
er (Employee)
Outp
ut
Name
Manoj
Raju
Suresh

5KS01 (DBMS)  Unit II – Relational Algebra ,


Mr. Jaikumar M.Patil 63
Intersect/ Intersection Operators Example

Exam Display Name of person who are employee as well


ple as customer.
Custo Emplo
mer
I Nam Balan yee
I Nam De
D e ce D e pt
1 Raju 10000 2 Sures CE
h
2 Sures 20000
h 3 Mano ME
j
Answ ∏ Name (Customer) ∩ ∏ Name
er (Employee)
Outp
ut
Name
Suresh

5KS01 (DBMS)  Unit II – Relational Algebra ,


Mr. Jaikumar M.Patil 64
Minus/ Set difference Operators Example

Exam Display Name of person who are employee but


ple not customer.
Custo Emplo
mer
I Nam Balan yee
I Nam De
D e ce D e pt
1 Raju 10000 2 Sures CE
h
2 Sures 20000
h 3 Mano ME
j
Answ ∏ Name (Employee) − ∏ Name
er (Customer)
Outp
ut
Name
Manoj

5KS01 (DBMS)  Unit II – Relational Algebra ,


Mr. Jaikumar M.Patil 65
Minus/ Set difference Operators Example

Exam Display Name of person who are customer but


ple not employee.
Custo Emplo
mer
I Nam Balan yee
I Nam De
D e ce D e pt
1 Raju 10000 2 Sures CE
h
2 Sures 20000
h 3 Mano ME
j
Answ ∏ Name (Customer) − ∏ Name
er (Employee)
Outp
ut
Name
Raju

5KS01 (DBMS)  Unit II – Relational Algebra ,


Mr. Jaikumar M.Patil 66
Set Operators [Exercise]
Exerci What is the output of following relational algebra for the below
se mentioned tables:
Custo Emplo
mer
I Nam Balan yee
I Nam Sala
D e ce D e ry
1 Raju 10000 2 Sures 8000
h
2 Sures 20000
h 3 Mano 9000
j
Algebr ∏ (Customer) U ∏ ID, Name
ID, Name
a-1 (Employee)
Algebr ∏ ID, Name, Balance (Customer) U ∏ ID, Name, Salary
a-2 (Employee)
Algebr ∏ (Customer) ∩ ∏ ID, Name
ID, Name
a-3 (Employee)
Algebr ∏ ID, Name, Balance (Customer) ∩ ∏ ID, Name, Salary
a-4 (Employee)
5KS01 (DBMS)  Unit II – Relational Algebra ,
Mr. Jaikumar M.Patil 67
Set Operators [Exercise]
Exerci What is the output of following relational algebra for the below
se mentioned tables:
Custo Emplo
mer
I Nam Balan yee
I Nam Sala
D e ce D e ry
1 Raju 10000 2 Sures 8000
h
2 Sures 20000
h 3 Mano 9000
j
Algebr ∏ (Customer) − ∏ ID, Name
ID, Name
a-1 (Employee)
Algebr ∏ ID, Name, Balance (Customer) − ∏ ID, Name, Salary
a-2 (Employee)
Algebr ∏ (Employee) − ∏ ID, Name
ID, Name
a-3 (Customer)
Algebr ∏ ID, Name, Balance (Employee) − ∏ ID, Name, Salary
a-4 (Customer)
5KS01 (DBMS)  Unit II – Relational Algebra ,
Mr. Jaikumar M.Patil 68
Relational Algebra
Operations
Division Operator
Section - 3.7
Division Operator
 Symbol: ÷ (Division)
 Notation: Relation1 (R1) ÷ Relation2 (R2) OR Algebra1 ÷ Algebra2
 Condition:
 Attributes of relation2/algebra2 must be a proper subset of attributes of
relation1/algebra1.
 Operation:
 The output of the division operator will have attributes =
All attributes of relation1 – All attributes of relation2
 The output of the division operator will have tuples =
Tuples in relation1, which are associated with the all tuples of relation2.

#3130703 (DBMS)  Unit 3 – Relational Query


Prof. Firoz A Sherasiya 70
Division Operator Example
Exam Perform Division operation between Student Answ (Student) ÷
ple and Subject. er (Subject)
Stude Subje Outp
nt ct ut
Nam Subje Subjec Name
e ct t Rohit
Raj DBMS DBMS
Suresh
Raj DS DS
Meet DS DF
Meet DF
Rohit DBMS
Rohit DS
Rohit DF
Sures DBMS
h
Sures DF
h
5KS01 (DBMS)  Unit II – Relational Algebra ,
SuresMr. Jaikumar
DS M.Patil 71
Division Operator Example

A B B B B
Sn 1 2 3 4
PNo PNo PNo PNo PNo
o
P2 P2 P1 P2
S1 P1
P4 P2 P5
S1 P2
P4
S1 P3
Alge (A) ÷ Alge (A) ÷ Alge (A) ÷ Alge (A) ÷
S1 P4
bra (B1) bra (B2) bra (B3) bra (B4)
S2 P1 Outp Outp Outp Outp
ut ut ut ut
S2 P2 SNo SNo SNo SNo
S3 P2 S1 S1 S1
S4 P2 S2 S4
S4 P4 S3
S5 P4 S4

5KS01 (DBMS)  Unit II – Relational Algebra ,


Mr. Jaikumar M.Patil 72
Division Operator Example
Exam List the name of students doing a project in all
ple technologies.
Stude Proje
nt
RN Nam Technol ct
TI Technol
o e ogy D ogy
10 Raj .NET 1 .NET
1
2 PHP
10 Raj PHP
1 3 Android
10 Meet .NET 4 iPhone
2
Answ ∏ Name, Technology (Student) ÷ ∏ Technology
10 Meet PHP er (Project)
2 Outp
10 Meet iPhone ut
Name
2
Meet
10 Meet Android
2
10 Rohit Android
3 Mr. Jaikumar M.Patil
5KS01 (DBMS)  Unit II – Relational Algebra ,
73
Relational Algebra
Operations
Rename Operator
Section - 3.8
Rename Operator
 Symbol: ρ (Rho)
 Notation: ρ
A (X1,X2….Xn) (Relation)

 Operation:
 The rename operation is used to rename the output relation.
 The result of rename operator are also relations with new name.
 How to use:
 ρ x (E)
Returns a relation E under a new name X.
 ρ A1, A2. …,An (E)
Returns a relation E with the attributes renamed to A1, A2, …., An.
 ρ x(A1, A2. …,An) (E)
Returns a relation E under a new name X with the attributes renamed to A1, A2,
…., An.

#3130703 (DBMS)  Unit 3 – Relational Query


Prof. Firoz A Sherasiya 75
Rename Operator Example
Exam Rename table Exam Rename
ple ple attributes
Stud Stud
ent ent
Rn Na CP
RN Na CP
o me I o me I
10 Raj 8 10 Raj 8
1 1
10 Meet 9 10 Meet 9
2 2
AlgeJay ρPerson
10 7 AlgeJay ρ(RollNo,
10 7 StudentName, SPI)
3
bra (Student) 3
bra (Student)
Perso Stud
n ent
Roll StudentN SP
RN Na CP
o me I No ame I
10 Raj 8 101 Raj 8
1
102 Meet 9
10 Meet 9
2 103 Jay 7
5KS01 (DBMS)  Unit II – Relational Algebra ,
10Mr. Jaikumar
Jay M.Patil
7 76
Rename Operator Example
Exam Rename table and Exam Rename particular
ple attributes both ple attributes
Stud Stud
ent
Rn Na CP ent
Rn Na CP
o me I o me I
10 Raj 8 10 Raj 8
1 1
10 Meet 9 10 Meet 9
2 2
AlgeJay ρPerson
10 7 (RollNo, StudentName) (∏ RNo, Name AlgeJay ρStudentName
10 7 / Name
3
bra (Student)) 3
bra (Student)
Perso Stud
n
Roll StudentN ent
Rn StudentN CP
No ame o ame I
101 Raj 10 Raj 8
102 Meet 1
10 Meet 9
103 Jay 2
5KS01 (DBMS)  Unit II – Relational Algebra ,
Mr. Jaikumar M.Patil 77
Rename Operator Example
Exam Find out maximum CPI from Step- ρA (Student) X ρB
ple student table. 1 (Student)
Stud Outpu
ent
Rn Na CP t-1
A.Rn A.Na A.C B.Rn B.Na B.C
o me I o me PI o me PI
10 Raj 8 101 Raj 8 101 Raj 8
1
101 Raj 8 102 Meet 9
10 Meet 9
2 101 Raj 8 103 Jay 7
10 Jay
Step- 7 σ
A.CPI<B.CPI (ρA (Student) X ρB 102 Meet 9 101 Raj 8
3
2 (Student)) 102 Meet 9 102 Meet 9
Outpu
t-2 102 Meet 9 103 Jay 7
A.Rn A.Na A.C B.Rn B.Na B.C
o me PI o me PI 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
5KS01 (DBMS)  Unit II – Relational Algebra ,
Mr. Jaikumar M.Patil 78
Rename Operator Example
Exam Find out maximum CPI from
ple student table.
Stud
Step- ∏ A.CPI (σA.CPI<B.CPI (ρA (Student) X ρB
ent
Rn Na CP 3 (Student)))
o me I Outpu
10 Raj 8 t-3
A.CPI
1
8
10 Meet 9
2 7
10 Jay
Step- 7 σ
A.CPI<B.CPI (ρA (Student) X ρB
3
2 (Student))
Outpu
t-2
A.Rn A.Na A.C B.Rn B.Na B.C
o me PI o me PI
101 Raj 8 102 Meet 9
103 Jay 7 101 Raj 8
103 Jay 7 102 Meet 9
#3130703 (DBMS)  Unit 3 – Relational Query
Prof. Firoz A Sherasiya 79
Rename Operator Example
Exam Find out maximum CPI from
ple student table.
Stud
Step- ∏ A.CPI (σA.CPI<B.CPI (ρA (Student) X ρB
ent
Rn Na CP 3 (Student)))
o me I Outpu
10 Raj 8 t-3
A.CPI
1
8
10 Meet 9
2 7
10 Jay 7
Step-
3 ∏ CPI (Student) − ∏ A.CPI (σA.CPI<B.CPI (ρ A (Student) X
4
ρStud
B (Student))) Outpu Outpu
ent t-3 t
CPI A.CPI CPI
8 − 8 = 9
9 7
7
#3130703 (DBMS)  Unit 3 – Relational Query
Prof. Firoz A Sherasiya 80
Relational Algebra
Operations
Aggregate Functions
Section - 3.9
Aggregate Functions
 Symbol: g or G
 Notation: g (Relation)
function-name(column), function-name(column), …, function-name(column)

 Operation:
 It takes a more than one value as input and returns a single value as output
(result).
 Aggregate functions are:
 Sum (It returns the sum (addition) of the values of a column.)
 Max (It returns the maximum value for a column.)
 Min (It returns the minimum value for a column.)
 Avg (It returns the average of the values for a column.)
 Count (It returns total number of values in a given column.)

#3130703 (DBMS)  Unit 3 – Relational Query


Prof. Firoz A Sherasiya 82
Aggregate Functions Example
Stud
ent Exam Find out sum of CPI of all Outpu
Rn Bran Semes CP ple students. t
Name sum
o ch ter I
Answ g sum(CPI)
10 Rames CE 3 9 73
er (Student)
1 h
10 Mahes EC 3 8 Exam Find out maximum & Outpu
2 h ple minimum CPI. tma mi
10 Suresh ME 4 7 Answ g max(CPI), min(CPI) x n
3 er (Student) 9 7
10 Amit EE 4 8 Exam Count the number of Outpu
4 ple students. t
count
10 Anita CE 4 8 Answ g count(Rno)
5 9
er (Student)
10 Reeta ME 3 7
6
Exam Find out average of CPI of all Outpu
ple students. t
avg
Answ g avg(CPI)
10 Rohit EE 4 9
7 8.11
er (Student)
10 Chetan CE 3 8
#3130703 (DBMS)  Unit 3 – Relational Query
8 Prof. Firoz A Sherasiya 83
Relational Algebra [Exercise]
 Write down relational algebras for the following table:
 Employee (person-name, street, city)
 Works (person-name, company-name, salary)
 Company (company-name, city)
 Managers (person-name, manager-name)
 Find the names of all employees who work for “TCS”.
 Find the names and cities of residence of all employees who work for “Infosys”.
 Find the names, street and city of residence of all employees who work for “ITC” and
earn more than $10,000 per annum.
 Find the names of all employees in this database who live in the same city as the
company for which they work.
 Find the names of all employees working in “TCS” who earn more than 25000 and
less than 40000.
 Find the name of employee whose manager is “Ajay Patel” and salary is more than
50000.
 Display the name of employee with street, city, company name, salary and manager
name staying in “Rajkot” and working in “Ahmedabad”.
 Find maximum, minimum and average salary of all employee.
#3130703 (DBMS)  Unit 3 – Relational Query
 Prof.
Find Firozout the total number of employee.
A Sherasiya 84
Open Source and
Commercial DBMS
Section - 4
Open Source and Commercial DBMS
Open Source Commercial DBMS
DBMS, which is available in the DBMS, which is available in the
market at free of cost. market at a certain price.
The code of open source DBMS The code of commercial DBMS
product can be viewed, shared or product can not be view, share or
modified
There arebychances
the community.
of malfunctioning modify by theiscommunity.
The security high and code is not
with code as source code is open. accessible to unauthorized person.
Examples: MySQL, MongoDB, SQLite Examples: Microsoft SQL Server, IBM
etc Db2 etc

5KS01 (DBMS)  Unit II – Relational Algebra ,


Mr. Jaikumar M.Patil 86
Questions asked in SGBAU
1. Define Super key, Primary key, Candidate key and Alternate key.
2. Explain following Relational Algebra Operation with example.
I. Selection
II. Projection
III. Cross Product
IV. Joins (Inner Join, Outer Joins)
V. Rename
VI. Division
VII. Set operators
3. Explain different aggregate functions with example.

5KS01 (DBMS)  Unit II – Relational Algebra ,


Mr. Jaikumar M.Patil 87
Questions asked in SGBAU [Relational Algebra]
4. Consider the following relational database, where the primary keys are
underlined. Give an expression in the relational algebra to express each
of the following queries
 employee (ssn, name, dno, salary, hobby, gender)
 department (dno, dname, budget, location, mgrssn)
 works_on (ssn, pno)
 project (pno, pname, budget, location, goal)
I. List all pairs of employee names and the project numbers they work on.
II. List out department number, department name and department budget.
III. List all projects that Raj Yadav works on by project name.
IV. List the names of employees who supervise themselves.

5KS01 (DBMS)  Unit II – Relational Algebra ,


Mr. Jaikumar M.Patil 88
Questions asked in SGBAU [Relational Algebra]
5. Consider the following relational database, where the primary keys are
underlined. Give an expression in the relational algebra to express each
of the following queries
 course (course-id, title, dept_name, credits)
 instructor (id, name, dept_name, salary)
 section (course-id, sec-id, semester, year, building, room_no, time_slot_id)
 teaches (id, course-id, sec-id, semester, year)
I. Find the name of all instructors in the physics department.
II. Find all the courses taught in the fall 2009 semester but not in Spring semester.
III. Find the names of all instructors in the Comp. Sci. department together with the
course titles of all the courses that the instructors teach.
IV. Find the average salary in each department.

5KS01 (DBMS)  Unit II – Relational Algebra ,


Mr. Jaikumar M.Patil 89
Questions asked in SGBAU [Relational Algebra]
6. Consider the following relations and write an relational algebra:
 EMP (empno, ename, jobtitle, managerno, hiredate, sal, commission, deptno)
 DEPT (deptno, dname, location)
I. Find the Employees working in the department number10, 20, 30 only.
II. Find Employees whose names start with letter A or letter a.
III. Find Employees along with their department name.
IV. Find the Employees who are working in Smith's department
V. Find the Employees who get salary more than Allen’s salary.
VI. Display employees who are getting maximum salary in each department.
VII. Find list of employees whose hire date is on or before 1-April-18.
7.Consider the relational database given below and give an expression in
the relational algebra:
 Employee (person-name, street, city) , Works (person-name, company-name, salary)
 Company (company-name, city) , Manages (person-name, manager-name)
I. Find the names of all employees in this database who live in the same city as the
company for which they work.
II. Find the names, street address, and cities of residence of all employees who work
#3130703 (DBMS)  Unit 3 – Relational Query
for
Prof. HCL
Firoz and earn more than
A Sherasiya $10,000 per annum. 90
Questions asked in SGBAU[Relational Algebra]
8. The relational database schema is given below and write the relational algebra
expressions for the given queries.
 employee (person-name, street, city)
 works (person-name, company-name, salary)
 company (company-name, city)
 manages (person-name, manager-name)
I. Find the names of all employees who work for First Bank Corporation.
II. Find the names and cities of residence of all employees who work for First Bank
Corporation.
III. Find the names, street address, and cities of residence of all employees who work
for First Bank Corporation and earn more than $10,000 per annum.
IV. Find the names of all employees in this database who do not work for First Bank
Corporation.

5KS01 (DBMS)  Unit II – Relational Algebra ,


Mr. Jaikumar M.Patil 91
Database Management Systems
(DBMS)
GTU # 3130703

Thank
You

Mr. J. M. Patil
Department of Computer Science and
Engineering
SSGMCE,Shegaon
[email protected]
9921860806

You might also like