Relational Algebra & Calculus
Relational Algebra & Calculus
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
Join Operations
• JOINs can be used to combine tables
• Join is a derivative of Cartesian product.
19
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
20
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
Note: General power of 1st order predicate calculus – allow more flexible expression
formats
Tuple Relational Calculus
• A logical language with variables ranging over tuples:
{ T | Cond }
– Return all tuples T that satisfy the condition Cond.
• { T | R(T) }: returns all tuples T such that T is a tuple in relation R.
• { T.name | F ACULT Y (T) AND T.DeptId = ‘CS’ }.
returns the values of name field of all faculty tuples with the value
’CS’ in their department id field.
– The variable T is said to be free since it is not bound by a quantifier
(for all, exists).
– The result of this statement is a relation (or a set of tuples) that
correspond to all possible ways to satisfy this statement.
– Find all possible instances of T that make this statement true.
TUPLE RELATIONAL CALCULUS
TUPLE RELATIONAL CALCULUS
JOINS IN THE TUPLE RELATIONAL
CALCULUS
Domain Relational Calculus
• Query has the form:
x1, x2,..., xn | p x1, x2,..., xn
x1, x2,..., xn
Answer includes all tuples that
make the formula p x1, x2,..., xn
be true.