Types Of Joins:
Table Name “EMPLOYEE”
ID |
NAME |
101 |
Rajesh |
102 |
Swati |
103 |
Manoj |
104 |
Sachin |
105 |
Swati |
ID |
SALARY |
101 |
10000 |
102 |
8000 |
ID |
NAME |
SALARY |
101 |
Rajesh |
10000 |
102 |
Swati |
8000 |
Left Outer Join:
It is also called Left Join. It returns all records from the left table,
and the matched records from the right table.
Syntax:
SELECT column1, column2 ..column xFROM table1 LEFT OUTER JOIN table2ON table1.column = table2.column;
Old Syntax:
SELECT column1, column2 ..column x FROM table1, table2WHERE table1.column = table2.column(+);
Example:
Suppose we have a table named "EMPLOYEE" with two fields ID and Name. It contains the following data.
Table Name “EMPLOYEE”
ID
NAME
101
Rajesh
102
Swati
103
Manoj
104
Sachin
We have another table named "SALARY" having two fields ID and
SALARY. It contains the following data.
Table Name “SALARY”
ID
SALARY
101
10000
102
8000
Query:
SELECT ID, NAME, SALARY FROM EMPLOYEELEFT OUTER JOIN SALARYON EMPLOYEE.ID = SALARY.ID;
OR
SELECT ID, NAME, SALARY FROM EMPLOYEE, SALARY WHERE EMPLOYEE.ID = SALARY.ID(+);
Output of the Query will be:
ID
NAME
SALARY
101
Rajesh
10000
102
Swati
8000
103
Manoj
Null
104
Sachin
Null
ID |
NAME |
101 |
Rajesh |
102 |
Swati |
103 |
Manoj |
104 |
Sachin |
We have another table named "SALARY" having two fields ID and
SALARY. It contains the following data.
Table Name “SALARY”
ID |
SALARY |
101 |
10000 |
102 |
8000 |
ID |
NAME |
SALARY |
101 |
Rajesh |
10000 |
102 |
Swati |
8000 |
103 |
Manoj |
Null |
104 |
Sachin |
Null |
Right Outer Join:
It is also called Right Join. It returns all records from the Right table, and the matched records from the Left table.
Syntax:
SELECT column1, column2 ..column xFROM table1 RIGHT OUTER JOIN table2ON table1.column = table2.column;
Old Syntax:
SELECT column1, column2 ..column x FROM table1, table2WHERE table1.column(+) = table2.column;
Example:
Suppose we have a table named "EMPLOYEE" with two fields ID and Name. It contains the following data.
Table Name “EMPLOYEE”ID
NAME
101
Rajesh
102
Swati
103
Manoj
104
Sachin
We have another table named "SALARY" having two fields ID and SALARY. It contains the following data.
Table Name “SALARY”
ID
SALARY
101
10000
102
8000
105
14000
Query:
SELECT ID, NAME, SALARY FROM EMPLOYEERIGHT OUTER JOIN SALARYON EMPLOYEE.ID = SALARY.ID;
OR
SELECT ID, NAME, SALARY FROM EMPLOYEE, SALARY WHERE EMPLOYEE.ID(+) = SALARY.ID;
Output of the Query will be:
ID
NAME
SALARY
101
Rajesh
10000
102
Swati
8000
105
Null
14000
ID | NAME |
101 | Rajesh |
102 | Swati |
103 | Manoj |
104 | Sachin |
We have another table named "SALARY" having two fields ID and SALARY. It contains the following data.
Table Name “SALARY”
ID |
SALARY |
101 |
10000 |
102 |
8000 |
105 |
14000 |
ID |
NAME |
SALARY |
101 |
Rajesh |
10000 |
102 |
Swati |
8000 |
105 |
Null |
14000 |
Full Outer Join:
It is also called Full Join. It returns all records when there is a match in either left or right table.
Syntax:
SELECT column1, column2 ..column xFROM table1 FULL OUTER JOIN table2ON table1.column = table2.column;
Old Syntax:
No Syntax for Full outer join.
Example:
Suppose we have a table named "EMPLOYEE" with two fields ID and Name. It contains the following data.
Table Name “EMPLOYEE”ID
NAME
101
Rajesh
102
Swati
103
Manoj
104
Sachin
We have another table named "SALARY" having two fields ID and SALARY. It contains the following data.
Table Name “SALARY”
ID
SALARY
101
10000
102
8000
105
14000
Query:
SELECT ID, NAME, SALARY FROM EMPLOYEEFULL OUTER JOIN SALARYON EMPLOYEE.ID = SALARY.ID;
Output of the Query will be:
ID
NAME
SALARY
101
Rajesh
10000
102
Swati
8000
103
Manoj
Null
104
Sachin
Null
105
Null
14000
ID | NAME |
101 | Rajesh |
102 | Swati |
103 | Manoj |
104 | Sachin |
We have another table named "SALARY" having two fields ID and SALARY. It contains the following data.
Table Name “SALARY”
ID | SALARY |
101 | 10000 |
102 | 8000 |
105 | 14000 |
ID |
NAME |
SALARY |
101 |
Rajesh |
10000 |
102 |
Swati |
8000 |
103 |
Manoj |
Null |
104 |
Sachin |
Null |
105 |
Null |
14000 |