Relational Algebra
Relational Algebra
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)
1. Union (∪): Combines the results of two relations and removes duplicates.
4. Join (⨝): Combines tuples from two relations based on a common attribute.
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.
the age is greater than 30. The ∃DeptID part ensures that the tuple
o This expression retrieves all names and ages of employees where
| Name |
|-------|
| Carol |
| David |
Example 2: Join Operation
Question: Given the following relations:
Employees:
| 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:
o Employees ⨝ Employees.DepartmentID =
Departments.DepartmentID Departments
2. Projection (π): Retrieve the Name and DeptName columns.
| 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:
o Managers ∪ Contractors
| 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
| 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
| 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:
| 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:
3. Projection:
| 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)
| 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:
| 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:
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.