SlideShare una empresa de Scribd logo
1 de 48
Rushdi Shams, Dept of CSE, KUET 1
Database SystemsDatabase Systems
Relational AlgebraRelational Algebra
Version 1.0Version 1.0
2Rushdi Shams, Dept of CSE, KUET
What is Relational AlgebraWhat is Relational Algebra
 It is a collection of operations on relationsIt is a collection of operations on relations
 If you apply relational algebra on some relations,If you apply relational algebra on some relations,
the result will be another relationthe result will be another relation
 You can say a relation- a table.You can say a relation- a table.
 So, operation on tables=another tableSo, operation on tables=another table
 That is what relational algebra does!That is what relational algebra does!
3Rushdi Shams, Dept of CSE, KUET
Memory RefreshmentMemory Refreshment
 A relation is a set of attributes with values for eachA relation is a set of attributes with values for each
attribute such that:attribute such that:
 Each attribute value must be a single value onlyEach attribute value must be a single value only
(atomic).(atomic).
 All values for a given attribute must be of the same typeAll values for a given attribute must be of the same type
(or domain).(or domain).
 Each attribute name must be unique.Each attribute name must be unique.
 The order of attributes is insignificantThe order of attributes is insignificant
 No two rows (tuples) in a relation can be identical.No two rows (tuples) in a relation can be identical.
 The order of the rows (tuples) is insignificant.The order of the rows (tuples) is insignificant.
4Rushdi Shams, Dept of CSE, KUET
2 types of operation2 types of operation
1.1. Set theory operations:Set theory operations:
Union, Intersection, Difference and CartesianUnion, Intersection, Difference and Cartesian
product.product.
2.2. Specific Relational Operations:Specific Relational Operations:
Selection, Projection, Join, DivisionSelection, Projection, Join, Division
We will take a look into set theory operations,We will take a look into set theory operations,
and then we will dig into specific relationaland then we will dig into specific relational
operationsoperations
5Rushdi Shams, Dept of CSE, KUET
Our example tablesOur example tables
 There are 2 tables- R andThere are 2 tables- R and
S, we will play withS, we will play with
them!!them!!
6Rushdi Shams, Dept of CSE, KUET
UnionUnion
 RUS means, a unionRUS means, a union
operation on R and Soperation on R and S
that will produce anotherthat will produce another
table which will have alltable which will have all
the rows of R and Sthe rows of R and S
except the duplicatesexcept the duplicates
 There are 2 Sally in RThere are 2 Sally in R
and S, so, just take 1and S, so, just take 1
from themfrom them
7Rushdi Shams, Dept of CSE, KUET
DifferenceDifference
 R-S means, a differenceR-S means, a difference
operation on R and Soperation on R and S
that will produce anotherthat will produce another
table which will have alltable which will have all
the rows which is in Rthe rows which is in R
but not in S.but not in S.
 As Sally is in R and in S,As Sally is in R and in S,
so, she has been omitted.so, she has been omitted.
8Rushdi Shams, Dept of CSE, KUET
IntersectionIntersection
 R∩S means, anR∩S means, an
intersection operation onintersection operation on
R and S that will produceR and S that will produce
another table which willanother table which will
have all the rows whichhave all the rows which
are in both R and S.are in both R and S.
 As Sally is in R and in S,As Sally is in R and in S,
so, she has been selected.so, she has been selected.
9Rushdi Shams, Dept of CSE, KUET
More UnionMore Union
10Rushdi Shams, Dept of CSE, KUET
More IntersectionMore Intersection
11Rushdi Shams, Dept of CSE, KUET
More DifferenceMore Difference
12Rushdi Shams, Dept of CSE, KUET
ConstraintsConstraints
 You see tables, and start performing set theoryYou see tables, and start performing set theory
operations on them- MISTAKE!! You shouldoperations on them- MISTAKE!! You should
remember the following constraints before youremember the following constraints before you
perform set theory operations on tables.perform set theory operations on tables.
1.1. Attributes of relations needAttributes of relations need notnot be identical tobe identical to
perform union, intersection and differenceperform union, intersection and difference
operations.operations.
2.2. However, theyHowever, they must havemust have the same number ofthe same number of
attributes and the domains for correspondingattributes and the domains for corresponding
attributes must be identical.attributes must be identical.
13Rushdi Shams, Dept of CSE, KUET
Other things to rememberOther things to remember
 Union, Intersection and difference operatorsUnion, Intersection and difference operators
may only be applied to Union Compatiblemay only be applied to Union Compatible
relations.relations.
 Union and Intersection are commutativeUnion and Intersection are commutative
operationsoperations
RUS= SURRUS= SUR
R∩S = S∩RR∩S = S∩R
 Difference operation isDifference operation is NOTNOT commutative.commutative.
R - S not equal S - RR - S not equal S - R
14Rushdi Shams, Dept of CSE, KUET
Class PerformanceClass Performance
 Find out RUTFind out RUT
 Find out R∩TFind out R∩T
 Find out R-TFind out R-T
 Find out T-RFind out T-R
15Rushdi Shams, Dept of CSE, KUET
Cartesian ProductCartesian Product
 It provides all the combinations of rows from two tables.It provides all the combinations of rows from two tables.
 If A and B are 2 tables and A has 3 rows and B has 4 rows, then AIf A and B are 2 tables and A has 3 rows and B has 4 rows, then A×B is-×B is-
A1-B1A1-B1
A1-B2A1-B2
A1-B3A1-B3
A1-B4A1-B4
A2-B1A2-B1
A2-B2A2-B2
A2-B3A2-B3
A2-B4A2-B4
A3-B1A3-B1
A3-B2A3-B2
A3-B3A3-B3
A3-B4A3-B4
16Rushdi Shams, Dept of CSE, KUET
Cartesian Product (continued)Cartesian Product (continued)
17Rushdi Shams, Dept of CSE, KUET
More ProductMore Product
18Rushdi Shams, Dept of CSE, KUET
SelectionSelection
 Selection and Projection areSelection and Projection are unaryunary operators.operators.
 The selection operator isThe selection operator is sigma (sigma (σσ))
 The selection operation acts like a filter on aThe selection operation acts like a filter on a
relation byrelation by returning only a certain number ofreturning only a certain number of
rows.rows.
 TheThe resulting relation will have the sameresulting relation will have the same
number of attributesnumber of attributes as the original relation.as the original relation.
19Rushdi Shams, Dept of CSE, KUET
Selection (continued)Selection (continued)
 The resulting relationThe resulting relation maymay have fewer rows thanhave fewer rows than
