Operations in Relational Algebra

Union in Relational Algebra

The union operation in Relational Algebra merges two relations, eliminating any duplicate rows. It is denoted as R ∪ S, where R and S are two relations with identical schemas. This operation is widely used in SQL queries and other database management systems to combine data from multiple sources.

Let Consider the original Table

Original Table: Customers

IDNameCity
1John DoeNew York
2Sarah SmithLos Angeles
3Michael JohnsonChicago
4Emily BrownHouston
5David WilsonMiami
6Olivia JonesSan Francisco

Original Table: Suppliers

IDNameCity
101XYZ CompanySeattle
102ABC CorporationDallas
103LMN EnterprisesAtlanta
104PQR Inc.Boston
105DEF IndustriesPhiladelphia
106GHI Co.Phoenix

The syntax for the union operation
  • It denoted as R ∪ S
  • where R and S represent two relations with the same schemas.
  • The resulting relation maintains the same schema as R and S, ensuring consistency in the structure.
  • To better illustrate the union operation, let's consider two relations, Customers and Suppliers, with the following schemas: Customers  Suppliers.
                        Customers (ID, Name, City)
                        Suppliers (ID, Name, City)

Customers _Suppliers

IDNameCity
1John DoeNew York
2Sarah SmithLos Angeles
3Michael JohnsonChicago
4Emily BrownHouston
5David WilsonMiami
6Olivia JonesSan Francisco
1XYZ CompanySeattle
2ABC CorporationDallas
3LMN EnterprisesAtlanta
4PQR Inc.Boston
5DEF IndustriesPhiladelphia
6GHI Co.Phoenix

Intersection in Relational Algebra

The intersection operation identifies common rows between two relations. It is represented as R ∩ S, where R and S are relations with the same schemas. This operation is beneficial in extracting meaningful information from overlapping data.

The syntax for the Intersection operation

  • To perform an intersection operation, we use the symbol "∩"
  • The syntax R ∩ S,
  • Where R and S represent two relations with identical schemas.
  • It's important to note that the resulting relation will also have the same schema as R and S, ensuring consistency in the structure.
  • Let's consider two relations, Customers and Suppliers, with the following schemas:
                 Student (ID, Name, Department, Age, Hobby)

                  Hobby (ID, Name, Hobby, Age)
  • Suppose we want to find all entities that exist as both Students and Hobby.
  • By utilizing the intersection operation on these two relations,
  • we can identify the common rows that satisfy this condition.

Student table:

IDNAMEDepartmentAGEHOBBY
1BirhanuIT30Movie
2AtlawMathematics29Football
3ChalaPhysics25Karate
4AlemayehuSport25Karate
5DawitComputer Science33Football
6AnbikoAgriculture30Reading


Hobby table:

IDNAMEHOBBYAGE
1BogaleDriving30
2AtlawFootball29
3SolomonGame25
4KaltamoFootball20
5SelamuFootball26
6DawitFootball33

Student ∩ Hobby

IDNAMEHOBBYAGE
2AtlawFootball29
5DawitFootball33

Set Difference in Relational Algebra

Set Difference allows identification of rows present in one relation but absent in another. It uses the syntax R - S, where R and S are relations with the same schema. This operation aids in comparing two sets of data and extracting unique elements.

Syntax for SET DIFFERENCE operation

  • To perform the SET Difference operation, we use the "-" symbol
  • The syntax is R - S,
  • Where R and S represent two relations with the same schema.
  • We obtain a resulting relation that retains the schema of R and S.
  • Example
  • Let's consider two relations, Student and Hobby, with the following schemas:
                  Student (ID, Name, Department, Age, Hobby)

                  Hobby (ID, Name, Hobby, Age)
  • Suppose our objective is to find all Student who have not Hobby. In other words, we want to identify the rows in the student relation that do not exist in the Hobby relation.
  • To accomplish this, we can utilize the SET Difference operation.

Student - Hobie

IDNAMEDepartmentAGEHOBBY
1BirhanuIT30Movie
3ChalaPhysics25Karate
4AlemayehuSport25Karate
6AnbikoAgriculture30Reading

Cartesian Product (Cross Product) in Relational Algebra

The Cartesian Product combines all possible pairs of rows from two relations. It is represented as R x S, where R and S denote relations with different schemas. This operation generates comprehensive result sets and is often used in SQL queries by employing the CROSS JOIN option.

