Relational Algebra
Relational Algebra
Introduction
• Relational algebra
– Basic set of operations for the relational model
– More operational (procedural), very useful for
representing execution plans.
• Relational calculus
– Higher-level declarative language for specifying
relational queries
– Lets users describe what they want, rather than
how to compute it: Non-operational, declarative.
Relational Algebra
• Relational algebra operations work on one or more relations to define
another relation without changing the original relations.
• Basic operations:
– Selection ( ) Selects a subset of rows from relation.
– Projection (
) Deletes unwanted columns from relation.
– Cross-product ( ) Allows us to combine two relations.
– Set-difference (
– Union (
) Tuples in reln. 1, but not in reln. 2.
) Tuples in reln. 1 and in reln. 2.
• Additional operations:
– Intersection, join, division, renaming: Not essential, but (very!) useful.
Relational Algebra Operations
Relational Algebra Operations
Selection (or Restriction)
• predicate (R)
– Works on a single relation R and defines a relation that contains only
those tuples (rows) of R that satisfy the specified condition (predicate).
– Selects rows that satisfy selection condition.
– No duplicates in result!
– Schema of result identical to schema of input relation.
– Selection is distributive over binary operators
– Selection is commutative
7
Example - Selection Restriction)
• List all staff with a salary greater than £10,000.
Note: real systems typically don’t do duplicate elimination unless the user
explicitly asks for it (by DISTINCT). Why not?
Example - Projection
• Produce a list of salaries for all staff, showing only staffNo, fName,
lName, and salary details.
city(Branch) city(PropertyForRent)
Set Difference
• R–S
– Defines a relation consisting of the tuples that are
in relation R, but not in S.
– R and S must be union-compatible.
Example - Set Difference
• List all cities where there is a branch office but no properties for rent.
city(Branch) – city(PropertyForRent)
Intersection
• RS
– Defines a relation consisting of the set of all tuples
that are in both R and S.
– R and S must be union-compatible.
city(Branch) city(PropertyForRent)
Cartesian product
• RXS
– Defines a relation that is the concatenation of
every tuple of relation “R with every tuple of
relation S”
Example - Cartesian product
• List the names and comments of all clients who have viewed a property for
rent.
(clientNo, fName, lName(Client)) X (clientNo, propertyNo, comment
(Viewing))
18
Pearson Education © 2009
Example - Cartesian product and
Selection
• Use selection operation to extract those tuples where Client.clientNo =
Viewing.clientNo.
sClient.clientNo = Viewing.clientNo((ÕclientNo, fName, lName(Client))
(ÕclientNo, propertyNo, comment(Viewing)))
Cartesian product and Selection can be reduced to a single operation called a Join.
19
Pearson Education © 2009
Join Operations
• JOINs can be used to combine tables
• Join is a derivative of Cartesian product.
20
Pearson Education © 2009
Join Operations
• Various forms of join operation
– JOIN (Inner Join) : Return rows when there is at least
one match in both tables
– LEFT JOIN(Left Outer Join): Return all rows from the
left table, even if there are no matches in the right table
– RIGHT JOIN(Right Outer Join): Return all rows from the
right table, even if there are no matches in the left table
– FULL JOIN: Return rows when there is a match in one
of the tables
21
Pearson Education © 2009
SQL INNER JOIN Syntax
SELECT column_name(s) FROM table_name1 INNER JOIN table_name2
ON
table_name1.column_name=table_name2.column_name
OTHER REFRENCES
• https://cs.uwaterloo.ca/~tozsu/courses/CS348/notes/4b-calculus-handout-notes.pdf
• http://www.ccs.neu.edu/home/kathleen/classes/cs3200/4-RAAndRC.pdf
• https://www2.cs.sfu.ca/CourseCentral/354/louie/Chap3_practice.pdf
• http://www.nyu.edu/classes/jcf/CSCI-GA.2433-001_fa11/slides/session5/RelationalAlgebra-
RelationalCalculus-SQL.pdf
• https://blog.inf.ed.ac.uk/da15/files/2015/01/inf1-da-15-t3.pdf
• DatabaseSystemConceptsbySudarshan,Korth(McGraw-HillEducation)
• FundamentalsofDatabaseSystemByElmasari&Navathe-PearsonEducation
• http://ecomputernotes.com/database-system/rdbms
• https://www.tutorialspoint.com/sql/sql-rdbms-concepts.htm
• https://www.studytonight.com/dbms/rdbms-concept
SUGGESTED BOOK REFERENCES
• Ramez Elmasri and Shamkant B. Navathe,“Fundamentals of Database System”,
The Benjamin / Cummings Publishing Co.
• Korth and Silberschatz Abraham, “Database SystemConcepts”, McGraw Hall.
• Pratt,”DBMS”, Cengage Learning.