0% found this document useful (0 votes)
42 views16 pages

Relational Algebra

Uploaded by

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

Relational Algebra

Uploaded by

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

 Selection: Given a relation Employees(EmpID, Name, Age,

Department), write a relational algebra expression to find all employees


in the "Sales" department.
 Projection: From the relation Products(ProductID, ProductName,
Price), write a relational algebra expression to get a list of all unique
product names.
 Union: Given two relations A(ID, Name) and B(ID, Name), write a
relational algebra expression to combine both relations, ensuring no
duplicates.
 Intersection: For two relations StudentsA(StudentID, Name) and
StudentsB(StudentID, Name), write an expression to find students who
are in both relations.
 Difference: Given relations Customers1(ID, Name) and
Customers2(ID, Name), write an expression to find customers who are
in Customers1 but not in Customers2.
 Cartesian Product: Given relations Orders(OrderID, CustomerID) and
Customers(CustomerID, Name), write an expression to produce a
cartesian product of both relations.
 Join: For relations Authors(AuthorID, Name) and Books(BookID, Title,
AuthorID), write a relational algebra expression to find all books along
with their authors.
 Rename: Given a relation Students(StudentID, Name), write a
relational algebra expression to rename it to Scholars(ID, FullName).
 Natural Join: For two relations Employees(EmpID, Name, DeptID) and
Departments(DeptID, DeptName), write an expression to find the names
of employees along with their department names.
 Aggregation: Given a relation Sales(OrderID, ProductID, Quantity),
write a relational algebra expression to find the total quantity sold for
each product.
Relational Algebra
Relational Algebra is a procedural query language that provides a set of
operations to manipulate and retrieve data from relational databases. These
operations can be classified into unary and binary operators, and set operations.
Unary Operators
1. Selection (σ): Filters rows based on a specified condition.
o Example: σ_age>30(Employees)

 This operation selects employees whose age is greater than


30.
2. Projection (π): Retrieves specific columns from a relation.
o Example: π_name, age(Employees)

 This operation retrieves only the name and age columns from
the Employees relation.
3. Renaming (ρ): Renames the relation or its attributes.
o Example: ρ(EmpName/Name, Employees)

 This operation renames the Name attribute in Employees to


EmpName.
Binary Operators

1. Union (∪): Combines the results of two relations and removes duplicates.

o Example: Employees ∪ Managers

 This operation combines employees and managers, removing


duplicate tuples.
2. Difference (-): Retrieves tuples present in the first relation but not in the
second.
o Example: Employees - Managers

 This operation finds employees who are not managers.


3. Cartesian Product (×): Combines tuples from two relations into a single
relation.
o Example: Employees × Departments

 This operation creates a relation where each tuple is a


combination of an employee and a department.

4. Join (⨝): Combines tuples from two relations based on a common attribute.

o Example: Employees ⨝ DepartmentID=DeptID Departments

 This operation joins employees with their respective


departments based on matching DepartmentID and DeptID.
5. Intersection (∩): Retrieves tuples present in both relations.
o Example: Employees ∩ Managers

 This operation finds tuples that are present in both


Employees and Managers.
6. Division (÷): Used to find tuples in one relation that are related to all
tuples in another relation.
o Example: Projects ÷ Skills

 This operation finds projects that require all the skills listed in
Skills.
Relational Calculus
Relational Calculus is a non-procedural query language that focuses on what
data to retrieve rather than how to retrieve it. It has two main forms: Tuple-
Oriented and Domain-Oriented.
Tuple-Oriented Relational Calculus
Tuple-Oriented Relational Calculus uses variables that range over tuples. The
result is a set of tuples that satisfy the specified conditions.

 Example: {T | T ∈ Employees ∧ T.age > 30}


o This expression retrieves all tuples T from Employees where the age
is greater than 30.
Domain-Oriented Relational Calculus
Domain-Oriented Relational Calculus uses variables that range over the values of
attributes (domains). The result is a set of tuples representing combinations of
values that satisfy the conditions.

 Example: {<Name, Age> | ∃DeptID (Employees(Name, Age, DeptID) ∧


Age > 30)}

the age is greater than 30. The ∃DeptID part ensures that the tuple
o This expression retrieves all names and ages of employees where

is present in the Employees relation.


Examples of Relational Calculus Operations
1. Tuple-Oriented Example:
o Retrieve names of employees who work in department 10:

 {T.Name | T ∈ Employees ∧ T.DeptID = 10}