the original relation.the original relation.
 The rows to be returned are dependent on aThe rows to be returned are dependent on a
condition that is part of the selection operator.condition that is part of the selection operator.
 σσCC (R) Returns only those rows in R that satisfy(R) Returns only those rows in R that satisfy
conditioncondition CC
 A condition C can be made up of anyA condition C can be made up of any
combination of comparison or logical operatorscombination of comparison or logical operators
that operate on the attributes of R.that operate on the attributes of R.
20Rushdi Shams, Dept of CSE, KUET
Selection ExampleSelection Example
 We have a table named EMP.We have a table named EMP.
 We will play with this table named EMP for a while!We will play with this table named EMP for a while!
21Rushdi Shams, Dept of CSE, KUET
Selection Example (continued)Selection Example (continued)
 Select only those Employees in the CS departmentSelect only those Employees in the CS department
σσ Dept=‘CS’Dept=‘CS’ (EMP)(EMP)
22Rushdi Shams, Dept of CSE, KUET
Selection Example (continued)Selection Example (continued)
 Select only those Employees with last name Smith whoSelect only those Employees with last name Smith who
are assistant professorsare assistant professors
σσ Name=‘Smith’Name=‘Smith’ ΛΛ Rank=‘Assistant’Rank=‘Assistant’ (EMP)(EMP)
23Rushdi Shams, Dept of CSE, KUET
Selection Example (continued)Selection Example (continued)
 Select only those Employees who are either AssistantSelect only those Employees who are either Assistant
Professors or in the Economics departmentProfessors or in the Economics department
σσ Dept =‘Econ’V Rank=‘Assistant’Dept =‘Econ’V Rank=‘Assistant’ (EMP)(EMP)
24Rushdi Shams, Dept of CSE, KUET
Selection Example (continued)Selection Example (continued)
 Select only those Employees who are not in the CSSelect only those Employees who are not in the CS
department or Adjunctsdepartment or Adjuncts
σσ ¬¬(Dept=‘CS’ V Rank=‘Adjunct’)(Dept=‘CS’ V Rank=‘Adjunct’) (EMP)(EMP)
25Rushdi Shams, Dept of CSE, KUET
More SelectionMore Selection
26Rushdi Shams, Dept of CSE, KUET
Class PerformanceClass Performance
 σσ(Rank='Adjunct'(Rank='Adjunct' ΛΛ Dept ='CS')Dept ='CS') (EMP)(EMP)
 σσ Rank= 'Associate'Rank= 'Associate' ΛΛ Dept ='CS'Dept ='CS' EMP )EMP )
 σσ Dept= 'CS'Dept= 'CS' VV Rank='Associate'Rank='Associate' EMP )EMP )
 σσ Age>26Age>26 (R U S)(R U S)
27Rushdi Shams, Dept of CSE, KUET
ProjectionProjection
 Projection is also aProjection is also a UnaryUnary operator.operator.
 The Projection operator isThe Projection operator is pi (pi (ππ))
 Projection limits the columns that will be returned from theProjection limits the columns that will be returned from the
original relation.original relation.
 The general syntax is:The general syntax is: ππ attributesattributes RR
Where attributes is the list of attributes to be displayed and R isWhere attributes is the list of attributes to be displayed and R is
the relation.the relation.
 The resulting table will have the same number of rows as theThe resulting table will have the same number of rows as the
original relation (unless there are duplicate rows produced).original relation (unless there are duplicate rows produced).
 The number of columns of the resulting relation may be equal toThe number of columns of the resulting relation may be equal to
or less than that of the original relation.or less than that of the original relation.
28Rushdi Shams, Dept of CSE, KUET
Projection ExampleProjection Example
 Project only the names and departments of theProject only the names and departments of the
employeesemployees
ππ name,deptname,dept (EMP)(EMP)
29Rushdi Shams, Dept of CSE, KUET
Combining selection &Combining selection &
projectionprojection
 Show the names of all employees working in the CSShow the names of all employees working in the CS
departmentdepartment
ππ namename ((σσ Dept ='CS'Dept ='CS' (EMP) )(EMP) )
30Rushdi Shams, Dept of CSE, KUET
Combining selection & projectionCombining selection & projection
(continued)(continued)
 Show the name and rank of those Employees who areShow the name and rank of those Employees who are
not in the CS department or Adjunctsnot in the CS department or Adjuncts
ππ name, rankname, rank (( σσ ¬¬(Rank='Adjunct' V Dept='CS')(Rank='Adjunct' V Dept='CS') (EMP) )(EMP) )
31Rushdi Shams, Dept of CSE, KUET
More ProjectionMore Projection
32Rushdi Shams, Dept of CSE, KUET
Class PerformanceClass Performance
 ππ name, rankname,rank (( σσ ¬¬(Rank='Adjunct'(Rank='Adjunct' ΛΛ Dept ='CS')Dept ='CS') (EMP) )(EMP) )
 ππ fname,agefname,age ((σσ Age>22Age>22 (R U S) )(R U S) )
 σσ office>300office>300 (( ππ name,rankname,rank (EMP))(EMP))
33Rushdi Shams, Dept of CSE, KUET
Aggregate FunctionsAggregate Functions
 We can also apply Aggregate functions to columnsWe can also apply Aggregate functions to columns
and rowsand rows
1.1. SUMSUM
2.2. MINIMUMMINIMUM
3.3. MAXIMUMMAXIMUM
4.4. AVERAGE, MEAN, MEDIANAVERAGE, MEAN, MEDIAN
5.5. COUNTCOUNT
 Aggregate functions are sometimes written using theAggregate functions are sometimes written using the
Projection operator or theProjection operator or the Tau character (Tau character (ττ))
34Rushdi Shams, Dept of CSE, KUET
Aggregate Functions (continued)Aggregate Functions (continued)
 Find the minimum SalaryFind the minimum Salary
ττ MIN(salary)MIN(salary) (EMP)(EMP)
 Find the average SalaryFind the average Salary
ττ AVG(salaryAVG(salary) (EMP)) (EMP)
 Count the number of employees in the CS departmentCount the number of employees in the CS department
ττ COUNT(name)COUNT(name) (( σσ Dept='CS'Dept='CS' (EMP) )(EMP) )
 Find the total payroll for the Economics departmentFind the total payroll for the Economics department
