Lecture 9: BCSE302L - DBMS: Relational Algebra
Lecture 9: BCSE302L - DBMS: Relational Algebra
Lecture 9: BCSE302L - DBMS: Relational Algebra
RELATIONAL ALGEBRA
Translating SQL to Relational Algebra
Translating SQL to Relational Algebra
1. Select Operation:
The select operation selects tuples that satisfy a given predicate.
It is denoted by sigma (σ).
Notation: σ p(r)
Where:
σ is used for selection prediction
r is used for relation
p is used as a propositional logic formula which may use connectors like: AND OR
and NOT. These relational can use as relational operators like =, ≠, ≥, <, >, ≤.
Example :
σ BRANCH_NAME="perryride" (LOAN)
Example
Conditions can be combined together using ^ (AND) and v (OR). For example, all
employees in department 1 called `Smith':
SELECTdepno = 1 ^ surname = `Smith'(employee) becomes σdepno = 1 ^ surname = `Smith'(employee)
2. Project Operation:
This operation shows the list of those attributes that we wish to appear in the
result. Rest of the attributes are eliminated from the table.
It is denoted by ∏.
Notation: ∏ A1, A2, An (r)
Where
A1, A2, A3 is used as an attribute name of relation r.
Example:
∏ NAME, CITY (CUSTOMER)
3. Union Operation:
Suppose there are two tuples R and S. The union operation contains all the
tuples that are either in R or S or both in R & S.
It eliminates the duplicate tuples. It is denoted by ∪.
Notation: R ∪ S
A union operation must hold the following condition:
R and S must have the attribute of the same number.
Duplicate tuples are eliminated automatically.
Example:
∏ CUSTOMER_NAME (BORROW) ∪ ∏ CUSTOMER_NAME (DEPOSITOR)
4. Set Intersection:
Suppose there are two tuples R and S. The set intersection operation
contains all tuples that are in both R & S.
It is denoted by intersection ∩.
Notation: R ∩ S
Example: Using the above DEPOSITOR table and BORROW table
Input:
∏ CUSTOMER_NAME (BORROW) ∩ ∏ CUSTOMER_NAME (DEPOSITOR)
Example:
∏ CUSTOMER_NAME (BORROW) ∪ ∏ CUSTOMER_NAME (DEPOSITOR)
5. Set Difference:
Suppose there are two tuples R and S. The set intersection operation contains
all tuples that are in R but not in S.
It is denoted by intersection minus (-).
Notation: R - S
The Cartesian product is used to combine each row in one table with each row
in the other table. It is also known as a cross product.
It is denoted by X.
Notation: E X D
EMPLOYEE X DEPARTMENT
7. Rename Operation:
SQL Query
SELECT Select-list
FROM R1, . . . , R2 T2, . . .
WHERE Where-condition
Sample Relational Schema
Select-From-Where No Subqueries
Select-From-Where No Subqueries
Join Operation
Join Operation
Join Operation
Join Operation
Union, Intersection, Difference
Translation is straightforward
UNION →R1∪R2
EXCEPT →R1−R2
Except Operation
Group by and Having Clause
Correlated subquery
First subquery
Second suquery
final
Relational Algebra Symbols
Thank you