Relational Algebra
SQL AND RELATIONAL ALGEBRA DEFINITION WITH EXAMPLE
Relational Algebra is a theoretical query language used to query and manipulate data in a relational database. It uses mathematical set operations to operate on relations (tables).
It forms the foundation of SQL and other relational query languages.
Basic Operations in Relational Algebra
Operation |
Symbol |
Description |
Selection |
σ (sigma) |
Filters rows (horizontal) |
Projection |
π (pi) |
Selects columns (vertical) |
Union |
∪ |
Combines two relations |
Set Difference |
− |
Returns tuples in one relation but not in another |
Cartesian Product |
× |
Combines all tuples (cross product) |
Rename |
ρ (rho) |
Renames relation or attributes |
Derived (Compound) Operations
Operation |
Symbol |
Description |
Join |
⋈ |
Combines related tuples from two relations |
Intersection |
∩ |
Common tuples in both relations |
Division |
÷ |
Special case query like "for all" |
1. Student
ID |
Name |
Dept |
1 |
Alice |
CS |
2 |
Bob |
IT |
3 |
Carol |
CS |
2. Department
Dept |
HOD |
CS |
Dr. Rao |
IT |
Dr. Das |
1. Selection (σ): Get all CS students
σ Dept = 'CS' (Student)
Result:
ID |
Name |
Dept |
1 |
Alice |
CS |
3 |
Carol |
CS |
2. Projection (π): Get student names
π Name (Student)
Result:
Name |
Alice |
Bob |
Carol |
3. Union (∪): Combine two student lists
Student1 and Student2 are two relations with same schema.
Student1 ∪ Student2
Student − σ Dept = 'CS' (Student)
Result:
ID |
Name |
Dept |
2 |
Bob |
IT |
5. Cartesian Product (×)
Student × Department
Each student is paired with each department (not useful alone, but used in joins).
6. Natural Join (⋈): Join Student and Department on Dept
Student ⋈ Department
Result:
ID |
Name |
Dept |
HOD |
1 |
Alice |
CS |
Dr. Rao |
2 |
Bob |
IT |
Dr. Das |
3 |
Carol |
CS |
Dr. Rao |
Operation |
Notation |
SQL Equivalent |
Selection |
σ |
WHERE |
Projection |
π |
SELECT columns |
Cartesian Product |
× |
FROM A, B |
Union |
∪ |
UNION |
Set Difference |
− |
EXCEPT or MINUS |
Join |
⋈ |
JOIN ... ON |
- SELECT (symbol: σ)
Select Operation is used as an expression to choose tuples which meet the selection condition.
Employee
DeptID |
Name |
Salary |
D1 |
Ram |
40000 |
D2 |
Hari |
80000 |
D1 |
Rajat |
60000 |
Question : Select all employees whose salary is greater than 50000.
SQL Query : SELECT * FROM Employee WHERE Salary > 50000;
Relational Algebra:σ salary > 50000 (Employee)
Output :
DeptID |
Name |
Salary |
D2 |
Hari |
80000 |
D1 |
Rajat |
60000 |
Question : Select all employees whose department is D1 and salary is greater than 50000.
SQL Query :SELECT * FROM Employee WHERE Salary > 50000 AND DeptID=”D1”;
Relational Algebra : σ(DeptID = "D1" AND Salary >50000)(Employee)
DeptID |
Name |
Salary |
D1 |
Rajat |
60000 |
- PROJECT (symbol: π)
This operation is used to select a subset of columns from a table.The projection eliminates all attributes of the input relation but those mentioned in the projection list.
Employee
DeptID |
Name |
Salary |
D1 |
Ram |
40000 |
D2 |
Hari |
80000 |
D1 |
Rajat |
60000 |
Question: Display Name and Salary from Employee Table
Relational Query: Π Name, Salary (Employee)
Output:
Name |
Salary |
Ram |
40000 |
Hari |
80000 |
Rajat |
60000 |
Question: Display DeptID from Employee Table
Relational Query: Π DeptID (Employee)
Output:
DeptID |
D1 |
D2 |
Note: Project Operation removes the duplicate values
- RENAME (symbol: ρ)
Rename is a unary operation used for renaming attributes of a relation.
ρ (a/b)R will rename the attribute ‘b’ of relation by ‘a’.
- UNION (∪)
UNION is symbolized by ∪ symbol. It includes all tuples that are in tables A or in B. It also eliminates duplicate tuples. So, set A UNION set B would be expressed as:
A ∪ B
For a union operation to be valid, the following conditions must hold –
- R and S must have the same number of attributes.
- Attribute domains need to be compatible.
- Duplicate tuples should be automatically removed.
Table A |
|
Table B |
||
ID |
Name |
|
ID |
Name |
1 |
A |
|
1 |
A |
1 |
B |
|
1 |
C |
SQL Query : SELECT ID, Name FROM Table A UNION SELECT ID, Name FROM Table B;
Relational Algebra:
Π ID, Name(Table A) ∪ Π ID, Name (Table B)
Output |
|
ID |
Name |
1 |
A |
1 |
B |
1 |
C |
- INTERSECTION ( ∩)
An intersection is defined by the symbol ∩
Defines a relation consisting of a set of all tuples that are in both A and B.
Table A |
|
Table B |
||
ID |
Name |
|
ID |
Name |
1 |
A |
|
1 |
A |
1 |
B |
|
1 |
C |
SQL Query :SELECT ID, Name FROM Table A INTERSECT SELECT ID, Name FROM Table B;
Relational Algebra: Π ID, Name(Table A)∩ Π ID, Name(Table B)
Output |
|
ID |
Name |
1 |
A |
- DIFFERENCE (-)
The result of A – B, is a relation which includes all tuples that are in A but not in B.
- The attribute name of A has to match with the attribute name in B.
- The two-operand relations A and B should be either compatible or Union compatible.
- It should be a defined relation consisting of the tuples that are in relation A, but not in B.
Table A |
|
Table B |
||
ID |
Name |
|
ID |
Name |
1 |
A |
|
1 |
A |
1 |
B |
|
1 |
C |
SQL Query : SELECT ID, Name FROM Table A MINUS SELECT ID, Name FROM Table A; Relational Algebra:Π ID, Name(Table A)- Π ID, Name(Table B)
Output |
|
ID |
Name |
1 |
B |
- CARTESIAN PRODUCT ( x )
Cartesian Product associates every tuple of R1 with every tuple of R2.
R1 X R2= All possible pairing. We don't need any common attributes in both relation.
EMPLOYEE
EID |
ENAME |
EDEPT |
1 |
Sam |
A |
2 |
Hari |
C |
3 |
Jai |
B |
DEPARTMENT
DNO |
DNAME |
A |
Marketing |
B |
Sales |
C |
Legal |
EMPLOYEE X DEPARTMENT
EID |
ENAME |
EDEPT |
DNO |
DNAME |
1 |
Sam |
A |
A |
Marketing |
1 |
Sam |
A |
B |
Sales |
1 |
Sam |
A |
C |
Legal |
2 |
Hari |
C |
A |
Marketing |
2 |
Hari |
C |
B |
Sales |
2 |
Hari |
C |
C |
Legal |
3 |
Jai |
B |
A |
Marketing |
3 |
Jai |
B |
B |
Sales |
3 |
Jai |
B |
C |
Legal |
Note : Can you tell me the difference between Union and Join ?
- JOIN
A join is an operation that combines the rows of two or more tables based on related columns.
-
NATURAL JOIN (⋈)
Natural join can only be performed if there is a common attribute (column) between the relations. The name and type of the attribute must be same.
Example
Consider the following two tables
|
|
Num |
Square |
2 |
4 |
3 |
9 |
B |
|
Num |
Cube |
2 |
8 |
3 |
27 |
SQL Query : SELECT * FROM TableA NATURAL JOIN TableB;
Relational Query: σ(A)⋈ Num= Num (B)
Num |
Square |
Cube |
2 |
4 |
8 |
3 |
9 |
27
|
-
Equi Join
Equi Join is a type of Inner join in which we use equivalence(‘=’) condition in the join condition
Employee
Id |
Name |
Dname |
Salary |
1 |
John |
HR |
5000 |
2 |
Jane |
IT |
6000 |
3 |
Bob |
Finance |
7000 |
DEPARTMENT
Did |
Dname |
1 |
HR |
2 |
IT |
3 |
Finance |
SQL QUERY : SELECT Id, Name, Salary, Dname
FROM Employee
INNER JOIN Department
ON Employee.Dname = Department.Dname;
Relational Algebra : Π ID, Name,Salary, Dname(Employee) ⨝ Employee.Dname = Department.Dname (DEPARTMENT)
-
OUTER JOIN
In an outer join, along with tuples that satisfy the matching criteria, we also include some or all tuples that do not match the criteria.
c.i) Left Outer Join(A ⟕ B)
In the left outer join, operation allows keeping all tuple in the left relation. However, if there is no matching tuple is found in right relation, then the attributes of right relation in the join result are filled with null values.
CUSTOMERS Table:
CID |
NAME |
CITY |
AGE |
SALARY |
1 |
Jay |
N |
30 |
2000.00 |
2 |
Shyam |
L |
25 |
1500.00 |
3 |
Mohanl |
C |
23 |
2000.00 |
4 |
Ram |
H |
25 |
6500.00 |
5 |
Lok |
P |
27 |
8500.00 |
ORDERS Table:
ORDER_ID |
DATE |
CID |
AMOUNT |
100 |
2019-10-18 00:00:00 |
1 |
3000 |
101 |
2019-10-18 00:00:00 |
2 |
1500 |
102 |
2019-11-10 00:00:00 |
3 |
1560 |
103 |
2018-05-10 00:00:00 |
4 |
2060 |
SQLQUERY:
SELECT ID, NAME, AMOUNT, DATE FROM CUSTOMERS LEFT JOIN ORDERS ON CUSTOMERS.CID = ORDERS.CID;
Π ID,Name, Salary (CUSTOMERS ⟕ customers.cid=orders.cid ORDERS)
Output:
CID |
NAME |
AMOUNT |
DATE |
1 |
Jay |
3000 |
2019-10-18 00:00:00 |
2 |
Shyam |
1500 |
2019-10-18 00:00:00 |
3 |
Mohanl |
1560 |
2019-11-10 00:00:00 |
4 |
Ram |
2060 |
2018-05-10 00:00:00 |
5 |
Lok |
NULL |
NULL |
c.ii.) Right Outer Join
Right Outer Join It is also called a right join. This type of outer join retrieves all records from the right table and retrieves matching records from the left table. And for the record which doesn’t lies in Left table will be marked as NULL in result Set.
Consider Above Example:
SQL Query:
SELECT CID, NAME, AMOUNT, DATE FROM CUSTOMERS RIGHT JOIN ORDERS ON CUSTOMERS.CID = ORDERS.CID;
Π ID,Name, Salary (CUSTOMERS ⟖customers.cid=orders.cid ORDERS)
c.iii) Full Join (⟗)
FULL JOIN creates the result-set by combining results of both LEFT JOIN and RIGHT JOIN. The result-set will contain all the rows from both tables. For the rows for which there is no matching, the result-set will contain NULL values.
SQL Query:
SELECT Student.NAME,Course.COURSE_ID FROM Student FULL JOIN Course ON Course.ROLL_NO = Student.ROLL_NO;
Π Name,Course_ID(STUDENT ⟗Course.ROLL_NO=Student.ROLL_NO (COURSE)
Output :
Name |
Course |
Harsh |
1 |
Pratik |
2 |
Riyanka |
2 |
Deep |
3 |
Sapthari |
1 |
Dhanraj |
Null |
Rohit |
Null |
Niraj |
Null |
Null |
4 |
Null |
5 |
Null |
4 |
Properties of Relational Algebra:
Closure: The result of a relational algebra operation is also a relation.
Commutativity: The order of operands does not affect the result of ∪ and ∩ operations.
Associativity: The grouping of operands does not affect the result of ∪ and ∩ operations.
Distributivity: ∩ distributes over ∪, and vice versa.