www.gradeup.
co
1
www.gradeup.co
Content :-
1. Binary Operations
a. Union
b. Intersection
c. Set Difference
d. Cartesian Product
2. Joins
UNION : The result of this operation , denoted by R U S, is a relation that includes all the
tuples that are either in R or in S or in both R and S. Duplicate tuples are eliminated .
Table R
Name Address
Ram U.P.
Shaym Haryana
Mohan Delhi
Table S
Name Mobile No.
Ram 9999999393
Shyam 9999999563
S.K. 8899999393
Shruti 9999779393
∏ NAME (R) ∪ ∏ NAME (S)
Name
Ram
Shaym
Mohan
S.K.
Shruti
Intersection :-The result of this operation , denoted by R ∩ S is a relation that includes all
the tuples that are common in both R and S.
∏ NAME (R) ∩ ∏ NAME (S)
Name
Ram
Shaym
Set Difference (or MINUS):- The result of this operation , denoted by R-S , is relation
that includes all the tuples that are in R but not in S.
∏ NAME (R) - ∏ NAME (S)
Name
Mohan
Cartesian product:- It is also known as cross product or cross join which is denoted by X .
It is used to combine each row of one table with the each row of other table .
Employee :
Name Salary
2
www.gradeup.co
Ram 10000
Shaym 20000
Mohan 5000
Department :-
Dprt_No. Dprt_Name
1 H.R.
2 Management
3 IT
Employee X Department
Name Salary Dprt_No. Dprt_Name
Ram 10000 1 H.R.
Ram 10000 2 Management
Ram 10000 3 IT
Shaym 20000 1 H.R.
Shaym 20000 2 Management
Shaym 20000 3 IT
Mohan 5000 1 H.R.
Mohan 5000 2 Management
Mohan 5000 3 IT
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 ⋈.
Join Operation
Outer Join
Left Outer Join
Right Outer Join
Equi Join
Full Natrual Join
Outer Join
Employee :-
Emp_Code EMP_Name
001 Sam
f
002 Jerry
003 Hack
Salary :-
t
3
www.gradeup.co
Emp_code Salary
001 50000
002 60000
003 35000
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 ⋈.
Example: Let's use the above EMPLOYEE table and SALARY table:
∏EMP_NAME, SALARY (EMPLOYEE ⋈ SALARY)
2. Outer Join :
The outer join operation is an extension of the join operation. It is used to deal with missing
information.
EMPLOYEE
EMP_NAME STREET CITY
Riya Civil Line Delhi
Suman M.G. Street Bengaluru
Shyam Nehru Nagar Mumbai
Ravi Park street Chennai
FACT_WORKERS
EMP_NAME BRANCH SALARY
Riya HCL 15000
Suman WIPRO 20000
Shyam INFOSYS 30000
Kulbir TCS 10000
EMPLOYEE ⋈ FACT_WORKERS
EMP_NAME STREET CITY BRANCH SALARY
Riya Civil Line Delhi HCL 15000
Suman M.G. Street Bengaluru WIPRO 20000
Shyam Nehru Nagar Mumbai INFOSYS 30000
An outer join is basically of three types:
a. Left outer join
b. Right outer join
c. Full outer join
Left Outer Join:
o Left outer join contains the set of tuples of all combinations in R and S that are equal on
their common attribute names.
o In the left outer join, tuples in R have no matching tuples in S.
o It is denoted by ⟕.
4
www.gradeup.co
Example: Using the above EMPLOYEE table and FACT_WORKERS table.
EMPLOYEE ⟕ FACT_WORKERS
EMP_NAME STREET CITY BRANCH SALARY
Riya Civil Line Delhi HCL 15000
Suman M.G. Street Bengaluru WIPRO 20000
Shyam Nehru Nagar Mumbai INFOSYS 30000 Right
Ravi Park street Chennai NULL NULL Outer
Join
o Right outer join contains the set of tuples of all combinations in R and S that are equal on
their common attribute names.
o In right outer join, tuples in S have no matching tuples in R.
o It is denoted by ⟖.
Example: Using the above EMPLOYEE table and FACT_WORKERS Relation
EMPLOYEE ⟖ FACT_WORKERS
EMP_NAME BRANCH SALARY STREET CITY
Riya HCL 15000 Civil Line Delhi
Suman WIPRO 20000 M.G. Street Bengaluru
Shyam INFOSYS 30000 Nehru Nagar Mumbai
Kulbir TCS 10000 NULL NULL
Full Outer Join:
o Full outer join is like a left or right join except that it contains all rows from both tables.
o 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.
o It is denoted by ⟗.
Example: Using the above EMPLOYEE table and FACT_WORKERS table
EMPLOYEE ⟗ FACT_WORKERS
EMP_NAME STREET CITY BRANCH SALARY
Riya Civil Line Delhi HCL 15000
Suman M.G. Street Bengaluru WIPRO 20000
Shyam Nehru Nagar Mumbai INFOSYS 30000
Ravi Park street Chennai NULL NULL
Kulbir NULL NULL TCS 10000
Equi Join:-It is also known as an inner join. It is the most common join. It is based on
matched data as per the equality condition. The equi join uses the comparison operator(=)
Example :-
Customer
C_ID NAME
1 Heena
2 Reena
3 Hari
5
www.gradeup.co
4 Saurbh
Product
Product_ID City
1 Delhi
2 Mumbai
3 U.P.
4 H.P.
Customer ⋈ Product
C_ID NAME Product_ID City
1 Heena 1 Delhi
2 Reena 2 Mumbai
3 Hari 3 U.P.
4 Saurbh 4 H.P.
6
www.gradeup.co