Syntax
  • The syntax for the Cartesian Product operation is represented as R x S
  • where R and S denote two relations with different schemas.
  • The resulting relation has a schema that is the union of the schemas of R and S, incorporating all the attributes from both relations
  • Example
  • Let's consider two relations, Customers and Orders, with the following schemas:

            Customers (ID, Name)
            Orders (OrderID, CustomerID, Product)
  • Suppose our goal is to find all possible combinations of customers and orders.
  • We can accomplish this by employing the Cartesian Product operation, which combines each row from the Customers relation with every row from the Orders relation.
  • Here's an example of the Cartesian Product operation applied to these two relations:

multiplication (cross join) of the Customers and Orders tables

Customers x Orders

IDNameOrderIDCustomerIDProduct
101John Smith201101Laptop
101John Smith202103Headphones
101John Smith203102Smartphone
101John Smith204105Tablet
101John Smith205101Smartwatch
101John Smith206104Camera
102Emily Davis201101Laptop
102Emily Davis202103Headphones
102Emily Davis203102Smartphone
102Emily Davis204105Tablet
102Emily Davis205101Smartwatch
102Emily Davis206104Camera
103Alex Johnson201101Laptop
103Alex Johnson202103Headphones
103Alex Johnson203102Smartphone
103Alex Johnson204105Tablet
103Alex Johnson205101Smartwatch
103Alex Johnson206104Camera
104Sarah Brown201101Laptop
104Sarah Brown202103Headphones
104Sarah Brown203102Smartphone
104Sarah Brown204105Tablet
104Sarah Brown205101Smartwatch
104Sarah Brown206104Camera
105Michael Wilson201101Laptop
105Michael Wilson202103Headphones
105Michael Wilson203102Smartphone
105Michael Wilson204105Tablet
105Michael Wilson205101Smartwatch
105Michael Wilson206104Camera
106Olivia Lee201101Laptop
106Olivia Lee202103Headphones
106Olivia Lee203102Smartphone
106Olivia Lee204105Tablet
106Olivia Lee205101Smartwatch
106Olivia Lee206104Camera

Selection in Relational Algebra

Selection, also known as the Restriction operation, filters rows based on specified conditions. It uses the syntax σ<condition>(R), where R is a relation, and <condition> is a logical expression evaluating to true or false.

Syntax

  • The syntax for Selection is: σ<condition>(R)
  • R is a relation and <condition> is a logical expression that evaluates to either true or false.
  • The resulting relation contains only the rows from R that satisfy the <condition>.
Example
  • Let's consider a relation, Employees, with the following schema:
              Employees (ID, Name, Department, Salary)

Employees Table:

IDNameDepartmentSalary
201Emma JohnsonIT$62,000
202Liam AndersonSales$58,500
203Sophia ClarkHR$67,200
204Noah BakerMarketing$59,800
205Olivia GarciaSales$52,500
206Ethan MartinezSales$33,700
  • Suppose we want to find all employees who belong to the Sales department and have a salary greater than $50,000.
  • We can use the Selection operation to filter the rows based on this condition.
  • Here's an example of the Selection operation applied to this relation:

σ(Department = 'Sales' AND Salary > $50,000)(Employees)

DNameDepartmentSalary
202Liam AndersonSales$58,500
205Olivia GarciaSales$52,500

Projection in Relational Algebra

Projection selects certain columns (attributes) from a relation while discarding others. It employs the syntax π<list of attributes>(R), where R is a relation, and <list of attributes> is a comma-separated list of attribute names.

Syntax
  • The syntax for Projection is: Ï€<list of attributes>(R)
  • R is a relation and <list of attributes> is a comma-separated list of attribute names.
  • The resulting relation contains only the selected attributes from R.
  • Example
  • Let's consider a relation, Employees, with the following schema:

                    Employees (ID, Name, Department, Salary)

Employees Table:

IDNameDepartmentSalary
101John SmithIT$60,000
102Emily DavisSales$55,000
103Alex JohnsonHR$65,000
104Sarah BrownMarketing$58,000
105Michael WilsonFinance$70,000
106Olivia LeeOperations$52,000
  • Suppose we want to find the names and salaries of all employees.
  • We can use the Projection operation to select the Name and Salary columns from the Employees relation.
  • Here's an example of the Projection operation applied to this relation:
                Ï€ (Name, Salary) (Employees):

NameSalary
John Smith$60,000
Emily Davis$55,000
Alex Johnson$65,000
Sarah Brown$58,000
Michael Wilson$70,000
Olivia Lee$52,000

Join Operation

Join combines related tuples from two relations into single "longer" tuples. It is denoted by the symbol "⋈" and is crucial for processing relationships among relations in a database.

Types of join Operation

EMPLOYEE

