0% found this document useful (0 votes)
12 views24 pages

Relational Algebra

Relational Algebra is a procedural query language used to access and manipulate data in database tables through operations such as selection, projection, union, intersection, set difference, Cartesian product, and renaming. It allows users to perform various queries and operations on relations, including filtering and combining data from multiple tables. Additionally, it includes concepts like joins (natural, theta, equi, and outer joins) and aggregate functions for summarizing data.

Uploaded by

nacap91209
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views24 pages

Relational Algebra

Relational Algebra is a procedural query language used to access and manipulate data in database tables through operations such as selection, projection, union, intersection, set difference, Cartesian product, and renaming. It allows users to perform various queries and operations on relations, including filtering and combining data from multiple tables. Additionally, it includes concepts like joins (natural, theta, equi, and outer joins) and aggregate functions for summarizing data.

Uploaded by

nacap91209
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Relational Algebra

What is Relational Algebra?


Every database management system must define a query language to allow users to access the data
stored in the database. Relational Algebra is a procedural query language used to query the database
tables to access data in different ways.
The primary operations that we can perform using relational algebra are:
• Select
• Project
• Union
• Set Different
• Cartesian product
• Rename
EMployess
Name Null? Type
------------------ ---------------------- ----------------------------
EMPLOYEE_ID NOT NULL NUMBER(6)
FIRST_NAME VARCHAR2(20)
LAST_NAME NOT NULL VARCHAR2(25)
EMAIL NOT NULL VARCHAR2(25)
PHONE_NUMBER VARCHAR2(20)
HIRE_DATE NOT NULL DATE
JOB_ID NOT NULL VARCHAR2(10)
SALARY NUMBER(8,2)
COMMISSION_PCT NUMBER(2,2)
MANAGER_ID NUMBER(6)
DEPARTMENT_ID NUMBER(4)
Selection Operator-
•Selection Operator (σ) is a unary operator in relational algebra that performs a selection operation.
•It selects those rows or tuples from the relation that satisfies the selection condition.
Notation − σp(r) (Greek letter sigma () to denote selection)
Where σ stands for selection predicate and r stands for relation. p is prepositional logic
formula which may use connectors like and, or, and not. These terms may use relational
operators like − =, ≠, ≥, < , >, ≤.

Syntax-
σ<selection_condition>(R)
Examples-
Select tuples from a relation “Employees” where job_id is “ST_MAN”
Select * from employees
Where job_id=‘ST_MAN’;
But in relational algebra
σjob_id = “ST_MAN” (employees)
Select tuples from a relation “Employees” where job_id is “ST_MAN” and
department_id is 90

Select * from employees


Where job_id=‘ST_MAN’
And department_id=90;

But in relational algebra


σjob_id = “ST_MAN” ∧ department_id = “90” (employees)

Select tuples from a relation “Employees” where job_id is “ST_MAN” and


department_id is 90 or salary less than 7000.
σjob_id = “ST_MAN” ∧ department_id = “90” ∨ salary <”7000″ (employees)
Project Operation The project operation is a unary operation that returns its argument
relation, with certain attributes left out. Since a relation is a set, any duplicate rows are
eliminated. Projection is denoted by the uppercase Greek letter pi (π).
It projects column(s) that satisfy a given predicate.
Notation − ∏A1, A2, An(r)
Where A1, A2 , An are attribute names of relation r.
Duplicate rows are automatically eliminated, as relation is a set.
For example −
∏employee_id, first_name (Employees)

SQL Query:
Select employee_id, frist_name from employees;
Select tuples from a relation “Employees” where job_id is “ST_MAN” and department_id
is 90
∏employee_id, first_name, last_name, job_id (σjob_id = “ST_MAN” ∧ department_id = “90” (employees))

Select employee_id, first_name, last_name, job_id from employees


Where job_id=‘ST_MAN’
And department_id=90;
A = {1, 3, 5, 7, 9}
B = {3, 6, 9, 12, 15}
A∪B = {1, 3, 5, 6, 7, 9, 12, 15}

