Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
28 views5 pages

Lab Relational Algebra

Uploaded by

JABBAR ALTAF
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)
28 views5 pages

Lab Relational Algebra

Uploaded by

JABBAR ALTAF
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/ 5

Introduction to RelaX and Relational Algebra

RelaX is a declarative query language based on relational algebra, which provides a formal
framework for database operations. It is designed for manipulating and querying relational
databases, focusing on operations that can be applied to tables (relations). Relational algebra
includes a set of operators that allow users to retrieve and manipulate data in databases.

Relational algebra is primarily concerned with operations like:

1. Selection (σ): Filters rows based on a condition.

2. Projection (π): Selects specific columns from a table.

3. Union (∪): Combines the result of two queries.

4. Difference (−): Returns the difference between two tables.

5. Cartesian Product (×): Combines each row from one table with every row of another table.

6. Join: Combines rows from two tables based on some condition.

7. And (∧), Or (∨), Not (¬): Logical operators used in conditions.

For our discussion, we'll focus on Selection, Projection, and the logical operators AND, OR, and
NOT, which form the core of many queries in relational databases.

Basic Relational Algebra Operations

1. Selection (σ)

Selection is used to filter rows from a relation (table) based on a specified condition.

Syntax:
σcondition(R)σ_{condition}(R)σcondition(R)

Where:

• RRR is a relation (table).

• condition is a logical expression used to filter rows.

Example:

• Select employees whose salary is greater than 50,000:


σsalary>50000(Employees)σ_{\text{salary} > 50000}(\text{Employees})σsalary>50000
(Employees)

In a table called Employees:

ID Name Salary Department

1 John 55000 HR
ID Name Salary Department

2 Alice 48000 IT

3 Bob 60000 IT

After applying the selection operation σ_salary > 50000(Employees), the result would be:

ID Name Salary Department

1 John 55000 HR

3 Bob 60000 IT

2. Projection (π)

Projection is used to select specific columns from a relation.

Syntax:
πcolumns(R)π_{columns}(R)πcolumns(R)

Where:

• RRR is the relation (table).

• columns specifies which columns to keep.

Example:

• Project the Name and Salary columns from the Employees table:
πName,Salary(Employees)π_{\text{Name}, \text{Salary}}(\text{Employees})πName,Salary
(Employees)

For the table Employees:

ID Name Salary Department

1 John 55000 HR

2 Alice 48000 IT

3 Bob 60000 IT

After applying the projection operation π_Name, Salary(Employees), the result would be:

Name Salary

John 55000

Alice 48000
Name Salary

Bob 60000

3. Logical Operators: AND, OR, NOT

These operators are used in selection conditions to combine or negate conditions.

• AND (∧): Both conditions must be true.

• OR (∨): At least one condition must be true.

• NOT (¬): Negates a condition.

Syntax:

• condition1∧condition2\text{condition1} \land \text{condition2}condition1∧condition2


(AND)

• condition1∨condition2\text{condition1} \lor \text{condition2}condition1∨condition2 (OR)

• ¬condition\lnot \text{condition}¬condition (NOT)

Example:

• Select employees who are in the IT department and have a salary greater than 50,000:
σDepartment=IT∧Salary>50000(Employees)σ_{\text{Department} = \text{IT} \land
\text{Salary} > 50000}(\text{Employees})σDepartment=IT∧Salary>50000(Employees)

For the Employees table:

ID Name Salary Department

1 John 55000 HR

2 Alice 48000 IT

3 Bob 60000 IT

After applying the selection operation:


σDepartment=IT∧Salary>50000(Employees)σ_{\text{Department} = \text{IT} \land \text{Salary} >
50000}(\text{Employees})σDepartment=IT∧Salary>50000(Employees) The result will be:

ID Name Salary Department

3 Bob 60000 IT

4. Combined Example

Let’s combine selection, projection, and logical operators.

Example:
• Select the Name and Salary of employees who are in the IT department or have a salary
greater than 50,000.

Relational Algebra: πName,Salary(σDepartment=IT∨Salary>50000(Employees))π_{\text{Name},


\text{Salary}}(σ_{\text{Department} = \text{IT} \lor \text{Salary} >
50000}(\text{Employees}))πName,Salary(σDepartment=IT∨Salary>50000(Employees))

For the table Employees:

ID Name Salary Department

1 John 55000 HR

2 Alice 48000 IT

3 Bob 60000 IT

After applying the selection: σDepartment=IT∨Salary>50000(Employees)σ_{\text{Department} =


\text{IT} \lor \text{Salary} > 50000}(\text{Employees})σDepartment=IT∨Salary>50000(Employees)
The result is:

ID Name Salary Department

1 John 55000 HR

2 Alice 48000 IT

3 Bob 60000 IT

Then, after applying the projection: πName,Salary(Result)π_{\text{Name}, \text{Salary}}(


\text{Result})πName,Salary(Result) The final result is:

Name Salary

John 55000

Alice 48000

Bob 60000

Implementation in an Online IDE (Example in SQL Syntax)

Since RelaX is similar to SQL and relational algebra, you can simulate relational algebra operations
in an SQL-based online IDE. Below is the SQL equivalent of relational algebra for the examples
above.

Example 1: Selection with AND and OR

sql

Copy code
SELECT Name, Salary

FROM Employees

WHERE Department = 'IT' OR Salary > 50000;

Example 2: Projection

sql

SELECT Name, Salary

FROM Employees;

Example 3: Selection with AND

sql

SELECT Name, Salary

FROM Employees

WHERE Department = 'IT' AND Salary > 50000;

These SQL queries can be run in online IDEs such as:

• SQL Fiddle (http://sqlfiddle.com/)

• DB Fiddle (https://www.db-fiddle.com/)

• W3Schools SQL TryIt Editor (https://www.w3schools.com/sql/trysql.asp)

You might also like