EMP_NOEMP_NAMECITYEXPERIENCE
Emp-1LelisaAssosa6
Emp-2BirhanuAddis Ababa8
Emp-3DawitNekemte9
Emp-4DebeloHawasa10

DEPARTMENT

DEP_NOD_NAMEEMP_NO
Dep-01ChemistryEmp-4
Dep-02EconomicsEmp-2
Dep-03Computer ScienceEmp-3

1. EQUIJOIN Operation

The EQUIJOIN operation is a type of join in relational databases that combines rows from two tables based on a common column where values are equal. Specifically, an EQUIJOIN is synonymous with an INNER JOIN, where the join condition uses the equality operator (=) to match values in the specified columns of the participating tables.

When performing an EQUIJOIN (or INNER JOIN) between two tables, the resulting table will include only the rows that have matching values in the specified columns from both tables.

This operation retrieves rows from both tables where the values in the specified columns are equal. Rows that do not have a matching value in the specified columns between the tables will not be included in the resulting output.

EMPLOYEE ⨝ (EMPLOYEE.EMP_NO = DEPARTMENT.EMP_NO) DEPARTMENT

Here's the result of performing an EQUIJOIN operation between the EMPLOYEE and DEPARTMENT tables based on the condition EMPLOYEE.EMP_NO = DEPARTMENT.EMP_NO:

EMP_NOEMP_NAMECITYEXPERIENCEDEP_NOD_NAME
Emp-2BirhanuAddis Ababa8Dep-02Economics
Emp-3DawitNekemte9Dep-03Computer Science
Emp-4DebeloHawasa10Dep-01Chemistry


NATURAL JOIN (⋈ ) or ( )in Relational Algebra

NATURAL JOIN combines two relations based on common attributes between them, ensuring the result contains only one copy of each common attribute. It is denoted by a multiplication symbol (*) OR and requires common attributes to have the same name in both relations.

EMPLOYEEDEPARTMENT

Here's the result of performing a NATURAL JOIN operation between the EMPLOYEE and DEPARTMENT tables:

EMP_NOEMP_NAMECITYEXPERIENCEDEP_NOD_NAME
Emp-2BirhanuAddis Ababa8Dep-02Economics
Emp-3DawitNekemte9Dep-03Computer Science
Emp-4DebeloHawasa10Dep-01Chemistry

Outer join Operation

OUTER JOIN operation is a type of join that combines tuples from two relations (tables) based on a common attribute and includes unmatched tuples by using special symbols. There are three types of OUTER JOINs:

  • LEFT OUTER JOIN (⟕):

  • Retrieves all records from the left table and matching records from the right table. If there's no match in the right table, NULL values are included for those columns.

Here's the result of performing a LEFT JOIN operation between the EMPLOYEE and DEPARTMENT tables based on the condition EMPLOYEE.E_NO = DEPARTMENT.E_NO:

EMP_NOEMP_NAMECITYEXPERIENCEDEP_NOD_NAME
Emp-1LelisaAssosa6NULLNULL
Emp-2BirhanuAddis Ababa8Dep-02Economics
Emp-3DawitNekemte9Dep-03Computer Science
Emp-4DebeloHawasa10Dep-01Chemistry
  • RIGHT OUTER JOIN (⟖): 

  • Opposite of the LEFT OUTER JOIN, it fetches all records from the right table and matching records from the left table. If there's no match in the left table, NULL values are included for those columns.


  • Here's the result of performing a RIGHT OUTER JOIN operation between the EMPLOYEE and DEPARTMENT tables based on the condition EMPLOYEE.EMP_NO = DEPARTMENT.EMP_NO:

    EMP_NOEMP_NAMECITYEXPERIENCEDEP_NOD_NAME
    Emp-4DebeloHawasa10Dep-01Chemistry
    Emp-2BirhanuAddis Ababa8Dep-02Economics
    Emp-3DawitNekemte9Dep-03Computer Science
  • FULL OUTER JOIN (⟗):

  • Combines all records from both tables. It includes matching rows and adds NULL values for columns where there are no matches in either table.


  • Here's the result of performing a FULL OUTER JOIN operation between the EMPLOYEE and DEPARTMENT tables based on the condition EMPLOYEE.EMP_NO = DEPARTMENT.EMP_NO:

    EMP_NOEMP_NAMECITYEXPERIENCEDEP_NOD_NAME
    Emp-1LelisaAssosa6
    Emp-2BirhanuAddis Ababa8Dep-02Economics
    Emp-3DawitNekemte9Dep-03Computer Science
    Emp-4DebeloHawasa10Dep-01Chemistry


Post a Comment

0 Comments