ττ SUM(salary)SUM(salary) ((σσ Dept='Econ'Dept='Econ' (EMP) )(EMP) )
35Rushdi Shams, Dept of CSE, KUET
JoinJoin
 Join operations bring together two tables and combineJoin operations bring together two tables and combine
their rows and columns in a specific fashion.their rows and columns in a specific fashion.
 The generic join operator called theThe generic join operator called the Theta JoinTheta Join isis
 It takes as arguments the columns from the two tablesIt takes as arguments the columns from the two tables
that are to be joined.that are to be joined.
 The join condition can beThe join condition can be
 When the join condition operator is  When the join condition operator is   ==   then we call  then we call
this an Equijointhis an Equijoin
 Note that the columns in common are repeated.Note that the columns in common are repeated.
36Rushdi Shams, Dept of CSE, KUET
Join (continued)Join (continued)
 Now, we have two tables Emp and depart.Now, we have two tables Emp and depart.
 We will play with these 2 tables for a while!We will play with these 2 tables for a while!
37Rushdi Shams, Dept of CSE, KUET
Join (continued)Join (continued)
 Find all information on every employee including theirFind all information on every employee including their
department infodepartment info
EMPEMP emp.Dept=depart.Deptemp.Dept=depart.Dept DEPARTDEPART
38Rushdi Shams, Dept of CSE, KUET
Join (continued)Join (continued)
 Take a look, one table has 5 rows, the other has 4 rows,Take a look, one table has 5 rows, the other has 4 rows,
the resultant has 5 rows!the resultant has 5 rows!
39Rushdi Shams, Dept of CSE, KUET
Join (continued)Join (continued)
 Find all information on every employee including departmentFind all information on every employee including department
info where the employee works in an office numbered less thaninfo where the employee works in an office numbered less than
the department main officethe department main office
EMPEMP (emp.office<depart.mainoffice)(emp.office<depart.mainoffice) ΛΛ (emp.dept =depart.dept)(emp.dept =depart.dept) DEPARTDEPART
40Rushdi Shams, Dept of CSE, KUET
Natural JoinNatural Join
 Notice in the generic (Theta) join operation, anyNotice in the generic (Theta) join operation, any
attributes in common (such as dept above) areattributes in common (such as dept above) are
repeated.repeated.
 The Natural Join operation removes theseThe Natural Join operation removes these
duplicate attributes.duplicate attributes.
 The natural join operator is:The natural join operator is: **
 We can also assume usingWe can also assume using ** that the jointhat the join
condition will becondition will be == on the two attributes inon the two attributes in
common.common.
41Rushdi Shams, Dept of CSE, KUET
Natural Join (continued)Natural Join (continued)
 Emp * departEmp * depart
 There are 2 Dept columns, one is omitted!There are 2 Dept columns, one is omitted!
42Rushdi Shams, Dept of CSE, KUET
Outer JoinOuter Join
 In the Join operations so far, only those tuples from bothIn the Join operations so far, only those tuples from both
relations that satisfy the join condition are included in therelations that satisfy the join condition are included in the
output relation.output relation.
 The Outer join includes other rows as well according to a fewThe Outer join includes other rows as well according to a few
rules.rules.
 Three types of outer joins:Three types of outer joins:
1.1. Left Outer Join includes all rows in the left hand relation andLeft Outer Join includes all rows in the left hand relation and
includes only those matching rows from the right hand relation.includes only those matching rows from the right hand relation.
2.2. Right Outer Join includes all rows in the right hand relationRight Outer Join includes all rows in the right hand relation
and includes only those matching rows from the left handand includes only those matching rows from the left hand
relation.relation.
3.3. Full Outer Join includes all rows in the left hand relation andFull Outer Join includes all rows in the left hand relation and
from the right hand relation.from the right hand relation.
43Rushdi Shams, Dept of CSE, KUET
Outer Join (continued)Outer Join (continued)
 Now we have another 2 tables.Now we have another 2 tables.
 We will play with them for next few slides!We will play with them for next few slides!
44Rushdi Shams, Dept of CSE, KUET
Left Outer JoinLeft Outer Join
PEOPLEPEOPLE people.food=menu.foodpeople.food=menu.food MENUMENU
45Rushdi Shams, Dept of CSE, KUET
Right Outer JoinRight Outer Join
PEOPLEPEOPLE people.food=menu.foodpeople.food=menu.food MENUMENU
46Rushdi Shams, Dept of CSE, KUET
Full Outer JoinFull Outer Join
PEOPLEPEOPLE people.food=menu.foodpeople.food=menu.food MENUMENU
47Rushdi Shams, Dept of CSE, KUET
Outer UnionOuter Union
 In this case, just go from row of table 1 to row of table 2.In this case, just go from row of table 1 to row of table 2.
 Take common columns just once.Take common columns just once.
 if matching criteria don’t match, then put NULL, else include inif matching criteria don’t match, then put NULL, else include in
the tablethe table
 Repeat the same but this time go from row of table 2 to row ofRepeat the same but this time go from row of table 2 to row of
table 1table 1
48Rushdi Shams, Dept of CSE, KUET
ReferencesReferences
 Database Management Systems by Prof.Database Management Systems by Prof.
Holowczak, Zicklin School of Business,Holowczak, Zicklin School of Business,
Baruch College, City University of NewBaruch College, City University of New
YorkYork
 Database Systems: Design, ImplementationDatabase Systems: Design, Implementation
& Management by Rob & Coronel, 6& Management by Rob & Coronel, 6thth
EditionEdition

Más contenido relacionado

La actualidad más candente

Relational Algebra
Relational AlgebraRelational Algebra
Relational AlgebraAmin Omi
 
Relational Algebra Introduction
Relational Algebra IntroductionRelational Algebra Introduction
Relational Algebra IntroductionMd. Afif Al Mamun
 
Relational algebra-and-relational-calculus
Relational algebra-and-relational-calculusRelational algebra-and-relational-calculus
Relational algebra-and-relational-calculusSalman Vadsarya
 
Relational algebra (basics)
Relational algebra (basics)Relational algebra (basics)
Relational algebra (basics)usama nizam
 
Dbms ii mca-ch5-ch6-relational algebra-2013
Dbms ii mca-ch5-ch6-relational algebra-2013Dbms ii mca-ch5-ch6-relational algebra-2013
Dbms ii mca-ch5-ch6-relational algebra-2013Prosanta Ghosh
 
Relational Algebra & Calculus
Relational Algebra & CalculusRelational Algebra & Calculus
Relational Algebra & CalculusAbdullah Khosa
 
