Professional Documents
Culture Documents
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
2)Find the Roll no, student name and student address whose rollno is greater than 300
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
-------------------------------------------------------------------------------------------------
Input: (EMPLOYEE ⋈ FACT_WORKERS)
Output:
-------------------------------------------------------------------------------------------------
Input: (EMPLOYEE ⟕ FACT_WORKERS)
•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)
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(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
Id Name Salary
-----------------------
1 A 80
2 B 40
3 C 60
4 D 70
5 E 60
6 F Null
Min(salary): Minimum value in the salary column except NULL i.e., 40.
5. Max():
Id Name Salary
-----------------------
1 A 80
2 B 40
3 C 60
4 D 70
5 E 60
6 F Null