Relational Algebra
Outline
1 Selection and Projection
2 Union, intersection, and difference
3 Renaming
4 Cross-Product, Join and Division
5 Expression Tree and Introduction of Query Optimization
INTRO
ALGEBRA?
Mathematicalsystem that consists of :
Operands : variables or values from which values can be constructed
Operators : symbols denoting procedures that construct new values from given
values
Example:
INTRO
• Relational Algebra : a formal query language
• The relational algebra is important because:
1. It provides a formal foundation for relational model operations;
2. It is used as a basis for implementing and optimizing queries in the query
processing and optimization modules that are integral parts of relational
database management systems (RDBMS).
• Relational Algebra consists of :
• Basic Operator (selection, projection, union, intersect, difference, cross-
product)
• Additional Operator (renaming, join, division)
SELECTION
AND
PROJECTION
1
SCHEMA
DataPasien Dokter
IDPasien NoKTP Nama Pasien NID KodePoli Nama
D009 P01 Dr. Budikusnaedi
PS00006 3507254111940001 Lidra Trifidya
D0010 P02 Dr. Hariyanto Kusuma
PS00007 3606125204940003 Yutika Amelia Effendi D0011 P02 Dr. Sri Herianti
D0012 P03 Drg. Elvin Purwantari
Pemeriksaan
IDPeriksa NID IDPasien TglPeriksa Diagnosa
C001 D009 PS00006 01-Jan-15 Radang Usus Buntu
C002 D010 PS00007 02-Jan-15 Gigi Berlubang
C003 D011 PS00007 10-Feb-15 Flu
C004 D019 PS00007 02-May-15 Radang Usus Buntu
SELECTION
•Selection
operators : pick certain rows
• is a condition (as in “if” statements) that refers to attributes of
• is all those tuples of that satisfy
SELECTION
Example 1 : A patient who has IDPasien PS00006
Result:
DataPasien
IDPasien NoKTP Nama Pasien
PS00006 3507254111940001 Lidra Trifidya
SELECTION
Example 2 : Patients who are handled by a doctor with NID D009 on 01 January 2015
Result:
Pemeriksaan
IDPeriksa NID IDPasien TglPeriksa Diagnosa
C001 D009 PS00006 01-Jan-15 Radang Usus Buntu
PROJECTION
•Projection
operators : pick certain columns
• is a list of attributes from the schema of
• is constructed by looking at each tuple of , extracting the attributes on list , L , in
the order specified, and creating from those components a tuple for
PROJECTION
Example 1 : Show IDPeriksa and IDPasien on Table Pemeriksaan
Result:
Pemeriksaan
IDPeriksa IDPasien
C001 PS00006
C002 PS00007
C003 PS00007
C004 PS00007
SELECTION AND
PROJECTION
Example 1 : Show IDPeriksa and IDPasien of Patients who are handled by a doctor with
NID D009 on 01 January 2015
Result:
Pemeriksaan
IDPeriksa IDPasien
C001 PS00006
UNION, INTERSECTION,
AND
DIFFERENTS
2
UNION, INTERSECTION,
DIFFERENT
• Union: R∪ S returns a relation instance containing all tuples that occur in either
relation instance R or relation instance S (or both)
• Intersection: R∩S returns a relation instance containing all tuples that occur in
both R and S
• Set-difference: R−S returns a relation instance containing all tuples that occur in
R but not in S.
UNION, INTERSECTION,
DIFFERENT
Union
Dokter KepalaDokter
NID KodePoli Nama NID KodePoli Nama NID KodePoli Nama
D0001 Bedah Saraf Agus Turchan D0001 Bedah Saraf Agus Turchan D0001 Bedah Saraf Agus Turchan
D0002 Bedah Saraf Achmad Fahmi D0003 Anak Sulis D0002 Bedah Saraf Achmad Fahmi
D0003 Anak Sulis
D0003 Anak Sulis
D0004 Anak Prima
D0004 Anak Prima
UNION, INTERSECTION,
DIFFERENT
Intersection
Dokter KepalaDokter
NID KodePoli Nama NID KodePoli Nama NID KodePoli Nama
D0001 Bedah Saraf Agus Turchan D0001 Bedah Saraf Agus Turchan D0001 Bedah Saraf Agus Turchan
D0002 Bedah Saraf Achmad Fahmi D0003 Anak Sulis D0003 Anak Sulis
D0003 Anak Sulis
D0004 Anak Prima
UNION, INTERSECTION,
DIFFERENT
Set-Difference
Dokter KepalaDokter
NID KodePoli Nama NID KodePoli Nama NID KodePoli Nama
D0001 Bedah Saraf Agus Turchan D0001 Bedah Saraf Agus Turchan D0002 Bedah Saraf Achmad Fahmi
D0002 Bedah Saraf Achmad Fahmi D0003 Anak Sulis D0004 Anak Prima
D0003 Anak Sulis
D0004 Anak Prima
RENAMING
3
SCHEMA
DataPasien Dokter
IDPasien NoKTP Nama Pasien NID KodePoli Nama
D009 P01 Dr. Budikusnaedi
PS00006 3507254111940001 Lidra Trifidya
D0010 P02 Dr. Hariyanto Kusuma
PS00007 3606125204940003 Yutika Amelia Effendi D0011 P02 Dr. Sri Herianti
D0012 P03 Drg. Elvin Purwantari
Pemeriksaan
IDPeriksa NID IDPasien TglPeriksa Diagnosa
C001 D009 PS00006 01-Jan-15 Radang Usus Buntu
C002 D010 PS00007 02-Jan-15 Gigi Berlubang
C003 D011 PS00007 10-Feb-15 Flu
C004 D019 PS00007 02-May-15 Radang Usus Buntu
RENAMING
•Renaming
operators : changes attribute names for a relation without changing any
values.
There are two ways to express renaming:
2.
where has attributes ,…, and the same tuples as
RENAMING
•Renaming
tables: changes the name of table
is a table whose name is changed to
RENAMING
Example 1 : Change IDPeriksa of Table Pemeriksaan to NomorUrut and IDPasien of Table
Pemeriksaan to KodePasien
or
Result:
Pemeriksaan
NomorUrut NID KodePasien TglPeriksa Diagnosa
C001 D009 PS00006 01-Jan-15 Radang Usus Buntu
C002 D010 PS00007 02-Jan-15 Gigi Berlubang
C003 D011 PS00007 10-Feb-15 Flu
C004 D019 PS00007 02-May-15 Radang Usus Buntu
RENAMING
Example 1 : Change the name of Table Pemeriksaan to Table DaftarPemeriksaan
Result:
DaftarPemeriksaan
IDPeriksa NID IDPasien TglPeriksa Diagnosa
C001 D009 PS00006 01-Jan-15 Radang Usus Buntu
C002 D010 PS00007 02-Jan-15 Gigi Berlubang
C003 D011 PS00007 10-Feb-15 Flu
C004 D019 PS00007 02-May-15 Radang Usus Buntu
RENAMING
Example 1 : Change the name of Table Pemeriksaan to Table DaftarPemeriksaan, change
IDPeriksa to NomorUrut and change IDPasien to KodePasien
or
Result:
DaftarPemeriksaan
NomorUrut NID KodePasien TglPeriksa Diagnosa
C001 D009 PS00006 01-Jan-15 Radang Usus Buntu
C002 D010 PS00007 02-Jan-15 Gigi Berlubang
C003 D011 PS00007 10-Feb-15 Flu
C004 D019 PS00007 02-May-15 Radang Usus Buntu
CROSS-PRODUCT, JOIN,
AND
DIVISIONS
4
CROSS-PRODUCT
•Cross-Product
operators : a relation instance whose schema contains all the
fields of R (in the same order as they appear in R)
followed by all the fields of S (in the same order as they
appear in S)
CROSS-PRODUCT
Dokter KepalaDokter
NID KodePoli Nama NID KodePoli Nama
D0002 Bedah Saraf Achmad Fahmi D0001 Bedah Saraf Agus Turchan
D0004 Anak Prima D0003 Anak Sulis
NID KodePoli Nama NID KodePoli Nama
D0002 Bedah Saraf Achmad Fahmi D0001 Bedah Saraf Agus Turchan
D0002 Bedah Saraf Achmad Fahmi D0003 Anak Sulis
D0004 Anak Prima D0001 Bedah Saraf Agus Turchan
D0004 Anak Prima D0003 Anak Sulis
JOIN
Join combines information from two or more relations
• Join is cross product operation, followed by selection and projection
Type of Join :
• Theta-join (conditional join)
• Natural join
• Left Outer Join
• Right Outer Join
• Full Outer Join
JOIN
Theta Join (Conditional Join)
• Apply to the result
• The special case of theta join is Equijoin (having equality operator as the
condition C )
JOIN
Theta Join
A B
NIDA NameA NIDB NameB
1 K 1 M
5 L 2 N
NIDA NameA NIDB NameB
1 K 2 N
JOIN
Equijoin
• The condition (C) consists solely of equalities ( = )
• Equijoin is a theta join using equality operator
• In Equijoin, the join operation do an additional projection in which one of
the attribute in the condition is dropped
JOIN
Equijoin
Dokter KepalaDokter
NID KodePoli Nama NIDKepala KodePoli Nama
D0001 Bedah Saraf Agus Turchan D0001 Bedah Saraf Agus Turchan
D0004 Anak Prima D0003 Anak Sulis
NID KodePoli Nama KodePoli Nama
D0001 Bedah Saraf Agus Turchan Bedah Saraf Agus Turchan
Because of equijoin, only one of the attribute in the condition, i.e.
Dokter.NID=KepalaDokter.NIDKepala, is chosen, so KepalaDokter.NIDKepala is projected out.
JOIN
Natural Join
A useful join variant ( natural join) connects two relations by:
Equating attributes of the same name, and
Projecting out one copy of each pair of equated attributes
JOIN
Natural Join
Dokter KepalaDokter
NID KodePoli Nama NID KodePoli Nama
D0001 Bedah Saraf Agus Turchan D0001 Bedah Saraf Agus Turchan
D0004 Anak Prima D0003 Anak Sulis
NID KodePoli Nama
D0001 Bedah Saraf Agus Turchan
Because both of Table Dokter and Table KepalaDokter have same names of attributes, so only one
NID, one KodePoli and one Nama are chosen.
JOIN
Natural Join
Dokter KepalaDokter
NID KodePoli Nama NIDKepala KodePoliKepala NamaKepala
D0001 Bedah Saraf Agus Turchan D0001 Bedah Saraf Agus Turchan
D0004 Anak Prima
D0003 Anak Sulis
NID KodePoli Nama NIDKepala KodePoliKepala NamaKepala
D0001 Bedah Saraf Agus Turchan D0001 Bedah Saraf Agus Turchan
D0001 Bedah Saraf Agus Turchan D0003 Anak Sulis
D0004
D0004 Anak
Anak Prima
Prima D0001
D0001 Bedah
Bedah Saraf
Saraf Agus
Agus Turchan
Turchan
D0004
D0004 Anak
Anak Prima
Prima D0003
D0003 Anak
Anak Sulis
Sulis
Because there is no equated attributes of Table Dokter and Table KepalaDokter, each tuple in Table
Dokter is paired with each tuple in Table KepalaDokter (natural join becomes cross-product)
JOIN
Left Outer Join
It is left join in SQL
The relational algebra of left outer join is shown below.
JOIN
Left Outer Join
A B
NIDA NameA NIDB NameB
1 K 1 M
5 L 2 N
NIDA NameA NIDB NameB
1 K NULL NULL
5 L 1 M
5 L 2 N
JOIN
Right Outer Join
It is right join in SQL
The relational algebra of left outer join is shown below.
JOIN
Right Outer Join
A B
NIDA NameA NIDB NameB
1 K 1 M
5 L 2 N
NIDA NameA NIDB NameB
NULL NULL 1 M
1 K 2 N
JOIN
Full Outer Join
It is full join in SQL
The relational algebra of left outer join is shown below.
JOIN
Full Outer Join
A B
NIDA NameA NIDB NameB
1 K 1 M
5 L 2 N
NIDA NameA NIDB NameB
1 K 2 N
5 L NULL NULL
NULL NULL 1 M
DIVISION
Division
• The schema of the result relation contains the attributes defined in and not
defined in
DIVISION
Division
EXPRESSION TREE
AND
INTRODUCTION OF
QUERY OPTIMIZATION
5
EXPRESSION TREE
• Leaves : can be called operands -- either variables standing for relations or particular,
constant relations
• Interior nodes : can be called operators -- applied to their child or children
EXPRESSION TREE
Example 1 : Show IDPeriksa and IDPasien of Patients who are handled by a doctor with NID D009 on 01
January 2015
𝜋 𝐼𝐷𝑃𝑒𝑟𝑖𝑘𝑠𝑎 , 𝐼𝐷𝑃𝑎𝑠𝑖𝑒𝑛
𝜎 𝑁𝐼𝐷= 𝐷 009 ∧𝑇𝑔𝑙𝑃𝑒𝑟𝑖𝑘𝑠𝑎 = 01− 𝐽𝑎𝑛 −15
𝑃𝑒𝑚𝑒𝑟𝑖𝑘𝑠𝑎𝑎𝑛
EXPRESSION TREE
Example 2 : Show ID of patient, name of patient, name of doctors and the check date on 01 January 2015
𝜋 𝑁𝑜𝐾𝑇𝑃 , 𝑁𝑎𝑚𝑎 𝑃𝑎𝑠𝑖𝑒𝑛 , 𝑁𝑎𝑚𝑎 ,𝑇𝑔𝑙𝑃𝑒𝑟𝑖𝑘𝑠𝑎
𝜎 𝑇𝑔𝑙𝑃𝑒𝑟𝑖𝑘𝑠𝑎 = 01 −𝐽𝑎𝑛 − 15
⋈
𝐷𝑜𝑘𝑡𝑒𝑟 ⋈
𝑃𝑒𝑚𝑒𝑟𝑖𝑘𝑠𝑎𝑎𝑛
𝐷𝑎𝑡𝑎𝑃𝑎𝑠𝑖𝑒𝑛
QUERY OPTIMIZATION
• Relational Algebra can be used in query optimization by comparing the query with expression tree
• Example: Show ID of patient, name of patient, name of doctors and the check date on 01 January 2015
1st Option 2nd Option
𝜋
𝑁𝑜𝐾𝑇𝑃 , 𝑁𝑎𝑚𝑎 𝑃𝑎𝑠𝑖𝑒𝑛 , 𝑁𝑎𝑚𝑎 ,𝑇𝑔𝑙𝑃𝑒𝑟𝑖𝑘𝑠𝑎 𝜎 𝑇𝑔𝑙𝑃𝑒𝑟𝑖𝑘𝑠𝑎 = 01 − 𝐽𝑎𝑛 − 15
𝜎 𝑇𝑔𝑙𝑃𝑒𝑟𝑖𝑘𝑠𝑎 = 01 − 𝐽𝑎𝑛 − 15 ⋈
𝜋 𝑁𝐼𝐷 , 𝑁𝑎𝑚𝑎 ⋈
⋈
𝐷𝑜𝑘𝑡𝑒𝑟
𝐷𝑜𝑘𝑡𝑒𝑟 ⋈ 𝜋 𝐼𝐷𝑃𝑎𝑠𝑖𝑒𝑛 , 𝑁𝑜𝐾𝑇𝑃 , 𝑁𝑎𝑚𝑎 𝑃𝑎𝑠𝑖𝑒𝑛 𝜋 𝑁𝐼𝐷 , 𝐼𝐷𝑃𝑎𝑠𝑖𝑒𝑛 ,𝑇𝑔𝑙𝐶h𝑒𝑐𝑘
𝐷𝑎𝑡𝑎𝑃𝑎𝑠𝑖𝑒𝑛 𝑃𝑒𝑚𝑒𝑟𝑖𝑘𝑠𝑎𝑎𝑛 𝐷𝑎𝑡𝑎𝑃𝑎𝑠𝑖𝑒𝑛 𝑃𝑒𝑚𝑒𝑟𝑖𝑘𝑠𝑎𝑎𝑛
EXERCISES
6
SCHEMA
DataPasien(IDPasien: string, NoKtp: integer, Nama Pasien: string)
Dokter(NID: string, KodePoli: string, Nama: string)
Poli(KodePoli: string, NamaPoli: string)
Pemeriksaan(IDPeriksa: string, NID: string, IDPasien: string, TglPeriksa: date, Diagnosa: string)
IDPasien NoKTP Nama Pasien NID KodePoli Nama KodePoli NamaPoli
D009 P01 Dr. Budikusnaedi P01 Bedah Umum
PS00006 3507254111940001 Lidra Trifidya
D0010 P02 Dr. Hariyanto Kusuma P02 Mulut
PS00007 3606125204940003 Yutika Amelia D0011 P02 Dr. Sri Herianti P03 THT
Effendi
D0012 P03 Drg. Elvin Purwantari
IDPeriksa NID IDPasien TglPeriksa Diagnosa
C001 D009 PS00006 01-Jan-15 Radang Usus Buntu
C002 D010 PS00007 02-Jan-15 Gigi Berlubang
C003 D011 PS00007 10-Feb-15 Flu
C004 D019 PS00007 02-May-15 Radang Usus Buntu
Create the relational algebra, the expression tree and the table result
1. Select id of patient and check date of patient who got “Gigi Berlubang” or “Flu
2. Select KTP number and name of patients who checked on May
3. Select name of patients, name of doctors, the doctor’s poly, and check date of patients who handled by
doctor Dr. Budikusnaedi
4. Select a pair of doctors who are in the same poly (a pair cannot contain of same doctor)
5. Select patients who had been checked in all poly
51
Fill the results of the questions based on the below tables
6. = …
7. = …
8. = …
9. = …
52
ANSWER OF THE TASK
1. Select id of patient and check date of patient who got “Gigi Berlubang” or “Flu”
𝜋 𝐼𝐷𝑃𝑎𝑠𝑖𝑒𝑛 ,𝑇𝑔𝑙𝑃𝑒𝑟𝑖𝑘𝑠𝑎 (𝜎 𝐷𝑖𝑎𝑔𝑛𝑜𝑠𝑎= 𝐺𝑖𝑔𝑖 𝐵𝑒𝑟𝑙𝑢𝑏𝑎𝑛𝑔 ∨𝐷𝑖𝑎𝑔𝑛𝑜𝑠𝑎= 𝐹𝑙𝑢 (𝑃𝑒𝑚𝑒𝑟𝑖𝑘𝑠𝑎𝑎𝑛))
𝜋 𝐼𝐷𝑃𝑎𝑠𝑖𝑒𝑛 ,𝑇𝑔𝑙𝑃𝑒𝑟𝑖𝑘𝑠𝑎
𝜎 𝐷𝑖𝑎𝑔𝑛𝑜𝑠𝑎= 𝐺𝑖𝑔𝑖𝐵𝑒𝑟𝑙𝑢𝑏𝑎𝑛𝑔 ∨𝐷𝑖𝑎𝑔𝑛𝑜𝑠𝑎= 𝐹𝑙𝑢
𝑃𝑒𝑚𝑒𝑟𝑖𝑘𝑠𝑎𝑎𝑛
IDPasien TglPeriksa Diagnosa
PS00007 02-Jan-15 Gigi Berlubang
PS00007 10-Feb-15 Flu
ANSWER OF THE TASK
2. Select KTP number and name of patients who checked on May
𝜋 𝑁𝑜𝐾𝑇𝑃 , 𝑁𝑎𝑚𝑎 𝑃𝑎𝑠𝑖𝑒𝑛 (𝜎 𝑀𝑜𝑛𝑡h (𝑇𝑔𝑙𝑃𝑒𝑟𝑖𝑘𝑠𝑎 )= 5 (𝐷𝑎𝑡𝑎𝑃𝑎𝑠𝑖𝑒𝑛 ⋈ 𝑃𝑒𝑚𝑒𝑟𝑖𝑘𝑠𝑎𝑎𝑛))
𝜋 𝑁𝑜𝐾𝑇𝑃 , 𝑁𝑎𝑚𝑎 𝑃𝑎𝑠𝑖𝑒𝑛 NoKTP Nama Pasien
PS00007 Yutika Amelia Effendi
𝜎 𝑀𝑜𝑛𝑡h (𝑇𝑔𝑙𝑃𝑒𝑟𝑖𝑘𝑠𝑎 )= 5
⋈
𝐷𝑎𝑡𝑎 𝑃𝑎𝑠𝑖𝑒𝑛 𝑃𝑒𝑚𝑒𝑟𝑖𝑘𝑠𝑎𝑎𝑛
ANSWER OF THE TASK
3. Select name of patients, name of doctors, the doctor’s poly, and check date of patients who handled
by doctor Dr. Budikusnaedi
𝜋 (𝜎
𝑁𝑎𝑚𝑎 𝑃𝑎𝑠𝑖𝑒𝑛, 𝑁𝑎𝑚𝑎 , 𝑁𝑎𝑚𝑎 𝑃𝑜𝑙𝑖, 𝑇𝑔𝑙𝑃𝑒𝑟𝑖𝑘𝑠𝑎 (𝑃𝑜𝑙𝑖 ⋈ 𝐷𝑜𝑘𝑡𝑒𝑟 ⋈ ( 𝐷𝑎𝑡𝑎𝑃𝑎𝑠𝑖𝑒𝑛⋈ 𝑃𝑒𝑚𝑒𝑟𝑖𝑘𝑠𝑎𝑎𝑛 )
𝑁𝑎𝑚𝑎 = Dr. Budikusnaedi ( ) ))
𝜋
𝑁𝑎𝑚𝑎 𝑃𝑎𝑠𝑖𝑒𝑛 , 𝑁𝑎𝑚𝑎 , 𝑁𝑎𝑚𝑎 𝑃𝑜𝑙𝑖 , 𝑇𝑔𝑙𝑃𝑒𝑟𝑖𝑘𝑠𝑎 Nama Nama Nama Poli TglPeriksa
Pasien
Lidra Dr. Bedah Umum 01-Jan-15
𝜎 𝑁𝑎𝑚𝑎= Dr . Budikusnaedi Trifidya Budikusnaedi
⋈
𝑃𝑜𝑙𝑖 ⋈
𝐷𝑜𝑘𝑡𝑒𝑟 ⋈
𝐷𝑎𝑡𝑎 𝑃𝑎𝑠𝑖𝑒𝑛 𝑃𝑒𝑚𝑒𝑟𝑖𝑘𝑠𝑎𝑎𝑛
ANSWER OF THE TASK
4. Select a pair of doctors who are in the same poly (a pair cannot contain of same doctor)
or
𝜎 𝑁𝐼𝐷 ≠ 𝑁𝐼𝐷 1
NID KodePoli Nama NID1 Nama1
D0010 P02 Dr. Hariyanto D0011 Dr. Sri
Kusuma Herianti
⋈
D0011 P02 Dr. Sri D0010 Dr. Hariyanto
Herianti Kusuma
𝐷𝑜𝑘𝑡𝑒𝑟 𝜌 𝐷𝑜𝑘𝑡𝑒𝑟1(𝑁𝐼𝐷 →𝑁𝐼𝐷 1, 𝑁𝑎𝑚𝑎→𝑁𝑎𝑚𝑎1)
𝐷𝑜𝑘𝑡𝑒𝑟
ANSWER OF THE TASK
5. Select patients who had been checked in all poly
)
𝜋 𝐼𝐷𝑃𝑎𝑠𝑖𝑒𝑛 , 𝑁𝑜𝐾𝑇𝑃 , 𝑁𝑎𝑚𝑎 𝑃𝑎𝑠𝑖𝑒𝑛
There is no record because no
patient who had been checked in
/ all poly
⋈ 𝜋 𝐾𝑜𝑑𝑒𝑃𝑜𝑙𝑖
𝐷𝑜𝑘𝑡𝑒𝑟 ⋈
𝑃𝑜𝑙𝑖
𝐷𝑎𝑡𝑎𝑃𝑎𝑠𝑖𝑒𝑛 𝑃𝑒𝑚𝑒𝑟𝑖𝑘𝑠𝑎𝑎𝑛
ANSWER OF THE TASK
6. =
A B
5 6
ANSWER OF THE TASK
7. =
C
10
ANSWER OF THE TASK
A B B C
1 2 3 4
1 2 7 8
5 6 3 4
5 6 7 8
1 2 3 4
1 2 7 8
ANSWER OF THE TASK
9. =
A B B C
1 2 5 6
1 2 7 8
1 2 9 10
5 6 7 8
5 6 9 10
1 2 5 6
1 2 7 8
1 2 9 10
ANSWER OF THE TASK
10. =
There is no record because there is no same value between attribute B in Table R and attribute B in
Table R2