Relational algebra
Relational algebraRelational algebra
Relational algebrashynajain
 
Relational algebra in dbms
Relational algebra in dbmsRelational algebra in dbms
Relational algebra in dbmsshekhar1991
 
5 the relational algebra and calculus
5 the relational algebra and calculus5 the relational algebra and calculus
5 the relational algebra and calculusKumar
 
Ch6 formal relational query languages
Ch6 formal relational query languages Ch6 formal relational query languages
Ch6 formal relational query languages uoitc
 
Additional Relational Algebra Operations
Additional Relational Algebra OperationsAdditional Relational Algebra Operations
Additional Relational Algebra OperationsA. S. M. Shafi
 
Presentation on dbms(relational calculus)
Presentation on dbms(relational calculus)Presentation on dbms(relational calculus)
Presentation on dbms(relational calculus)yourbookworldanil
 
Relational algebra ppt
Relational algebra pptRelational algebra ppt
Relational algebra pptGirdharRatne
 
Relations & functions
Relations & functionsRelations & functions
Relations & functionsindu thakur
 

La actualidad más candente (18)

Relational Algebra
Relational AlgebraRelational Algebra
Relational Algebra
 
Relational Algebra Introduction
Relational Algebra IntroductionRelational Algebra Introduction
Relational Algebra Introduction
 
Relational algebra-and-relational-calculus
Relational algebra-and-relational-calculusRelational algebra-and-relational-calculus
Relational algebra-and-relational-calculus
 
Relational algebra (basics)
Relational algebra (basics)Relational algebra (basics)
Relational algebra (basics)
 
Dbms ii mca-ch5-ch6-relational algebra-2013
Dbms ii mca-ch5-ch6-relational algebra-2013Dbms ii mca-ch5-ch6-relational algebra-2013
Dbms ii mca-ch5-ch6-relational algebra-2013
 
Relational Algebra & Calculus
Relational Algebra & CalculusRelational Algebra & Calculus
Relational Algebra & Calculus
 
Lec02
Lec02Lec02
Lec02
 
Relational algebra
Relational algebraRelational algebra
Relational algebra
 
Relational algebra in dbms
Relational algebra in dbmsRelational algebra in dbms
Relational algebra in dbms
 
5 the relational algebra and calculus
5 the relational algebra and calculus5 the relational algebra and calculus
5 the relational algebra and calculus
 
Ch6 formal relational query languages
Ch6 formal relational query languages Ch6 formal relational query languages
Ch6 formal relational query languages
 
1643 y є r relational calculus-1
1643 y є r  relational calculus-11643 y є r  relational calculus-1
1643 y є r relational calculus-1
 
Dbms relational model
Dbms relational modelDbms relational model
Dbms relational model
 
Additional Relational Algebra Operations
Additional Relational Algebra OperationsAdditional Relational Algebra Operations
Additional Relational Algebra Operations
 
Relational Algebra
Relational AlgebraRelational Algebra
Relational Algebra
 
Presentation on dbms(relational calculus)
Presentation on dbms(relational calculus)Presentation on dbms(relational calculus)
Presentation on dbms(relational calculus)
 
Relational algebra ppt
Relational algebra pptRelational algebra ppt
Relational algebra ppt
 
Relations & functions
Relations & functionsRelations & functions
Relations & functions
 

Similar a Relational Algebra Guide for Database Systems

Module 2-2.ppt
Module 2-2.pptModule 2-2.ppt
Module 2-2.pptShylaja40
 
Score Week 5 Correlation and RegressionCorrelation and Regres.docx
Score Week 5 Correlation and RegressionCorrelation and Regres.docxScore Week 5 Correlation and RegressionCorrelation and Regres.docx
Score Week 5 Correlation and RegressionCorrelation and Regres.docxkenjordan97598
 
Relational Algebra Ch6 (Navathe 4th edition)/ Ch7 (Navathe 3rd edition)
Relational Algebra Ch6 (Navathe 4th edition)/ Ch7 (Navathe 3rd edition)Relational Algebra Ch6 (Navathe 4th edition)/ Ch7 (Navathe 3rd edition)
Relational Algebra Ch6 (Navathe 4th edition)/ Ch7 (Navathe 3rd edition)Raj vardhan
 
Chapter – 5 Relational Algebra.pdf
Chapter – 5 Relational Algebra.pdfChapter – 5 Relational Algebra.pdf
Chapter – 5 Relational Algebra.pdfTamiratDejene1
 
Unit-II DBMS presentation for students.pdf
Unit-II DBMS presentation for students.pdfUnit-II DBMS presentation for students.pdf
Unit-II DBMS presentation for students.pdfajajkhan16
 
Data Base Managment system
Data Base Managment systemData Base Managment system
Data Base Managment systembatermkf
 
Data base managment system
Data base managment system Data base managment system
Data base managment system batermkf
 
Query optimization
Query optimizationQuery optimization
Query optimizationNeha Behl
 
E212d9a797dbms chapter3 b.sc2
E212d9a797dbms chapter3 b.sc2E212d9a797dbms chapter3 b.sc2
E212d9a797dbms chapter3 b.sc2Mukund Trivedi
 
E212d9a797dbms chapter3 b.sc2 (2)
E212d9a797dbms chapter3 b.sc2 (2)E212d9a797dbms chapter3 b.sc2 (2)
E212d9a797dbms chapter3 b.sc2 (2)Mukund Trivedi
 
E212d9a797dbms chapter3 b.sc2 (1)
E212d9a797dbms chapter3 b.sc2 (1)E212d9a797dbms chapter3 b.sc2 (1)
E212d9a797dbms chapter3 b.sc2 (1)Mukund Trivedi
 
DBMS _Relational model
DBMS _Relational modelDBMS _Relational model
DBMS _Relational modelAzizul Mamun
 
Predicting US house prices using Multiple Linear Regression in R
Predicting US house prices using Multiple Linear Regression in RPredicting US house prices using Multiple Linear Regression in R
Predicting US house prices using Multiple Linear Regression in RSotiris Baratsas
 
Relational operation final
Relational operation finalRelational operation final
Relational operation finalStudent
 
5th chapter Relational algebra.pptx
5th chapter Relational algebra.pptx5th chapter Relational algebra.pptx
5th chapter Relational algebra.pptxkavitha623544
 

Similar a Relational Algebra Guide for Database Systems (20)

354 ch6
354 ch6354 ch6
354 ch6
 