2. Domain-Oriented Example:
o Retrieve names and ages of employees who have a certain skill:

 {<Name, Age> | ∃Skill (Employees(Name, Age, Skill) ∧ Skill


= 'Database')}
By understanding and utilizing these operations, you can effectively manipulate
and query relational databases, whether using procedural methods (Relational
Algebra) or declarative methods (Relational Calculus).
Example 1: Basic Selection and Projection
Question: Given the following relations:
 Employees:

| EmpID | Name | Age | DepartmentID |


|-------|--------|-----|--------------|
|1 | Alice | 30 | 101 |
|2 | Bob | 25 | 102 |
|3 | Carol | 35 | 101 |
|4 | David | 40 | 103 |
Retrieve the names of employees who are older than 30.
Answer: To retrieve the names of employees older than 30, use the selection
and projection operations:
1. Selection (σ): Filter rows where Age > 30.
o σ_Age > 30(Employees)

2. Projection (π): Retrieve only the Name column.


o π_Name(σ_Age > 30(Employees))

So, the result is:

| Name |
|-------|
| Carol |
| David |
Example 2: Join Operation
Question: Given the following relations:
 Employees:

| EmpID | Name | DepartmentID |


|-------|--------|--------------|
|1 | Alice | 101 |
|2 | Bob | 102 |
|3 | Carol | 101 |
 Departments:

| DepartmentID | DeptName |
|--------------|-----------|
| 101 | HR |
| 102 | IT |
Retrieve the names of employees along with their department names.
Answer: To retrieve the names of employees along with their department
names, perform a join operation on the DepartmentID attribute:

1. Join (⨝): Combine Employees with Departments on DepartmentID.

o Employees ⨝ Employees.DepartmentID =
Departments.DepartmentID Departments
2. Projection (π): Retrieve the Name and DeptName columns.

o π_Name, DeptName(Employees ⨝ Employees.DepartmentID =


Departments.DepartmentID Departments)
So, the result is:

| Name | DeptName |
|-------|----------|
| Alice | HR |
| Bob | IT |
| Carol | HR |
Example 3: Union Operation
Question: Given the following relations:
 Managers:

| EmpID | Name |
|-------|-------|
|1 | Alice |
|2 | Bob |
 Contractors:

| EmpID | Name |
|-------|-------|
|3 | Carol |
|4 | David |
Retrieve a list of all unique employees, either managers or contractors.
Answer: To retrieve a list of all unique employees, use the union operation:

1. Union (∪): Combine Managers and Contractors.

o Managers ∪ Contractors

So, the result is:

| EmpID | Name |
|-------|-------|
|1 | Alice |
|2 | Bob |
|3 | Carol |
|4 | David |
Example 4: Difference Operation
Question: Given the following relations:
 AllEmployees:

| EmpID | Name |
|-------|-------|
|1 | Alice |
|2 | Bob |
|3 | Carol |
|4 | David |
 Managers:

| EmpID | Name |
|-------|-------|
|1 | Alice |
|2 | Bob |
Retrieve a list of employees who are not managers.
Answer: To retrieve employees who are not managers, use the difference
operation:
1. Difference (-): Find tuples in AllEmployees but not in Managers.
o AllEmployees - Managers

So, the result is:

| EmpID | Name |
|-------|-------|
|3 | Carol |
|4 | David |
Example 5: Division Operation
Question: Given the following relations:
 Projects:
sql

| ProjectID | Skill |
|-----------|---------|
|1 | SQL |
|1 | Java |
|2 | SQL |
|2 | Python |
 RequiredSkills:
sql

| Skill |
|---------|
| SQL |
| Java |
Retrieve the IDs of projects that require all the skills listed in RequiredSkills.
Answer: To find projects that require all the listed skills, use the division
operation:
1. Division (÷): Divide Projects by RequiredSkills.
o Projects ÷ RequiredSkills

So, the result is:

| ProjectID |
|-----------|
|1 |
In this example, only Project 1 requires all the skills listed in RequiredSkills (SQL
and Java).
These examples should give you a solid understanding of how to apply relational
algebra operations to manipulate and query relational databases.

Question 1: Self-Join
Question: Given the following Employees relation:
sql
| EmpID | Name | ManagerID |
|-------|-------|-----------|
|1 | Alice | NULL |
|2 | Bob | 1 |
|3 | Carol | 1 |
|4 | David | 2 |
|5 | Eve | 2 |
Perform a self-join on the Employees relation to find the names of employees and
their managers. The result should include the employee name and their
manager’s name.
Answer: To perform a self-join to find employees and their managers, join the
Employees relation with itself on the ManagerID attribute. Then project the
names of employees and their managers:
1. Self-Join:

