SQL Outer Join

SQL OUTER Joins:

A NORMAL join finds values from two tables that are in a relation to each other. Usually, this relation is equality (=), but it can also be all sorts of operations that either return true or false. The important thing is that a NORMAL join only returns matching rows from both (joined) tables. Obviously, a row whose column-value is not found in the other table's joined column is not returned at all. But, sometimes, we need to show these rows as well.

The purpose of an outer join is to include non-matching rows, and the outer join returns these missing columns as NULL values.

A LEFT OUTER JOIN results in a table which includes joined rows and any unmatched rows from the table listed to the left. The keyword LEFT in a LEFT OUTER JOIN tells you that the resulting table will include unmatched rows from the table to the LEFT of the keyword JOIN in the FROM clause of the query. In other words when we use LEFT OUTER JOIN clause we point out that we want to get all rows from the left table listed in our FROM clause, even if they don’t have a match in the right table.

A RIGHT OUTER JOIN is just reverse of LEFT OUTER JOIN.

According to old oracle syntax(which uses ‘+’ for outer join operations) :

An outer join uses a (+) on the side of the operator (equality operator) where we want to have nulls returned if no value matches.

The syntax for performing an outer join in SQL is database-dependent. For example, in Oracle, we will place an "(+)" in the WHERE clause on the other side of the table for which we want to include all the rows.

According to http://www.oracle.com/technology/products/oracle9i/
daily/may31.html

An outer join extends the result of a simple join. An outer join returns all rows that satisfy the join condition and also returns some or all of those rows from one table for which no rows from the other satisfy the join condition.

  • To write a query that performs an outer join of tables A and B and returns all rows from A (a left outer join), use the LEFT [OUTER] JOIN syntax in the FROM clause, or apply the outer join operator (+) to all columns of B in the join condition in the WHERE clause. For all rows in A that have no matching rows in B, Oracle returns null for any select list expressions containing columns of B.
  • To write a query that performs an outer join of tables A and B and returns all rows from B (a right outer join), use the RIGHT [OUTER] JOIN syntax in the FROM clause, or apply the outer join operator (+) to all columns of A in the join condition in the WHERE clause. For all rows in B that have no matching rows in A, Oracle returns null for any select list expressions containing columns of A.
  • To write a query that performs an outer join and returns all rows from A and B, extended with nulls if they do not satisfy the join condition (a full outer join), use the FULL [OUTER] JOIN syntax in the FROM clause.

Full outer joins – They are like a left or right outer join, but all the rows from both row sources are returned - if they "join" - fine - they will be joined - but if they don't have a match in the other table by the join key(s) they are returned anyway.



Employee Table:
LND_Id
Kamal31
Jones33
Singh33
Smith34
Robinson34
Jasper36

Department Table:
Department NameD_Id
Sales31
Engineering33
Clerical34
Marketing35

Example left outer join (ANSI 92 standard syntax):
SELECT distinct *

FROM employee

LEFT OUTER JOIN

department

ON employee.D_id = department.D_id

Example left outer join (non-standard syntax):

SELECT *

FROM employee

WHERE employee.D_id = department.D_id(+)

Example right outer join (ANSI 92 standard syntax):

SELECT *

FROM employee

RIGHT OUTER JOIN

department

ON employee.D_id = department.D_id

Example right outer join (non-standard syntax):

SELECT *

FROM employee

WHERE employee.D_id )+) = department.D_id




LND_IdDepartment NameD_Id
Smith34Clerical34
Jones33Mathematicsg33
Robinson34Clerical34
Singh33mathematics33
Kamal31Sales31
NULLNULLMarketing35

Example full outer join (ANSI 92 standard syntax):
SELECT *

FROM employee

FULL OUTER JOIN

department

ON employee.D_id = department.D_id


LND_IdDepartment NameD_Id
Smith34Clerical34
Jones33Engineering33
Robinson34Clerical34
Jasper36NULLNULL
Singh33Engineering33
Kamal31Sales31
NULL
NULLMarketing35

Recent Tutorials