Module 2-2.ppt
Module 2-2.pptModule 2-2.ppt
Module 2-2.ppt
 
Score Week 5 Correlation and RegressionCorrelation and Regres.docx
Score Week 5 Correlation and RegressionCorrelation and Regres.docxScore Week 5 Correlation and RegressionCorrelation and Regres.docx
Score Week 5 Correlation and RegressionCorrelation and Regres.docx
 
Relational Algebra-23-04-2023.pdf
Relational Algebra-23-04-2023.pdfRelational Algebra-23-04-2023.pdf
Relational Algebra-23-04-2023.pdf
 
Relational Algebra Ch6 (Navathe 4th edition)/ Ch7 (Navathe 3rd edition)
Relational Algebra Ch6 (Navathe 4th edition)/ Ch7 (Navathe 3rd edition)Relational Algebra Ch6 (Navathe 4th edition)/ Ch7 (Navathe 3rd edition)
Relational Algebra Ch6 (Navathe 4th edition)/ Ch7 (Navathe 3rd edition)
 
Chapter – 5 Relational Algebra.pdf
Chapter – 5 Relational Algebra.pdfChapter – 5 Relational Algebra.pdf
Chapter – 5 Relational Algebra.pdf
 
1. linear model, inference, prediction
1. linear model, inference, prediction1. linear model, inference, prediction
1. linear model, inference, prediction
 
Unit-II DBMS presentation for students.pdf
Unit-II DBMS presentation for students.pdfUnit-II DBMS presentation for students.pdf
Unit-II DBMS presentation for students.pdf
 
Data Base Managment system
Data Base Managment systemData Base Managment system
Data Base Managment system
 
Data base managment system
Data base managment system Data base managment system
Data base managment system
 
Query optimization
Query optimizationQuery optimization
Query optimization
 
Chapter6
Chapter6Chapter6
Chapter6
 
E212d9a797dbms chapter3 b.sc2
E212d9a797dbms chapter3 b.sc2E212d9a797dbms chapter3 b.sc2
E212d9a797dbms chapter3 b.sc2
 
E212d9a797dbms chapter3 b.sc2 (2)
E212d9a797dbms chapter3 b.sc2 (2)E212d9a797dbms chapter3 b.sc2 (2)
E212d9a797dbms chapter3 b.sc2 (2)
 
E212d9a797dbms chapter3 b.sc2 (1)
E212d9a797dbms chapter3 b.sc2 (1)E212d9a797dbms chapter3 b.sc2 (1)
E212d9a797dbms chapter3 b.sc2 (1)
 
DBMS _Relational model
DBMS _Relational modelDBMS _Relational model
DBMS _Relational model
 
Predicting US house prices using Multiple Linear Regression in R
Predicting US house prices using Multiple Linear Regression in RPredicting US house prices using Multiple Linear Regression in R
Predicting US house prices using Multiple Linear Regression in R
 
Normal forms
Normal formsNormal forms
Normal forms
 
Relational operation final
Relational operation finalRelational operation final
Relational operation final
 
5th chapter Relational algebra.pptx
5th chapter Relational algebra.pptx5th chapter Relational algebra.pptx
5th chapter Relational algebra.pptx
 

Más de Rushdi Shams

Research Methodology and Tips on Better Research
Research Methodology and Tips on Better ResearchResearch Methodology and Tips on Better Research
Research Methodology and Tips on Better ResearchRushdi Shams
 
Common evaluation measures in NLP and IR
Common evaluation measures in NLP and IRCommon evaluation measures in NLP and IR
Common evaluation measures in NLP and IRRushdi Shams
 
Machine learning with nlp 101
Machine learning with nlp 101Machine learning with nlp 101
Machine learning with nlp 101Rushdi Shams
 
Semi-supervised classification for natural language processing
Semi-supervised classification for natural language processingSemi-supervised classification for natural language processing
Semi-supervised classification for natural language processingRushdi Shams
 
Natural Language Processing: Parsing
Natural Language Processing: ParsingNatural Language Processing: Parsing
Natural Language Processing: ParsingRushdi Shams
 
Types of machine translation
Types of machine translationTypes of machine translation
Types of machine translationRushdi Shams
 
L1 l2 l3 introduction to machine translation
L1 l2 l3  introduction to machine translationL1 l2 l3  introduction to machine translation
L1 l2 l3 introduction to machine translationRushdi Shams
 
Syntax and semantics
Syntax and semanticsSyntax and semantics
Syntax and semanticsRushdi Shams
 
Propositional logic
Propositional logicPropositional logic
Propositional logicRushdi Shams
 
Probabilistic logic
Probabilistic logicProbabilistic logic
Probabilistic logicRushdi Shams
 
Knowledge structure
Knowledge structureKnowledge structure
Knowledge structureRushdi Shams
 
Knowledge representation
Knowledge representationKnowledge representation
Knowledge representationRushdi Shams
 
L5 understanding hacking
L5  understanding hackingL5  understanding hacking
L5 understanding hackingRushdi Shams
 
L2 Intrusion Detection System (IDS)
L2  Intrusion Detection System (IDS)L2  Intrusion Detection System (IDS)
L2 Intrusion Detection System (IDS)Rushdi Shams
 

Más de Rushdi Shams (20)

Research Methodology and Tips on Better Research
Research Methodology and Tips on Better ResearchResearch Methodology and Tips on Better Research
Research Methodology and Tips on Better Research
 
Common evaluation measures in NLP and IR
Common evaluation measures in NLP and IRCommon evaluation measures in NLP and IR
Common evaluation measures in NLP and IR
 
Machine learning with nlp 101
Machine learning with nlp 101Machine learning with nlp 101
Machine learning with nlp 101
 
Semi-supervised classification for natural language processing
Semi-supervised classification for natural language processingSemi-supervised classification for natural language processing
Semi-supervised classification for natural language processing
 
Natural Language Processing: Parsing
Natural Language Processing: ParsingNatural Language Processing: Parsing
Natural Language Processing: Parsing
 
Types of machine translation
Types of machine translationTypes of machine translation
Types of machine translation
 
L1 l2 l3 introduction to machine translation
L1 l2 l3  introduction to machine translationL1 l2 l3  introduction to machine translation
L1 l2 l3 introduction to machine translation
 
Syntax and semantics
Syntax and semanticsSyntax and semantics
Syntax and semantics
 
Propositional logic
Propositional logicPropositional logic
Propositional logic
 
Probabilistic logic
Probabilistic logicProbabilistic logic
Probabilistic logic
 
