In
this document I am going to explain about the joins in SQL. The basic functionality of joins is to retrieve
the data from two are more tables.
When data is required
from more than one table or view then one table or view should be join with other
table or view in the means
of columns. Then the columns in different tables or views should match in their
data types and data. Columns names are may or may not be equal.
The joins are performed when FROM clause contains more than one table or view
in their SQL query. If FROM clause contains N tables then the minimum requirement is N-1 JOINS.
IMPORTANT
RULES ABOUT JOINS:
· The SELECT clause columns are may or may
not present in JOIN statement.
· If any columns are contains LOB data types then that columns are
should not be used in WHERE clause statement.
· The optimizer determines the order in
which join the table based on “given join conditions and Indexes upon the
tables”.
· If more than one table or
view contains the same column name then optimizer may confused which table
column you want. Use “.” Operator for representing the column
name with table name like TABLE
NAME.COLUMN NAME.
· In normal conditions also if you are
using column name in select statement with the help of table name then data
access is enhanced compared to normal one.
There are basically five types of
joins are there
I.
Inner
join
II.
Left
outer join
III.
Full
outer join
IV.
Right
outer join
V.
Cross
join
The other joins are Self join, Natural join. This joins basically depends upon the
previous joins only.
Chances are, you've already written a SQL statement that uses an
SQL INNER JOIN. It is the most common type of SQL join. SQL INNER JOINS return
all rows from multiple tables where the join condition is met.
The syntax for the SQL INNER JOIN is:SELECT columns
FROM table1
INNER JOIN table2
ON table1.column = table2.column;
The SQL INNER JOIN would return the records where table1 and table2 intersect.
Here is an example of a SQL INNER JOIN:
SELECT s.supplier_id, s.supplier_name, od.order_date
FROM suppliers AS s
INNER JOIN order_details AS od
ON s.supplier_id = od.supplier_id;
Let's look at some data to explain how the INNER JOINS work:
We have a table called suppliers with two fields (supplier_id and supplier_ name). It contains the following data:
supplier_id
|
supplier_name
|
10000
|
IBM
|
10001
|
Hewlett Packard
|
10002
|
Microsoft
|
10003
|
NVIDIA
|
order_id
|
supplier_id
|
order_date
|
500125
|
10000
|
2003/05/12
|
500126
|
10001
|
2003/05/13
|
500127
|
10004
|
2003/05/14
|
SELECT suppliers.supplier_id, suppliers.supplier_name, orders.order_date
FROM suppliers
INNER JOIN orders
ON suppliers.supplier_id = orders.supplier_id;
supplier_id
|
name
|
order_date
|
10000
|
IBM
|
2003/05/12
|
10001
|
Hewlett
Packard
|
2003/05/13
|
As a final note, it is worth mentioning that the SQL INNER JOIN example above could be rewritten using the older implicit syntax as follows (but we still recommend using the INNER JOIN keyword syntax):
SELECT suppliers.supplier_id, suppliers.supplier_name, orders.order_date
FROM suppliers, orders
WHERE suppliers.supplier_id = orders.supplier_id;
The syntax for the SQL LEFT OUTER JOIN is:
SELECT columns
FROM table1
LEFT [OUTER] JOIN table2
ON table1.column = table2.column;
In this visual diagram, the SQL LEFT OUTER JOIN returns the shaded area:
The SQL LEFT OUTER JOIN would return the all records from table1 and only those records from table2 that intersect with table1.
Here is an example of a SQL LEFT OUTER JOIN:
SELECT suppliers.supplier_id, suppliers.supplier_name, orders.order_date
FROM suppliers
LEFT OUTER JOIN orders
ON suppliers.supplier_id = orders.supplier_id;
If a supplier_id value in the suppliers table does not exist in the orders table, all fields in the orders table will display as <null> in the result set.
Let's look at some data to explain how LEFT OUTER JOINS work:
We have a table called suppliers with two fields (supplier_id and name). It contains the following data:
supplier_id
|
supplier_name
|
10000
|
IBM
|
10001
|
Hewlett
Packard
|
10002
|
Microsoft
|
10003
|
NVIDIA
|
order_id
|
supplier_id
|
order_date
|
500125
|
10000
|
2003/05/12
|
500126
|
10001
|
2003/05/13
|
SELECT suppliers.supplier_id, suppliers.supplier_name, orders.order_date
FROM suppliers
LEFT OUTER JOIN orders
ON suppliers.supplier_id = orders.supplier_id;
supplier_id
|
supplier_name
|
order_date
|
10000
|
IBM
|
2003/05/12
|
10001
|
Hewlett
Packard
|
2003/05/13
|
10002
|
Microsoft
|
<null>
|
10003
|
NVIDIA
|
<null>
|
As a final note, it is worth mentioning that the LEFT OUTER JOIN example above could be rewritten using the older implicit syntax that utilizes the outer join operator (+) as follows (but we still recommend using the LEFT OUTER JOIN keyword syntax):
SELECT suppliers.supplier_id, suppliers.supplier_name, orders.order_date
FROM suppliers, orders
WHERE suppliers.supplier_id = orders.supplier_id(+);
The syntax for the SQL FULL OUTER JOIN is:
SELECT columns
FROM table1
FULL [OUTER] JOIN table2
ON table1.column = table2.column;
In this visual diagram, the SQL FULL OUTER JOIN returns the shaded area:
The SQL FULL OUTER JOIN would return the all records from both table1 and table2.
Here is an example of a SQL FULL OUTER JOIN:
SELECT suppliers.supplier_id, suppliers.supplier_name, orders.order_date
FROM suppliers
FULL OUTER JOIN orders
ON suppliers.supplier_id = orders.supplier_id;
If a supplier_id value in the suppliers table does not exist in the orders table, all fields in the orders table will display as <null> in the result set. If a supplier_id value in the orders table does not exist in the suppliers table, all fields in the suppliers table will display as <null> in the result set.
Let's look at some data to explain how FULL OUTER JOINS work:
We have a table called suppliers with two fields (supplier_id and name). It contains the following data:
supplier_id
|
supplier_name
|
10000
|
IBM
|
10001
|
Hewlett Packard
|
10002
|
Microsoft
|
10003
|
NVIDIA
|
order_id
|
supplier_id
|
order_date
|
500125
|
10000
|
2013/08/12
|
500126
|
10001
|
2013/08/13
|
500127
|
10004
|
2013/08/14
|
SELECT suppliers.supplier_id, suppliers.supplier_name, orders.order_date
FROM suppliers
FULL OUTER JOIN orders
ON suppliers.supplier_id = orders.supplier_id;
supplier_id
|
supplier_name
|
order_date
|
10000
|
IBM
|
2013/08/12
|
10001
|
Hewlett Packard
|
2013/08/13
|
10002
|
Microsoft
|
<null>
|
10003
|
NVIDIA
|
<null>
|
<null>
|
<null>
|
2013/08/14
|
The row for supplier_id 10004 would be also included because a FULL OUTER JOIN was used. However, you will notice that the supplier_id and supplier_name field for those records contain a <null> value.
As a final note, it is worth mentioning that the FULL OUTER JOIN example above could not have been written in the old syntax without using a UNION query.
This type of join returns all rows from the RIGHT-hand table specified in the ON condition and only those rows from the other table where the joined fields are equal (join condition is met).
The syntax for the SQL RIGHT OUTER JOIN is:
SELECT columns
FROM table1
RIGHT [OUTER] JOIN table2
ON table1.column = table2.column;
In this visual diagram, the SQL RIGHT OUTER JOIN returns the shaded area:
The SQL RIGHT OUTER JOIN would return the all records from table2 and only those records from table1 that intersect with table2.
Here is an example of a SQL RIGHT OUTER JOIN:
SELECT orders.order_id, orders.order_date, suppliers.supplier_name
FROM suppliers
RIGHT OUTER JOIN orders
ON suppliers.supplier_id = orders.supplier_id;
If a supplier_id value in the orders table does not exist in the suppliers table, all fields in the suppliers table will display as <null> in the result set.
Let's look at some data to explain how RIGHT OUTER JOINS work:
We have a table called suppliers with two fields (supplier_id and name). It contains the following data:
supplier_id
|
supplier_name
|
10000
|
Apple
|
10001
|
Google
|
order_id
|
supplier_id
|
order_date
|
500125
|
10000
|
2013/08/12
|
500126
|
10001
|
2013/08/13
|
500127
|
10002
|
2013/08/14
|
SELECT orders.order_id, orders.order_date, suppliers.supplier_name
FROM suppliers
RIGHT OUTER JOIN orders
ON suppliers.supplier_id = orders.supplier_id;
order_id
|
order_date
|
supplier_name
|
500125
|
2013/08/12
|
Apple
|
500126
|
2013/08/13
|
Google
|
500127
|
2013/08/14
|
<null>
|
As a final note, it is worth mentioning that the RIGHT OUTER JOIN example above could be rewritten using the older implicit syntax that utilizes the outer join operator (+) as follows (but we still recommend using the RIGHT OUTER JOIN keyword syntax):
SELECT orders.order_id, orders.order_date, suppliers.supplier_name
FROM suppliers, orders
WHERE suppliers.supplier_id(+) = orders.supplier_id;
The
Cross Join is simply like mapping the
one record of the left table to all records in right table. The concept is looking like this
3 comments:
very nice.. keep it up
Good Job :-) keep it up
Post a Comment