A = {2, 3, 5, 7, 11}
B = {1, 3, 5, 7, 9, 11}
A∩B = {3, 5, 7, 11}

A = {2, 3, 5, 7, 11}
B = {1, 3, 5, 7, 9, 11}
A-B = {2}
Union Operator (∪)-
Union
Let R and S be two relations. In relational algebra, an operator used to merge
(append) two tables into a new table, dropping the
Then- duplicate rows. The tables must be union
compatible (Two or more tables that have the same
R ∪ S is the set of all tuples belonging to either R or S or both. number of columns and the corresponding columns
In R ∪ S, duplicates are automatically removed. have compatible domains)

PhD_student Id Name
employee
Id Name 101 ravi
id name
101 ravi 103 rohit
103 rohit ∪ 101 ram
102 ram
109 ashi 104 mohit
102 ram
201 Ayush 109 ashi
104 mohit
201 Ayush

∏ id, first_name (Student) U ∏ id, first_name (Employees)


Intersection
An intersection is defined by the symbol ∩
A∩B
Defines a relation consisting of a set of all tuple that are in both A and
B. However, A and B must be union-compatible.

PhD_student employee
Id Name
id name
101 ravi Id Name
103 rohit
ꓵ 101 ram
102 ram
109 ashi
102 ram
201 Ayush
104 mohit

∏ id, first_name (Student) ꓵ ∏ id, first_name (Employees)


Set Difference (-)
- Symbol denotes it. 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 defined relation consisting of the tuples that are in relation A,
but not in B.

PhD_student employee
Id Name
Id Name
id name 101 ravi
101 ravi
103 rohit ∪ 101 ram 103 rohit
109 ashi 104 mohit
102 ram
201 Ayush
104 mohit

∏ id, first_name (Student) - ∏ id, first_name (Employees)


The Cartesian-Product Operation
The Cartesian-product operation, denoted by a cross (×), allows us to combine information from any two relations. We
write the Cartesian product of relations r1 and r2 as r1 × r2.
A = {7, 8} and B = {2, 4, 6}
Then,
Col_A Col_B Col_X Col_Y
A × B = {(7, 2); (7, 4); (7, 6); (8, 2); (8, 4); (8,6)} ----- ------ ------ ------
R AA 100 XX 99
S
AA 100 YY 11
Col_A Col_B Col_X Col_Y AA 100 ZZ 101
----- ------ BB 200 XX 99
AA 100 ----- ----- BB 200 YY 11
BB 200 (×) XX 99 BB 200 ZZ 101
CC 300 YY 11 CC 300 XX 99
ZZ 101 CC 300 YY 11
CC 300 ZZ 101
Employees
Departments
Name Type
------------------ ---------------------------- Name Type
EMPLOYEE_ID NUMBER(6) ------------------------------ -------------------------
FIRST_NAME VARCHAR2(20)
DEPARTMENT_ID NUMBER(4)
LAST_NAME VARCHAR2(25)
EMAIL VARCHAR2(25) DEPARTMENT_NAME VARCHAR2(30)
PHONE_NUMBER VARCHAR2(20) MANAGER_ID NUMBER(6)
HIRE_DATE DATE
LOCATION_ID NUMBER(4)
JOB_ID VARCHAR2(10)
SALARY NUMBER(8,2)
COMMISSION_PCT NUMBER(2,2)
MANAGER_ID NUMBER(6)
DEPARTMENT_ID NUMBER(4)

∏employee_id, job_id,depart_name (σjob_id = “ST_MAN” ∧ department_id = “90” (employees x departments))

∏employee_id, job_id,department_name (σjob_id = “ST_MAN” ∧ department_id = “90” ∧ employees.department_id=depaerment.department_id (employees x


departments))
Rename (ρ) Example
Lets say we have a table Departments, we are fetching departments names and we are renaming the resulted
relation to DEPT_NAMES. Denoted by the lowercase Greek letter rho (ρ).

