Joins in SQL
Joins in SQL
JOINS
• SQL joins are used to combine rows from two or more tables
based on a common field between them
• In SQL, there are various types of joins:
1) Natural Join
• Inner Join
• Equi Join
2) Outer Join
• Left Outer Join
• Right Outer Join
• Full Outer Join
• Tables are joined on columns that have same datatype and size
Inner Join
• It is a natural join used for retrieving records from more than two
tables having common data
• Inner join can be Equi join if it is having equal condition only
Syntax:
Employee
Dept
Table 1 Table 2
Resultant
Table
Emp_No Name Branch_No
11 Smith B001
List out the employee details
23 William B007
along with their branch name
34 Simi C006
using Left Outer Join
Employee
Branch_Name Branch_N Select e.emp_no, e.name,
o
b.branch_name from Employee e LEFT
Mumbai B001 OUTER JOIN Branch b ON b. branch_no
Mumbai B007 = e. branch no
Pune C009
Banglore F007
Select e.emp_no, e.name,
Branch b.branch_name from Employee e,
Branch b where b. branch_no = e.
branch no (+);
Right Outer Join
• The Right JOIN keyword returns all rows from the right table
(table2), with the matching rows in the left table (table1).
• The result is NULL in the right side when there is no match.
Table 1 Table 2
Resultant
Table
Emp_No Name Branch_No
11 Smith B001
List out the employee details
23 William B007
along with their branch name
34 Simi C006
using Right Outer Join
Employee
Select e.emp_no, e.name,
Branch_Name Branch_N b.branch_name from Employee e
o RIGHT OUTER JOIN Branch b ON b.
Mumbai B001 branch_no = e. branch no
Mumbai B007
Pune C009 Select e.emp_no, e.name,
Banglore F007 b.branch_name from Employee e,
Branch b where b. branch_no (+) = e.
Branch
branch no;
Full Outer Join
• It does both of those operations, padding tuples from the left
relation that did not match any from the right relation
• As well as tuples from the right relation that did not match any
from the left relation and adding them to the result of the join.
Table 2
Table 1
Resultant
Table
Emp_No Name Branch_No
11 Smith B001
List out the employee details
23 William B007
along with their branch name
34 Simi C006
using Full Join
Employee
Branch_Name Branch_N
Select e.emp_no, e.name,
o b.branch_name from Employee e
Mumbai B001 FULL OUTER JOIN Branch b ON b.
Mumbai B007
branch_no = e. branch no
Pune C009
Banglore F007
Branch