L15 fuzzy logic
L15  fuzzy logicL15  fuzzy logic
L15 fuzzy logic
 
Knowledge structure
Knowledge structureKnowledge structure
Knowledge structure
 
Knowledge representation
Knowledge representationKnowledge representation
Knowledge representation
 
First order logic
First order logicFirst order logic
First order logic
 
Belief function
Belief functionBelief function
Belief function
 
L5 understanding hacking
L5  understanding hackingL5  understanding hacking
L5 understanding hacking
 
L4 vpn
L4  vpnL4  vpn
L4 vpn
 
L3 defense
L3  defenseL3  defense
L3 defense
 
L2 Intrusion Detection System (IDS)
L2  Intrusion Detection System (IDS)L2  Intrusion Detection System (IDS)
L2 Intrusion Detection System (IDS)
 
L1 phishing
L1  phishingL1  phishing
L1 phishing
 

Último

Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsRoshan Dwivedi
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 

Último (20)

Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 

Relational Algebra Guide for Database Systems

  • 1. Rushdi Shams, Dept of CSE, KUET 1 Database SystemsDatabase Systems Relational AlgebraRelational Algebra Version 1.0Version 1.0
  • 2. 2Rushdi Shams, Dept of CSE, KUET What is Relational AlgebraWhat is Relational Algebra  It is a collection of operations on relationsIt is a collection of operations on relations  If you apply relational algebra on some relations,If you apply relational algebra on some relations, the result will be another relationthe result will be another relation  You can say a relation- a table.You can say a relation- a table.  So, operation on tables=another tableSo, operation on tables=another table  That is what relational algebra does!That is what relational algebra does!
  • 3. 3Rushdi Shams, Dept of CSE, KUET Memory RefreshmentMemory Refreshment  A relation is a set of attributes with values for eachA relation is a set of attributes with values for each attribute such that:attribute such that:  Each attribute value must be a single value onlyEach attribute value must be a single value only (atomic).(atomic).  All values for a given attribute must be of the same typeAll values for a given attribute must be of the same type (or domain).(or domain).  Each attribute name must be unique.Each attribute name must be unique.  The order of attributes is insignificantThe order of attributes is insignificant  No two rows (tuples) in a relation can be identical.No two rows (tuples) in a relation can be identical.  The order of the rows (tuples) is insignificant.The order of the rows (tuples) is insignificant.
  • 4. 4Rushdi Shams, Dept of CSE, KUET 2 types of operation2 types of operation 1.1. Set theory operations:Set theory operations: Union, Intersection, Difference and CartesianUnion, Intersection, Difference and Cartesian product.product. 2.2. Specific Relational Operations:Specific Relational Operations: Selection, Projection, Join, DivisionSelection, Projection, Join, Division We will take a look into set theory operations,We will take a look into set theory operations, and then we will dig into specific relationaland then we will dig into specific relational operationsoperations
  • 5. 5Rushdi Shams, Dept of CSE, KUET Our example tablesOur example tables  There are 2 tables- R andThere are 2 tables- R and S, we will play withS, we will play with them!!them!!
  • 6. 6Rushdi Shams, Dept of CSE, KUET UnionUnion  RUS means, a unionRUS means, a union operation on R and Soperation on R and S that will produce anotherthat will produce another table which will have alltable which will have all the rows of R and Sthe rows of R and S except the duplicatesexcept the duplicates  There are 2 Sally in RThere are 2 Sally in R and S, so, just take 1and S, so, just take 1 from themfrom them
  • 7. 7Rushdi Shams, Dept of CSE, KUET DifferenceDifference  R-S means, a differenceR-S means, a difference operation on R and Soperation on R and S that will produce anotherthat will produce another table which will have alltable which will have all the rows which is in Rthe rows which is in R but not in S.but not in S.  As Sally is in R and in S,As Sally is in R and in S, so, she has been omitted.so, she has been omitted.
  • 8. 8Rushdi Shams, Dept of CSE, KUET IntersectionIntersection  R∩S means, anR∩S means, an intersection operation onintersection operation on R and S that will produceR and S that will produce another table which willanother table which will have all the rows whichhave all the rows which are in both R and S.are in both R and S.  As Sally is in R and in S,As Sally is in R and in S, so, she has been selected.so, she has been selected.
  • 9. 9Rushdi Shams, Dept of CSE, KUET More UnionMore Union
  • 10. 10Rushdi Shams, Dept of CSE, KUET More IntersectionMore Intersection
  • 11. 11Rushdi Shams, Dept of CSE, KUET More DifferenceMore Difference
  • 12. 12Rushdi Shams, Dept of CSE, KUET ConstraintsConstraints  You see tables, and start performing set theoryYou see tables, and start performing set theory operations on them- MISTAKE!! You shouldoperations on them- MISTAKE!! You should remember the following constraints before youremember the following constraints before you perform set theory operations on tables.perform set theory operations on tables. 1.1. Attributes of relations needAttributes of relations need notnot be identical tobe identical to perform union, intersection and differenceperform union, intersection and difference operations.operations. 2.2. However, theyHowever, they must havemust have the same number ofthe same number of attributes and the domains for correspondingattributes and the domains for corresponding attributes must be identical.attributes must be identical.
  • 13. 13Rushdi Shams, Dept of CSE, KUET Other things to rememberOther things to remember  Union, Intersection and difference operatorsUnion, Intersection and difference operators may only be applied to Union Compatiblemay only be applied to Union Compatible relations.relations.  Union and Intersection are commutativeUnion and Intersection are commutative operationsoperations RUS= SURRUS= SUR R∩S = S∩RR∩S = S∩R  Difference operation isDifference operation is NOTNOT commutative.commutative. R - S not equal S - RR - S not equal S - R
  • 14. 14Rushdi Shams, Dept of CSE, KUET Class PerformanceClass Performance  Find out RUTFind out RUT  Find out R∩TFind out R∩T  Find out R-TFind out R-T  Find out T-RFind out T-R
  • 15. 15Rushdi Shams, Dept of CSE, KUET Cartesian ProductCartesian Product  It provides all the combinations of rows from two tables.It provides all the combinations of rows from two tables.  If A and B are 2 tables and A has 3 rows and B has 4 rows, then AIf A and B are 2 tables and A has 3 rows and B has 4 rows, then A×B is-×B is- A1-B1A1-B1 A1-B2A1-B2 A1-B3A1-B3 A1-B4A1-B4 A2-B1A2-B1 A2-B2A2-B2 A2-B3A2-B3 A2-B4A2-B4 A3-B1A3-B1 A3-B2A3-B2 A3-B3A3-B3 A3-B4A3-B4
  • 16. 16Rushdi Shams, Dept of CSE, KUET Cartesian Product (continued)Cartesian Product (continued)
  • 17. 17Rushdi Shams, Dept of CSE, KUET More ProductMore Product
  • 18. 18Rushdi Shams, Dept of CSE, KUET SelectionSelection  Selection and Projection areSelection and Projection are unaryunary operators.operators.  The selection operator isThe selection operator is sigma (sigma (σσ))  The selection operation acts like a filter on aThe selection operation acts like a filter on a relation byrelation by returning only a certain number ofreturning only a certain number of rows.rows.  TheThe resulting relation will have the sameresulting relation will have the same number of attributesnumber of attributes as the original relation.as the original relation.
  • 19. 19Rushdi Shams, Dept of CSE, KUET Selection (continued)Selection (continued)  The resulting relationThe resulting relation maymay have fewer rows thanhave fewer rows than the original relation.the original relation.  The rows to be returned are dependent on aThe rows to be returned are dependent on a condition that is part of the selection operator.condition that is part of the selection operator.  σσCC (R) Returns only those rows in R that satisfy(R) Returns only those rows in R that satisfy conditioncondition CC  A condition C can be made up of anyA condition C can be made up of any combination of comparison or logical operatorscombination of comparison or logical operators that operate on the attributes of R.that operate on the attributes of R.
  • 20. 20Rushdi Shams, Dept of CSE, KUET Selection ExampleSelection Example  We have a table named EMP.We have a table named EMP.  We will play with this table named EMP for a while!We will play with this table named EMP for a while!
  • 21. 21Rushdi Shams, Dept of CSE, KUET Selection Example (continued)Selection Example (continued)  Select only those Employees in the CS departmentSelect only those Employees in the CS department σσ Dept=‘CS’Dept=‘CS’ (EMP)(EMP)
  • 22. 22Rushdi Shams, Dept of CSE, KUET Selection Example (continued)Selection Example (continued)  Select only those Employees with last name Smith whoSelect only those Employees with last name Smith who are assistant professorsare assistant professors σσ Name=‘Smith’Name=‘Smith’ ΛΛ Rank=‘Assistant’Rank=‘Assistant’ (EMP)(EMP)
  • 23. 23Rushdi Shams, Dept of CSE, KUET Selection Example (continued)Selection Example (continued)  Select only those Employees who are either AssistantSelect only those Employees who are either Assistant Professors or in the Economics departmentProfessors or in the Economics department σσ Dept =‘Econ’V Rank=‘Assistant’Dept =‘Econ’V Rank=‘Assistant’ (EMP)(EMP)
  • 24. 24Rushdi Shams, Dept of CSE, KUET Selection Example (continued)Selection Example (continued)  Select only those Employees who are not in the CSSelect only those Employees who are not in the CS department or Adjunctsdepartment or Adjuncts σσ ¬¬(Dept=‘CS’ V Rank=‘Adjunct’)(Dept=‘CS’ V Rank=‘Adjunct’) (EMP)(EMP)
  • 25. 25Rushdi Shams, Dept of CSE, KUET More SelectionMore Selection
  • 26. 26Rushdi Shams, Dept of CSE, KUET Class PerformanceClass Performance  σσ(Rank='Adjunct'(Rank='Adjunct' ΛΛ Dept ='CS')Dept ='CS') (EMP)(EMP)  σσ Rank= 'Associate'Rank= 'Associate' ΛΛ Dept ='CS'Dept ='CS' EMP )EMP )  σσ Dept= 'CS'Dept= 'CS' VV Rank='Associate'Rank='Associate' EMP )EMP )  σσ Age>26Age>26 (R U S)(R U S)
  • 27. 27Rushdi Shams, Dept of CSE, KUET ProjectionProjection  Projection is also aProjection is also a UnaryUnary operator.operator.  The Projection operator isThe Projection operator is pi (pi (ππ))  Projection limits the columns that will be returned from theProjection limits the columns that will be returned from the original relation.original relation.  The general syntax is:The general syntax is: ππ attributesattributes RR Where attributes is the list of attributes to be displayed and R isWhere attributes is the list of attributes to be displayed and R is the relation.the relation.  The resulting table will have the same number of rows as theThe resulting table will have the same number of rows as the original relation (unless there are duplicate rows produced).original relation (unless there are duplicate rows produced).  The number of columns of the resulting relation may be equal toThe number of columns of the resulting relation may be equal to or less than that of the original relation.or less than that of the original relation.
  • 28. 28Rushdi Shams, Dept of CSE, KUET Projection ExampleProjection Example  Project only the names and departments of theProject only the names and departments of the employeesemployees ππ name,deptname,dept (EMP)(EMP)
  • 29. 29Rushdi Shams, Dept of CSE, KUET Combining selection &Combining selection & projectionprojection  Show the names of all employees working in the CSShow the names of all employees working in the CS departmentdepartment ππ namename ((σσ Dept ='CS'Dept ='CS' (EMP) )(EMP) )
  • 30. 30Rushdi Shams, Dept of CSE, KUET Combining selection & projectionCombining selection & projection (continued)(continued)  Show the name and rank of those Employees who areShow the name and rank of those Employees who are not in the CS department or Adjunctsnot in the CS department or Adjuncts ππ name, rankname, rank (( σσ ¬¬(Rank='Adjunct' V Dept='CS')(Rank='Adjunct' V Dept='CS') (EMP) )(EMP) )
  • 31. 31Rushdi Shams, Dept of CSE, KUET More ProjectionMore Projection
  • 32. 32Rushdi Shams, Dept of CSE, KUET Class PerformanceClass Performance  ππ name, rankname,rank (( σσ ¬¬(Rank='Adjunct'(Rank='Adjunct' ΛΛ Dept ='CS')Dept ='CS') (EMP) )(EMP) )  ππ fname,agefname,age ((σσ Age>22Age>22 (R U S) )(R U S) )  σσ office>300office>300 (( ππ name,rankname,rank (EMP))(EMP))
  • 33. 33Rushdi Shams, Dept of CSE, KUET Aggregate FunctionsAggregate Functions  We can also apply Aggregate functions to columnsWe can also apply Aggregate functions to columns and rowsand rows 1.1. SUMSUM 2.2. MINIMUMMINIMUM 3.3. MAXIMUMMAXIMUM 4.4. AVERAGE, MEAN, MEDIANAVERAGE, MEAN, MEDIAN 5.5. COUNTCOUNT  Aggregate functions are sometimes written using theAggregate functions are sometimes written using the Projection operator or theProjection operator or the Tau character (Tau character (ττ))
  • 34. 34Rushdi Shams, Dept of CSE, KUET Aggregate Functions (continued)Aggregate Functions (continued)  Find the minimum SalaryFind the minimum Salary ττ MIN(salary)MIN(salary) (EMP)(EMP)  Find the average SalaryFind the average Salary ττ AVG(salaryAVG(salary) (EMP)) (EMP)  Count the number of employees in the CS departmentCount the number of employees in the CS department ττ COUNT(name)COUNT(name) (( σσ Dept='CS'Dept='CS' (EMP) )(EMP) )  Find the total payroll for the Economics departmentFind the total payroll for the Economics department ττ SUM(salary)SUM(salary) ((σσ Dept='Econ'Dept='Econ' (EMP) )(EMP) )
  • 35. 35Rushdi Shams, Dept of CSE, KUET JoinJoin  Join operations bring together two tables and combineJoin operations bring together two tables and combine their rows and columns in a specific fashion.their rows and columns in a specific fashion.  The generic join operator called theThe generic join operator called the Theta JoinTheta Join isis  It takes as arguments the columns from the two tablesIt takes as arguments the columns from the two tables that are to be joined.that are to be joined.  The join condition can beThe join condition can be  When the join condition operator is  When the join condition operator is   ==   then we call  then we call this an Equijointhis an Equijoin  Note that the columns in common are repeated.Note that the columns in common are repeated.
  • 36. 36Rushdi Shams, Dept of CSE, KUET Join (continued)Join (continued)  Now, we have two tables Emp and depart.Now, we have two tables Emp and depart.  We will play with these 2 tables for a while!We will play with these 2 tables for a while!
  • 37. 37Rushdi Shams, Dept of CSE, KUET Join (continued)Join (continued)  Find all information on every employee including theirFind all information on every employee including their department infodepartment info EMPEMP emp.Dept=depart.Deptemp.Dept=depart.Dept DEPARTDEPART
  • 38. 38Rushdi Shams, Dept of CSE, KUET Join (continued)Join (continued)  Take a look, one table has 5 rows, the other has 4 rows,Take a look, one table has 5 rows, the other has 4 rows, the resultant has 5 rows!the resultant has 5 rows!
  • 39. 39Rushdi Shams, Dept of CSE, KUET Join (continued)Join (continued)  Find all information on every employee including departmentFind all information on every employee including department info where the employee works in an office numbered less thaninfo where the employee works in an office numbered less than the department main officethe department main office EMPEMP (emp.office<depart.mainoffice)(emp.office<depart.mainoffice) ΛΛ (emp.dept =depart.dept)(emp.dept =depart.dept) DEPARTDEPART
  • 40. 40Rushdi Shams, Dept of CSE, KUET Natural JoinNatural Join  Notice in the generic (Theta) join operation, anyNotice in the generic (Theta) join operation, any attributes in common (such as dept above) areattributes in common (such as dept above) are repeated.repeated.  The Natural Join operation removes theseThe Natural Join operation removes these duplicate attributes.duplicate attributes.  The natural join operator is:The natural join operator is: **  We can also assume usingWe can also assume using ** that the jointhat the join condition will becondition will be == on the two attributes inon the two attributes in common.common.
  • 41. 41Rushdi Shams, Dept of CSE, KUET Natural Join (continued)Natural Join (continued)  Emp * departEmp * depart  There are 2 Dept columns, one is omitted!There are 2 Dept columns, one is omitted!
  • 42. 42Rushdi Shams, Dept of CSE, KUET Outer JoinOuter Join  In the Join operations so far, only those tuples from bothIn the Join operations so far, only those tuples from both relations that satisfy the join condition are included in therelations that satisfy the join condition are included in the output relation.output relation.  The Outer join includes other rows as well according to a fewThe Outer join includes other rows as well according to a few rules.rules.  Three types of outer joins:Three types of outer joins: 1.1. Left Outer Join includes all rows in the left hand relation andLeft Outer Join includes all rows in the left hand relation and includes only those matching rows from the right hand relation.includes only those matching rows from the right hand relation. 2.2. Right Outer Join includes all rows in the right hand relationRight Outer Join includes all rows in the right hand relation and includes only those matching rows from the left handand includes only those matching rows from the left hand relation.relation. 3.3. Full Outer Join includes all rows in the left hand relation andFull Outer Join includes all rows in the left hand relation and from the right hand relation.from the right hand relation.
  • 43. 43Rushdi Shams, Dept of CSE, KUET Outer Join (continued)Outer Join (continued)  Now we have another 2 tables.Now we have another 2 tables.  We will play with them for next few slides!We will play with them for next few slides!
  • 44. 44Rushdi Shams, Dept of CSE, KUET Left Outer JoinLeft Outer Join PEOPLEPEOPLE people.food=menu.foodpeople.food=menu.food MENUMENU
  • 45. 45Rushdi Shams, Dept of CSE, KUET Right Outer JoinRight Outer Join PEOPLEPEOPLE people.food=menu.foodpeople.food=menu.food MENUMENU
  • 46. 46Rushdi Shams, Dept of CSE, KUET Full Outer JoinFull Outer Join PEOPLEPEOPLE people.food=menu.foodpeople.food=menu.food MENUMENU
  • 47. 47Rushdi Shams, Dept of CSE, KUET Outer UnionOuter Union  In this case, just go from row of table 1 to row of table 2.In this case, just go from row of table 1 to row of table 2.  Take common columns just once.Take common columns just once.  if matching criteria don’t match, then put NULL, else include inif matching criteria don’t match, then put NULL, else include in the tablethe table  Repeat the same but this time go from row of table 2 to row ofRepeat the same but this time go from row of table 2 to row of table 1table 1
  • 48. 48Rushdi Shams, Dept of CSE, KUET ReferencesReferences  Database Management Systems by Prof.Database Management Systems by Prof. Holowczak, Zicklin School of Business,Holowczak, Zicklin School of Business, Baruch College, City University of NewBaruch College, City University of New YorkYork  Database Systems: Design, ImplementationDatabase Systems: Design, Implementation & Management by Rob & Coronel, 6& Management by Rob & Coronel, 6thth EditionEdition