You are on page 1of 25

UNIT-2

Chp-2
Relational Algebra
• Relational algebra is a procedural query language, which takes instances of relations as input and yields
instances of relations as output.
• Relational algebra operations work on one or more relations to define another relation without changing the original
relations
• In the Relation algebra there are two type of operation one is Unary operation and second one Binary operation
• Unary operation takes as input a single table and produces an output another table.
• Binary operations take as input two tables and produce as output another table.

Unary opration
Selection operation
• The Selection operator select the row from the table that satisfy a given predicate.
• This operation allows to manipulate data in the single relation.
• The Selection operation is defined by the symbol called sigma(σ). The predicate is appear at subscript of Sigma
symbol(σ).
• The argument relation is present in the parenthesis after the σ
• Syntax :
• σ<predicate><Comparsion_operator><Constant_value>(<input_table_name>))
• Where Predicate: Name of the column in the table Comparsion_Operator:=,<,<=,>,=>,<>
Example Select all the student from the student table who‘s Roll no is greater than 300
Projection Operation(π)
• This operator is used to select some of the attributes from the table to produces a desired result set
• Note that Projection operation is used to eliminates the duplicates records in the table
• Syntax :
• Π <attributes>(<Input_Table_Name>)

• Example 1)Find the all record from the Stu dent table

• Π Roll No,Students_Name, Students_Address(Student)

2)Find the Roll no, student name and student address whose rollno is greater than 300

Π Roll No,Students_Name, Students_Address(σ RollNo>300 (Student))


Rename Operator(ρ)
• Rename operation gave alternate name to the given column or to any table by using the operator called Rename
operator
• This operator is used for selecting some specific column from multiple table(set of two or more tables) containing
multiple columns having same column name
• Rename operator is denoted by the greek letter rho(ρ)
• Syntaxρ
<New Name for Column>(<Input_Table_Name>)

• ex. 1)Find the all record from the Student table


SET Opration:
1.UNION
2.INTERSECT
3.MINUS

1.Union Operator
Union operator is used combine all the result form the first query to the result from the second query
Union operator does not eliminate duplicate record from the database and they prints the result expression
Syntax
(Relation1) Ụ (Relation 2)

R S RỤS

A 1 A 1 A 1
B 2 C 2 B 2
D 3 D 3 C 2
F 4 E 4 D 3
E 5 E 4
F 4
E 5
Example 1)

Union of the Two Table result in Employee Table and Project Table
Syntax:
select deptname from Employee union select deptname from Project;

Result: The return value would be sales, marketing, R&D and HR.
2 Intersect Operator
• This operator is find out all the tuples that all the Common in the result of Relation 1 and in the Result of Relation 2
• It is denoted by intersection ∩.
Syntax
(Relation1) ∩ (Relation 2)

R S R∩S

A 1 A 1
A 1
B 2 C 2
D 3
D 3 D 3
F 4 E 4
E 5
Example : Get all the employee's full name that are working on a project.

Syntax:
select EMPFIRSTNAME, EMPLASTNAME from Employee, Project
where Project. EMPNO =Employee. EMPNO;
3 Difference Operation
• The difference builds a relation consisting of all tuples appearing in the first and not the second of two specified
relations.
• The difference between two relation R1 and R2, R1 MINUS R2, is the set of all tuples belonging to R1 and not to R2.
• It is denoted by minus (-).

• Syntax R1-R2

R S R-S

A 1 A 1 B 2
B 2 C 2 D 3
D 3 D 3 F 4
F 4 E 4 E 5
E 5
Example:
Find the employee that are in sales department and are not on project P2.

Syntax:
select EMPNO from Employee
where DEPTNAME='Sales’
minus
select EMPNO from Project ;
Cartesian product
•The Cartesian product is used to combine each row in one table with each row in the other
table.
•It is denoted by X.
•R1 X R2
•–The Cartesian product is the table consisting of all tuples formed by concatenating each tuple in R1 with a tuple in R2,
for all tuples in R2
• The Cartesian Product is also an operator which works on two sets. It is sometimes called the CROSS PRODUCT or
CROSS JOIN.
• It combines the tuples of one relation with all the tuples of the other relation.
❑ Join Operations:

• A Join operation combines related tuples from different relations, if and only if a given join
condition is satisfied.

• It is denoted by ⋈.
1. Natural Join:
•A natural join is the set of tuples of all combinations in R and S that are equal on their common
attribute names.
•It is denoted by ⋈
EMP_ EMP_NAME EMP_CODE SALARY EMP_CO EMP_NA SALARY
CODE DE ME
101 50000
101 Stephan 101 Stephan 50000
102 30000
102 Jack 102 Jack 30000
103 25000
103 Harry 103 Harry 25000
Salary
Employee Operation: (EMPLOYEE ⋈ SALARY)
--------------------------------------------------------
Eg. ∏ EMP_NAME, SALARY (EMPLOYEE ⋈ SALARY)
EMP_NAME SALARY

Stephan 50000
Jack 30000
Harry 25000
2. Outer Join:

The outer join operation is an extension of the join operation. It is used to deal with missing information.
EMPLOYEE FACT_WORKERS

EMP_NAME STREET CITY EMP_NAME BRANCH SALARY


