Relational Algebra
Relational Algebra
Syntax-
σ<selection_condition>(R)
Examples-
Select tuples from a relation “Employees” where job_id is “ST_MAN”
Select * from employees
Where job_id=‘ST_MAN’;
But in relational algebra
σjob_id = “ST_MAN” (employees)
Select tuples from a relation “Employees” where job_id is “ST_MAN” and
department_id is 90
SQL Query:
Select employee_id, frist_name from employees;
Select tuples from a relation “Employees” where job_id is “ST_MAN” and department_id
is 90
∏employee_id, first_name, last_name, job_id (σjob_id = “ST_MAN” ∧ department_id = “90” (employees))
A = {2, 3, 5, 7, 11}
B = {1, 3, 5, 7, 9, 11}
A∩B = {3, 5, 7, 11}
A = {2, 3, 5, 7, 11}
B = {1, 3, 5, 7, 9, 11}
A-B = {2}
Union Operator (∪)-
Union
Let R and S be two relations. In relational algebra, an operator used to merge
(append) two tables into a new table, dropping the
Then- duplicate rows. The tables must be union
compatible (Two or more tables that have the same
R ∪ S is the set of all tuples belonging to either R or S or both. number of columns and the corresponding columns
In R ∪ S, duplicates are automatically removed. have compatible domains)
PhD_student Id Name
employee
Id Name 101 ravi
id name
101 ravi 103 rohit
103 rohit ∪ 101 ram
102 ram
109 ashi 104 mohit
102 ram
201 Ayush 109 ashi
104 mohit
201 Ayush
PhD_student employee
Id Name
id name
101 ravi Id Name
103 rohit
ꓵ 101 ram
102 ram
109 ashi
102 ram
201 Ayush
104 mohit
PhD_student employee
Id Name
Id Name
id name 101 ravi
101 ravi
103 rohit ∪ 101 ram 103 rohit
109 ashi 104 mohit
102 ram
201 Ayush
104 mohit
Cross join performed a cartesian product among the rows of two different tables. where the columns
name may or may not be matched......but in natural join its mandatory that in order to perform join
operation columns name of two tables must be matched
A ⋈θ B
∏employee_id, job_id, depart_name (σ locations.location_id between 1700 and 2400 ∧ department_id >50 (employees ⋈θ departments))
A B
The query in the above example retrieves all rows in the EMPLOYEES table, even if there is no match in the
DEPARTMENTS table.
Right Outer Join:
In the right outer join, operation allows keeping all tuple in the right relation. However, if there is no
matching tuple is found in the left relation, then the attributes of the left relation in the join result are
filled with null values.
A B
The query in the above example retrieves all rows in the DEPARTMENTS table, even if there is no match in the EMPLOYEES
table.
Full Outer Join
In a full outer join, all tuples from both relations are included in the result, irrespective of the
matching condition.
A B
Aggregate functions take a collection of values and return a single value as a result. For example, the
aggregate function sum, avg, min, max etc takes a collection of values and returns the single resultant value
of these values. Thus, the function sum applied on the collection:
Department_idGavg(salary) (Employees)
• Gavg(salary) (Employees)
• Gmin(salary) (Employees)
Gcount-distinct(department_id)(σ salary>6000(Employees))
• Gmax(salary) (Employees)