Thursday, October 19, 2006

Outer<-----Join----->Inner

Anil
Blogging after a long time....
I find people often get confused (in fact me also) between all the join flavours.
Let me give you an example to see how it works but the syntax may vary depending on the database vendor this works for mysql.

Names
-------------------------
No | Name | Interests |
------------------------
1201 |Anil|jokes |
-------------------------
1220 |Ravi |kodi |
-------------------------
1209 |Lax |E.G.Dt.paper|
-------------------------
1209|Lax |kodi |
-------------------------
1220|Ravi |jokes |
-------------------------
1211|Pinak |G.F.'s |
-------------------------
1201|anil |Blogging |
-------------------------
1220|Ravi |Blogging |
-------------------------
1236|Ramana |Casino |
-------------------------
1238|Vineetha |Sliding colleges|
-------------------------------

contacts
------------------
No | city| |
------------------
1201 |Eluru City |
-------------------
1220 |Pakki |
-------------------
1209 |Modekurru |
-------------------
1211 |Mandapeta |
-------------------
8888 |Kandriga |
-------------------
5555 |Pulletikurru|
-------------------
1111 |Irusumanda |
-------------------

If I do a regular JOIN (with none of the keywords INNER, OUTER, LEFT or RIGHT), then I get all records that match in the appropriate way in the two tables,
and records in both incoming tables that do not match are not reported:
mysql>select name, interests,city from names join contacts on names.no = contacts.no

----- ----------------------------
No | | Name | Interests | city
----- -----------------------------
1201 |Anil |jokes | Eluru City
----- ------------------------------
1220 |Ravi |kodi | Pakki
----- ------------------------------
1209 |Lax |E.G.Dt.paper | Modekurru
----- ------------------------------
1209 |Lax |kodi | Modekurru
----- -------------------------------
1220 |Ravi |jokes | Pakki
----- ------------------------------
1211 |Pinak |Movies | Mandapeta
----- -----------------------------
1201 |anil |Blogging | Eluru City
----- ------------------------------
1220 |Ravi |Blogging | Pakki
----- -------------------------------

If I do a LEFT JOIN, I get all records that match in the same way and IN ADDITION I get an extra record for each unmatched record in the left table of the join - thus ensuring (in my example) that every PERSON gets a mention
mysql>select no,name, Interests,city from names left join contacts on names.no = contacts.no;

----- ----------------------------
No | | Name | Interests | city
----- -----------------------------
1201 |Anil |jokes | Eluru City
----- ------------------------------
1220 |Ravi |kodi | Pakki
----- ------------------------------
1209 |Lax |E.G.Dt.paper | Modekurru
----- ------------------------------
1209 |Lax |kodi | Modekurru
----- -------------------------------
1220 |Ravi |jokes | Pakki
----- ------------------------------
1211 |Pinak |Movies | Mandapeta
----- -----------------------------
1201 |anil |Blogging | Eluru City
----- ------------------------------
1220 |Ravi |Blogging | Pakki
----- ----------------------------------
1236|Ramana |Casino |NULL
-----------------------------------------
1238|Vineetha |Sliding colleges|NULL
----------------------------------------

If I do a RIGHT JOIN, I get all the records that match and IN ADDITION I get an extra record for each unmatched record in the right table of the join - im my example, that means that each property gets a mention
Observe 1236,1238 were not included in the set But 8888,5555,1111 are
mysql>select no,name, Interests,city from names right join contacts on names.no = contacts.no;

---------------------------------
No | | Name | Interests | city
----- -----------------------------
1201 |Anil |jokes | Eluru City
----- ------------------------------
1220 |Ravi |kodi | Pakki
----- ------------------------------
1209 |Lax |E.G.Dt.paper | Modekurru
----- ------------------------------
1209 |Lax |kodi | Modekurru
----- -------------------------------
1220 |Ravi |jokes | Pakki
----- ------------------------------
1211 |Pinak |Movies | Mandapeta
----- ------------------------------------
1201 |anil |Blogging | Eluru City
----- -------------------------------------
8888 |NULL |NULL | Kandriga
-------------------------------------------
5555 |NULL |NULL | Pulletikurru
----------------------------------------
1111 |NULL |NULL | Irusumanda
-------------------------------------------

An INNER JOIN does a full join, just like the first example, and the word OUTER may be added after the word LEFT or RIGHT in the last two examples - it's provided for ODBC compatability and doesn't add an extra capabilities.

1 Comments:

At 11:16 PM, Anonymous Anonymous said...

Some thing new..
Please add my following links to your blog(only if u like):

blog : http://vikranthit.blogspot.com/
My Java :
http://base.google.com/base/search?authorid=1842707&hl=en&gl=US

 

Post a Comment

<< Home