Professional Documents
Culture Documents
SQL JOIN
SQL Join is used to fetch data from two or more tables, which is joined to appear as single set of
data. It is used for combining column from two or more tables by using values common to both
tables.
JOIN Keyword is used in SQL queries for joining two or more tables. Minimum required
condition for joining table, is (n-1) where n, is number of tables. A table can also join to itself,
which is known as, Self Join.
Types of JOIN:
Following are the types of JOIN that we can use in SQL:
Inner
Outer
Left
Right
OUTER JOIN
Outer Join is based on both matched and unmatched data. Outer Joins subdivide further into,
The left outer join returns a resultset table with the matched data from the two tables and then
the remaining rows of the left table and null from the right table's columns.
Syntax for Left Outer Join is,
SELECT column-name-list FROM
table-name1 LEFT OUTER JOIN table-name2
ON table-name1.column-name = table-name2.column-name;
2. JOIN WITH NOT EQUALITY OPERATOR(NON EQUI JOIN): Display employee and
their respective Department where employee department number is not same as
department’s department id.
3. EQUI JOIN WITH SPECIFIED CONDITION: Display employee and their respective
Department where employee department number is not same as department’s department
id and Department name is MARKETING
SELECT EMP_NAME, DEPT_NAME
FROM EMPLOYEE E JOIN DEPARTMENT D ON E.DEPT_NUM<>D.DEPT_ID WHERE
D.DEPT_NAME='MARKETINGs';
OUTER JOIN:
1. LEFT OUTER JOIN OR LEFT JOIN: Join Employee and department tables with
reference to employee table
SELECT *
FROM EMPLOYEE E LEFT JOIN DEPARTMENT D ON E.DEPT_NUM=D.DEPT_ID;
2. RIGHT OUTER JOIN OR RIGHT JOIN: Join Employee and department tables with
reference to department table
SELECT * FROM
EMPLOYEE E RIGHT JOIN DEPARTMENT D ON E.DEPT_NUM=D.DEPT_ID;
3. COMBINATION OF SET AND JOIN OPERATIONS
SELECT *
FROM EMPLOYEE E LEFT JOIN DEPARTMENT D ON E.DEPT_NUM=D.DEPT_ID
UNION
SELECT *
FROM EMPLOYEE E RIGHT JOIN DEPARTMENT D ON E.DEPT_NUM=D.DEPT_ID;