# Relational Algebra

Relational Algebra is a procedural query language used to retrieve and manipulate data stored in relational databases. It provides a theoretical foundation for relational database management systems (RDBMS) and forms the basis for defining and performing operations on relational data.

Relational Algebra consists of a set of operators that operate on relations (tables) and produce new relations as their results. These operators can be categorized into two types: unary operators and binary operators.

Unary operators act on a single relation, while binary operators act on two relations. Here are some of the commonly used operators in Relational Algebra:

1. Selection (σ): The selection operator filters rows from a relation based on a specified condition. It selects tuples that satisfy the given predicate.
2. Projection (π): The projection operator selects a subset of columns from a relation. It creates a new relation with only the specified columns.
3. Union (⋃): The union operator combines two relations and returns a new relation that includes all tuples from both relations. Duplicate tuples are eliminated.
4. Intersection (⋂): The intersection operator returns a new relation that includes only the tuples that are common to both input relations.
5. Difference (-): The difference operator returns a new relation that includes tuples from the first relation that do not appear in the second relation.
6. Cartesian Product (×): The Cartesian product operator combines every tuple from the first relation with every tuple from the second relation. It returns a new relation with a combination of all possible pairs of tuples.
7. Join (⨝): The join operator combines tuples from two relations based on a common attribute. It returns a new relation that contains all combinations of tuples where the specified attribute values match.

These operators can be combined to create more complex queries in Relational Algebra. By using a combination of these operators, you can express a wide range of queries and operations on relational data.

It’s worth noting that Relational Algebra provides a theoretical foundation for working with relational databases, but in practice, most database systems use SQL (Structured Query Language) to interact with the data. SQL is a higher-level language that incorporates the concepts of Relational Algebra and provides a more user-friendly syntax for querying databases.

### Types of Relational operation:

In Relational Algebra, there are several types of relational operations that can be performed on relations (tables) to retrieve and manipulate data. These operations can be categorized into two groups: basic operations and derived operations.

1. Basic Operations: a. Selection (σ): The selection operation filters rows from a relation based on a specified condition. It selects tuples that satisfy the given predicate.

b. Projection (π): The projection operation selects a subset of columns from a relation. It creates a new relation with only the specified columns.

c. Union (⋃): The union operation combines two relations and returns a new relation that includes all tuples from both relations. Duplicate tuples are eliminated.

d. Intersection (⋂): The intersection operation returns a new relation that includes only the tuples that are common to both input relations.

e. Difference (-): The difference operation returns a new relation that includes tuples from the first relation that do not appear in the second relation.

f. Cartesian Product (×): The Cartesian product operation combines every tuple from the first relation with every tuple from the second relation. It returns a new relation with a combination of all possible pairs of tuples.

g. Join (⨝): The join operation combines tuples from two relations based on a common attribute. It returns a new relation that contains all combinations of tuples where the specified attribute values match.

2. Derived Operations: a. Rename (ρ): The rename operation changes the name of a relation or attribute. It allows you to assign new names to relations or attributes in order to improve clarity or resolve naming conflicts.

b. Set Operations: Set operations include operations like union, intersection, and difference, which were mentioned in the basic operations. These operations treat relations as mathematical sets and perform corresponding set operations.

c. Division (÷): The division operation is used to find tuples in one relation that match all tuples in another relation. It returns a new relation containing tuples that satisfy the division condition.

d. Aggregate Functions: Aggregate functions, such as COUNT, SUM, AVG, MIN, MAX, etc., perform calculations on groups of tuples and return a single value. These functions are used to summarize or derive information from the data.

e. Nested Operations: Nested operations involve nesting one operation inside another. For example, performing a selection operation followed by a projection operation on the result.

These are some of the commonly used relational operations in Relational Algebra. By combining and nesting these operations, you can express complex queries and transformations on relational data.