# DBMS | Basic Operators in Relational Algebra

**Basic operators in relational algebra**: Here, we are going to learn about the **basic operators with the examples in Relational Algebra**.

Submitted by Anushree Goswami, on June 23, 2019

There are 5 different types of **Basic Operators in Relational Algebra in DBMS**,

### 1) Selection

**Selection operator** operates on the rows from a relation. It is denoted by the symbol **'σ'**. It is used to manipulate the data in single relation.

**For e.g. –** Find the information about the students whose marks are above 90.

Name | RollNo | Marks |
---|---|---|

Ashish | 1 | 87 |

Shubham | 2 | 99 |

Vishakha | 3 | 94 |

Yash | 4 | 88 |

**σMarks>90(S)**

Name | RollNo | Marks |
---|---|---|

Shubham | 2 | 99 |

Vishakha | 3 | 94 |

### 2) Projection

**Projection operator** operates on the columns of the relation. It is denoted by the symbol **'π'**. It is used to manipulate the data in single relation. It is used to project the column.

**For e.g. –** From the previous table, only show the columns that contain the Roll No as well as Marks of the Students.

Name | RollNo | Marks |
---|---|---|

Ashish | 1 | 87 |

Shubham | 2 | 99 |

Vishakha | 3 | 94 |

Yash | 4 | 88 |

**πRollNo,Marks(S2)**

RollNo | Marks |
---|---|

1 | 87 |

2 | 99 |

3 | 94 |

4 | 88 |

Further, we will discuss the basic operators that are the set operations and used to perform the standard operations on the set in relational algebra. Let us consider two tables **A** and **B**.

**A –**

RollNo | Name | Marks |
---|---|---|

1 | Aashi | 98 |

3 | Anjali | 79 |

4 | Brijesh | 88 |

**B –**

RollNo | Name | Marks |
---|---|---|

1 | Aashi | 98 |

2 | Abhishek | 87 |

3 | Anjali | 79 |

4 | Brijesh | 88 |

### 3) Union

**Union** works on the relation as **'either this or that'**. In relational algebra, **A∪B** returns a relation instance that contains the tuples of either relation instance **A** or relation instance **B** (or both). Here, **A** and **B** need to be union-compatible, and the schema of both result and **A** must be identical.

**Syntax:**

SELECT * FROM A UNION SELECT * FROM B;

RollNo | Name | Marks |
---|---|---|

1 | Aashi | 98 |

2 | Abhishek | 87 |

3 | Anjali | 79 |

4 | Brijesh | 88 |

### 4) Set Difference

The **set difference** works on the relation as **'this but not that'**. In relational algebra, **A - B** returns a relation instance that contains the tuples that occur in relation instance **A** but not in relation instance **B**. Here, **A** and **B** need to be **union-compatible**, and the schema of both result and **A** must be identical.

**Syntax:**

SELECT * FROM B MINUS SELECT * FROM A;

RollNo | Name | Marks |
---|---|---|

2 | Abhishek | 87 |

### 5) Cross-product

In relational algebra, **Cross-product** operation is also referred to as **Cartesian Product** in which **A × B** returns a relation instance whose schema contains all the fields of **A** followed by all the fields of **B** (both in the same order as appear in their tables). The obtained result from the cross product of **A** and **B** contains one tuple**〈a, b〉**, i.e., the concatenation of the tuple **a** and **b** such that **a ∈A** and **b∈ B**.

**Syntax:**

SELECT * FROM A CROSS JOIN B;

RollNo | Name | Marks | RollNo | Name | Marks |
---|---|---|---|---|---|

1 | Aashi | 98 | 1 | Aashi | 98 |

1 | Aashi | 98 | 2 | Abhishek | 87 |

1 | Aashi | 98 | 3 | Anjali | 79 |

1 | Aashi | 98 | 4 | Brijesh | 88 |

3 | Anjali | 79 | 1 | Aashi | 98 |

3 | Anjali | 79 | 2 | Abhishek | 87 |

3 | Anjali | 79 | 3 | Anjali | 79 |

3 | Anjali | 79 | 4 | Brijesh | 88 |

4 | Brijesh | 88 | 1 | Aashi | 98 |

4 | Brijesh | 88 | 2 | Abhishek | 87 |

4 | Brijesh | 88 | 3 | Anjali | 79 |

4 | Brijesh | 88 | 4 | Brijesh | 88 |

TOP Interview Coding Problems/Challenges

- Run-length encoding (find/print frequency of letters in a string)
- Sort an array of 0's, 1's and 2's in linear time complexity
- Checking Anagrams (check whether two string is anagrams or not)
- Relative sorting algorithm
- Finding subarray with given sum
- Find the level in a binary tree with given sum K
- Check whether a Binary Tree is BST (Binary Search Tree) or not
- 1[0]1 Pattern Count
- Capitalize first and last letter of each word in a line
- Print vertical sum of a binary tree
- Print Boundary Sum of a Binary Tree
- Reverse a single linked list
- Greedy Strategy to solve major algorithm problems
- Job sequencing problem
- Root to leaf Path Sum
- Exit Point in a Matrix
- Find length of loop in a linked list
- Toppers of Class
- Print All Nodes that don't have Sibling
- Transform to Sum Tree
- Shortest Source to Destination Path

Comments and Discussions

**Ad:**
Are you a blogger? Join our Blogging forum.