ρ(Dept_NAMES, ∏ (Departments_Name) (CUSTOMER))


The natural join is a binary operation that allows us to combine certain selections and a Cartesian
product into one operation. It is denoted by the join symbol

Cross join performed a cartesian product among the rows of two different tables. where the columns
name may or may not be matched......but in natural join its mandatory that in order to perform join
operation columns name of two tables must be matched

∏employee_id, job_id,department_name (σdepartment.department_id=employees.department_id (employees departments))


Theta Join
Theta Join allows you to merge two tables based on the condition represented by theta. Theta joins work for
all comparison operators. The general case of JOIN operation is called a Theta join. It is denoted by symbol θ

A ⋈θ B

∏employee_id, job_id, depart_name (σ locations.location_id between 1700 and 2400 ∧ department_id >50 (employees ⋈θ departments))

SELECT a.department_id, a.department_name, b.city FROM departments a, locations b


WHERE b.location_id BETWEEN 1700 AND 2400
AND
a.department_id < 50;
EQUI join:
When a theta join uses only equivalence condition, it becomes an equi join.

∏employee_id, job_id, department_name (σ department.department_id=employees.department_id (employees ⋈ departments))

SELECT first_name, job_id, departments.department_id, department_name FROM employees, departments


WHERE employees.department_id = departments.department_id
Outer Join
An outer join doesn't require each record in the two join tables to have a matching record. In this type of join, the
table retains each record even if no other matching record exists.
Three types of Outer Joins are:
• Left Outer Join
• Right Outer Join
• Full Outer Join
LEFT OUTER JOIN

RIGHT OUTER JOIN

FULL OUTER JOIN


Left Outer Join
The LEFT JOIN returns all the rows from the table on the left even if no matching rows have been found in
the table on the right. Where no matching record found in the table on the right, NULL is returned.

A B

∏employee_id, job_id, department_name (σ department.department_id=employees.department_id (employees departments))

SELECT e.last_name, e.department_id, SELECT e.last_name, e.department_id,


d.department_name d.department_name FROM employees e LEFT
FROM employees e, departments d OUTER JOIN departments d
WHERE e.department_id = d.department_id(+); on (e.department_id= d.department_id);

The query in the above example retrieves all rows in the EMPLOYEES table, even if there is no match in the
DEPARTMENTS table.
Right Outer Join:
In the right outer join, operation allows keeping all tuple in the right relation. However, if there is no
matching tuple is found in the left relation, then the attributes of the left relation in the join result are
filled with null values.
A B

∏employee_id, job_id, department_name (σ department.department_id=employees.department_id (employees departments))

SELECT e.last_name, e.department_id, SELECT e.last_name, e.department_id,


d.department_name d.department_name FROM employees e RIGHT
FROM employees e, departments d OUTER JOIN departments d
WHERE e.department_id (+)= d.department_id; on (e.department_id= d.department_id);

The query in the above example retrieves all rows in the DEPARTMENTS table, even if there is no match in the EMPLOYEES
table.
Full Outer Join
In a full outer join, all tuples from both relations are included in the result, irrespective of the
matching condition.

A B

∏employee_id, job_id, department_name (σ department.department_id=employees.department_id (employees departments))

SELECT e.last_name, e.department_id, d.department_name FROM employees e Full


OUTER JOIN departments d
on (e.department_id= d.department_id);
Aggregate functions

Aggregate functions take a collection of values and return a single value as a result. For example, the
aggregate function sum, avg, min, max etc takes a collection of values and returns the single resultant value
of these values. Thus, the function sum applied on the collection:

• Gsum(salary) (Employees) Find the average salary in each department.

Department_idGavg(salary) (Employees)

• Gavg(salary) (Employees)

• Gmin(salary) (Employees)
Gcount-distinct(department_id)(σ salary>6000(Employees))

• Gmax(salary) (Employees)

You might also like