o Employees1 ⨝ Employees1.ManagerID = Employees2.EmpID


Employees2
2. Projection:

o π_Employees1.Name, Employees2.Name (Employees1 ⨝


Employees1.ManagerID = Employees2.EmpID Employees2)
So, the result is:

| EmployeeName | ManagerName |
|--------------|-------------|
| Bob | Alice |
| Carol | Alice |
| David | Bob |
| Eve | Bob |
Question 2: Join with Multiple Relations
Question: Given the following relations:
 Employees:
| EmpID | Name | DeptID |
|-------|-------|--------|
|1 | Alice | 101 |
|2 | Bob | 102 |
|3 | Carol | 103 |
 Departments:

| DeptID | DeptName |
|--------|----------|
| 101 | HR |
| 102 | IT |
| 104 | Finance |
 Projects:

| ProjectID | DeptID |
|-----------|--------|
|1 | 101 |
|2 | 102 |
|3 | 104 |
Retrieve the names of employees who work in departments that are also
assigned projects.
Answer: To retrieve employee names who work in departments that have
projects, join all three relations as follows:
1. Join Employees and Departments:

o Employees ⨝ Employees.DeptID = Departments.DeptID Departments

2. Join the result with Projects:

o Employees ⨝ Employees.DeptID = Projects.DeptID Projects

3. Projection:

o π_Name (Employees ⨝ Employees.DeptID = Projects.DeptID Projects)

So, the result is:


| Name |
|-------|
| Alice |
| Bob |
Carol is excluded because the department 103 is not associated with any project.
Question 3: Complex Join with Aggregation
Question: Given the following relations:
 Sales:

| SaleID | EmpID | Amount |


|--------|-------|--------|
|1 |1 | 100 |
|2 |2 | 150 |
|3 |1 | 200 |
|4 |3 | 300 |
 Employees:

| EmpID | Name |
|-------|-------|
|1 | Alice |
|2 | Bob |
|3 | Carol |
Find the names of employees who have made sales totaling more than 200.
Answer: To find the names of employees who have made sales totaling more
than 200, follow these steps:
1. Aggregate Sales:
o Sales_Aggregated ← π_EmpID, SUM(Amount) AS TotalAmount (Sales
GROUP BY EmpID)
2. Selection:
o σ_TotalAmount > 200 (Sales_Aggregated)

3. Join with Employees:

o Sales_Aggregated ⨝ Sales_Aggregated.EmpID = Employees.EmpID


Employees
4. Projection:

o π_Name (σ_TotalAmount > 200 (Sales_Aggregated ⨝


Sales_Aggregated.EmpID = Employees.EmpID Employees))
So, the result is:

| Name |
|-------|
| Alice |
| Carol |
Alice and Carol are the employees with total sales greater than 200.
Question 4: Multi-Attribute Join
Question: Given the following relations:
 Orders:

| OrderID | CustomerID | ProductID |


|---------|------------|-----------|
|1 | 101 | 201 |
|2 | 102 | 202 |
|3 | 103 | 203 |
 Products:

| ProductID | ProductName |
|-----------|-------------|
| 201 | Laptop |
| 202 | Phone |
| 204 | Tablet |
 Customers:

| CustomerID | CustomerName |
|------------|--------------|
| 101 | John |
| 102 | Jane |
| 104 | Bob |
Retrieve the names of customers who have ordered products that are available
in the Products table.
Answer: To find the names of customers who have ordered products listed in the
Products table:
1. Join Orders with Products:

o Orders ⨝ Orders.ProductID = Products.ProductID Products

2. Join the result with Customers:

o (Orders ⨝ Orders.ProductID = Products.ProductID Products) ⨝


Orders.CustomerID = Customers.CustomerID Customers
3. Projection:

π_CustomerName ((Orders ⨝ Orders.ProductID = Products.ProductID


Products) ⨝ Orders.CustomerID = Customers.CustomerID
o

Customers)
So, the result is:

| CustomerName |
|--------------|
| John |
| Jane |
Bob is excluded because his customer ID does not match any in the Orders table.
These questions test your ability to work with different types of joins and
understand how to combine and filter data from multiple relations.

You might also like