Ram Civil line Mumbai Ram Infosys 10000
Shyam Park street Kolkata Shyam Wipro 20000
Ravi M.G. Street Delhi Kuber HCL 30000
Hari Nehru nagar Hyderabad Hari TCS 50000

-------------------------------------------------------------------------------------------------
Input: (EMPLOYEE ⋈ FACT_WORKERS)
Output:

EMP_NAME STREET CITY BRANCH SALARY

Ram Civil line Mumbai Infosys 10000


Shyam Park street Kolkata Wipro 20000
Hari Nehru nagar Hyderabad TCS 50000
An outer join is basically of three types:

a.Left outer join

b.Right outer join

c.Full outer join


1. Left outer join:
•Left outer join contains the set of tuples of all combinations in R and S that are equal on their
common attribute names.
•In the left outer join, tuples in R have no matching tuples in S.
•It is denoted by ⟕.
EMP_NAME STREET CITY EMP_NAME BRANCH SALARY
Ram Civil line Mumbai Ram Infosys 10000
Shyam Park street Kolkata Shyam Wipro 20000
Ravi M.G. Street Delhi Kuber HCL 30000
Hari Nehru nagar Hyderabad Hari TCS 50000

-------------------------------------------------------------------------------------------------
Input: (EMPLOYEE ⟕ FACT_WORKERS)

EMP_NAME STREET CITY BRANCH SALARY

Ram Civil line Mumbai Infosys 10000


Shyam Park street Kolkata Wipro 20000
Hari Nehru street Hyderabad TCS 50000
Ravi M.G. Street Delhi NULL NULL
2.Right outer join:
•Right outer join contains the set of tuples of all combinations in R and S that are equal on their common attribute names.
•In right outer join, tuples in S have no matching tuples in R.

•It is denoted by ⟖.
EMP_NAME STREET CITY EMP_NAME BRANCH SALARY
Ram Civil line Mumbai Ram Infosys 10000
Shyam Park street Kolkata Shyam Wipro 20000
Ravi M.G. Street Delhi Kuber HCL 30000
Hari Nehru nagar Hyderabad Hari TCS 50000

-------------------------------------------------------------------------------------------------
Input: (EMPLOYEE ⟖ FACT_WORKERS)

EMP_NAME BRANCH SALARY STREET CITY


Ram Infosys 10000 Civil line Mumbai
Shyam Wipro 20000 Park street Kolkata
Hari TCS 50000 Nehru street Hyderabad
Kuber HCL 30000 NULL NULL
3.Full outer join:
•Full outer join is like a left or right join except that it contains all rows from both tables.
•In full outer join, tuples in R that have no matching tuples in S and tuples in S that have no
matching tuples in R in their common attribute name.
•It is denoted by ⟗
EMP_NAME STREET CITY EMP_NAME BRANCH SALARY
Ram Civil line Mumbai Ram Infosys 10000
Shyam Park street Kolkata Shyam Wipro 20000
Ravi M.G. Street Delhi Kuber HCL 30000
Hari Nehru nagar Hyderabad Hari TCS 50000
-------------------------------------------------------------------------------------------------
Input: (EMPLOYEE ⟗ FACT_WORKERS)
EMP_NAME STREET CITY BRANCH SALARY

Ram Civil line Mumbai Infosys 10000


Shyam Park street Kolkata Wipro 20000
Hari Nehru street Hyderabad TCS 50000
Ravi M.G. Street Delhi NULL NULL
Kuber NULL NULL HCL 30000
Aggregate functions:(Grouping Function)
• In database management an aggregate function is a function where the values of multiple rows are grouped
together as input on certain criteria to form a single value of more significant meaning.
• It can be applied either to all row in the table to a subsrt of table rows specified by WHERE clause or to one or
more group by clause.

Various Aggregate Functions

1) Count()

2) Sum()

3) Avg()

4) Min()

5) Max()
1.Count():
• This function return the number of data value of a specified column.
• If WHERE clause is included it return total no of data values for those rows that satisfies the
condition.
Id Name Salary
-----------------------
1 A 80
2 B 40
3 C 60
4 D 70
5 E 60
6 F Null

Count(*): Returns total number of records .i.e 6.

Count(salary): Return number of Non Null values over the column salary. i.e 5.

Count(Distinct Salary): Return number of distinct Non Null values over the column salary
.i.e 4
2.Sum():
• This function compute & return the sum of data values of specified numeric column for those rows
that satisfies the condition of Where clause.
• If where clause is omitted then query return sum of all data values of specified column

Id Name Salary
-----------------------
1 A 80
2 B 40
3 C 60
4 D 70
5 E 60
6 F Null

sum(salary): Sum all Non Null values of Column salary i.e., 310

sum(Distinct salary): Sum of all distinct Non-Null values i.e., 250.


3.Avg():

This function return the avg value of specified column.

Id Name Salary
-----------------------
1 A 80
2 B 40
3 C 60
4 D 70
5 E 60
6 F Null

Avg(salary) = Sum(salary) / count(salary) = 310/5

Avg(Distinct salary) = sum(Distinct salary) / Count(Distinct Salary) = 250/4


4.Min():

Min(salary): Minimum value in the salary column except NULL i.e., 40.

5. Max():

Max(salary): Maximum value in the salary i.e., 80.

Id Name Salary
-----------------------
1 A 80
2 B 40
3 C 60
4 D 70
5 E 60
6 F Null

You might also like