<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress.com" -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	>

<channel>
	<title>right-join &amp;laquo; WordPress.com Tag Feed</title>
	<link>http://en.wordpress.com/tag/right-join/</link>
	<description>Feed of posts on WordPress.com tagged "right-join"</description>
	<pubDate>Sat, 05 Dec 2009 08:04:17 +0000</pubDate>

	<generator>http://en.wordpress.com/tags/</generator>
	<language>en</language>

<item>
<title><![CDATA[basic of SQL Statement (part 6)]]></title>
<link>http://sevenlamp.wordpress.com/2009/09/18/sqlstatementp6/</link>
<pubDate>Fri, 18 Sep 2009 03:30:42 +0000</pubDate>
<dc:creator>sevenlamp</dc:creator>
<guid>http://sevenlamp.wordpress.com/2009/09/18/sqlstatementp6/</guid>
<description><![CDATA[Sql statement ေတြဘက္ကိုေတာ့ မလွည့္ျဖစ္တာ ေတာ္ေတာ္ၾကာသြားျပီ။ ကြၽန္ေတာ္လည္း ေရးခ်င္တာေတြကေတာ့ အမ်ားၾက]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p style="text-align:justify;">Sql statement ေတြဘက္ကိုေတာ့ မလွည့္ျဖစ္တာ ေတာ္ေတာ္ၾကာသြားျပီ။ ကြၽန္ေတာ္လည္း ေရးခ်င္တာေတြကေတာ့ အမ်ားၾကီး အခ်ိန္ကမရွိဘူးျဖစ္ေနတာနဲ႔ ေရွ႕မဆက္နိုင္ဘူးျဖစ္ေနတာ။ ဘာဘဲျဖစ္ျဖစ္ ရတဲ႔အခ်ိန္ေလးလုျပီး ကြၽန္ေတာ္သိသေလာက္ေတာ့ ထပ္ေရးေနဦးမွာမို႔ အားေပးၾကပါဦးဗ်ာ။ ကဲ… ကြၽန္ေတာ္တို႔ select statement ကို ေျပာလက္စ ဆက္ပါဦးမယ္။ table တစ္ခုထဲက data ကို ထုတ္ၾကည့္တာကိုေတာ့ နားလည္ေလာက္ျပီထင္ပါတယ္။ အခု ကြၽန္ေတာ္တို႔ တစ္ခုထက္ပိုတဲ႔ table ေတြထဲက data ေတြကို ထုတ္ၾကည့္ရေအာင္။ ဥပမာဗ်ာ… item table ထဲက record အားလံုးျပမယ္ဆိုပါေတာ့။ ဒါေပမယ့္ categoryid အစား categoryname ကိုျပခ်င္တယ္ဆိုရင္၊ item table နဲ႔ category table ၂ ခုကို ခ်ိတ္ရေတာ့မွာပါ။ ဟုတ္ျပီ… ဒါဆိုရင္ table ေတြ ခ်ိတ္ဖို႔အတြက္ ဘာေတြလိုလဲ… ?</p>
<p style="text-align:justify;">ဟုတ္ကဲ႔ table တစ္ခုနဲ႔ တစ္ခုခ်ိတ္ဆက္တာကို join လုပ္တယ္လို႔ ေခၚပါတယ္။ join နည္း ၃ မ်ိဳးရွိပါတယ္။</p>
<p><span style="color:#ff00ff;">၁) cross join</span></p>
<p>Cross join ကေတာ့ table ၂ ခုကို comer(<strong><span style="color:#ff00ff;">,</span></strong>) ခံျပီး join တာပါ။ ဒီလိုမ်ိဳးပါ</p>
<div style="background-color:lightgray;padding:5px;">
<pre>select item.*, category.categoryName
from item<span style="color:#ff00ff;">,</span> category
</pre>
</div>
<p><em>result</em></p>
<table border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td width="56" valign="top"><strong>ItemID</strong></td>
<td width="191" valign="top"><strong>ItemName</strong></td>
<td width="84" valign="top"><strong>CategoryID</strong></td>
<td width="78" valign="top"><strong>Price</strong></td>
<td width="63" valign="top"><strong>Balance</strong></td>
<td width="61" valign="top"><strong>Remark</strong></td>
<td width="105" valign="top"><strong>CategoryName</strong></td>
</tr>
<tr>
<td width="56" valign="top">1</td>
<td width="191" valign="top">Sony Optical Mouse</td>
<td width="84" valign="top">1</td>
<td width="78" valign="top">2500</td>
<td width="63" valign="top">10</td>
<td width="61" valign="top">-</td>
<td width="105" valign="top">Computer</td>
</tr>
<tr>
<td width="56" valign="top">2</td>
<td width="191" valign="top">A4 Tech Keyboard</td>
<td width="84" valign="top">1</td>
<td width="78" valign="top">5000</td>
<td width="63" valign="top">1</td>
<td width="61" valign="top">-</td>
<td width="105" valign="top">Computer</td>
</tr>
<tr>
<td width="56" valign="top">3</td>
<td width="191" valign="top">ViewSonic 17” Flat CRT</td>
<td width="84" valign="top">1</td>
<td width="78" valign="top">15000</td>
<td width="63" valign="top">5</td>
<td width="61" valign="top">-</td>
<td width="105" valign="top">Computer</td>
</tr>
<tr>
<td width="56" valign="top">4</td>
<td width="191" valign="top">First Eleven Book</td>
<td width="84" valign="top">2</td>
<td width="78" valign="top">500</td>
<td width="63" valign="top">15</td>
<td width="61" valign="top">-</td>
<td width="105" valign="top">Computer</td>
</tr>
<tr>
<td width="56" valign="top">5</td>
<td width="191" valign="top">SONY 32” TV</td>
<td width="84" valign="top">3</td>
<td width="78" valign="top">900000</td>
<td width="63" valign="top">1</td>
<td width="61" valign="top">-</td>
<td width="105" valign="top">Computer</td>
</tr>
<tr>
<td width="56" valign="top">6</td>
<td width="191" valign="top">Moon 16” Stand Fan</td>
<td width="84" valign="top">3</td>
<td width="78" valign="top">20000</td>
<td width="63" valign="top">3</td>
<td width="61" valign="top">-</td>
<td width="105" valign="top">Computer</td>
</tr>
<tr>
<td width="56" valign="top">1</td>
<td width="191" valign="top">Sony Optical Mouse</td>
<td width="84" valign="top">1</td>
<td width="78" valign="top">2500</td>
<td width="63" valign="top">10</td>
<td width="61" valign="top">-</td>
<td width="105" valign="top">Stationary</td>
</tr>
<tr>
<td width="56" valign="top">2</td>
<td width="191" valign="top">A4 Tech Keyboard</td>
<td width="84" valign="top">1</td>
<td width="78" valign="top">5000</td>
<td width="63" valign="top">1</td>
<td width="61" valign="top">-</td>
<td width="105" valign="top">Stationary</td>
</tr>
<tr>
<td width="56" valign="top">3</td>
<td width="191" valign="top">ViewSonic 17” Flat CRT</td>
<td width="84" valign="top">1</td>
<td width="78" valign="top">15000</td>
<td width="63" valign="top">5</td>
<td width="61" valign="top">-</td>
<td width="105" valign="top">Stationary</td>
</tr>
<tr>
<td width="56" valign="top">4</td>
<td width="191" valign="top">First Eleven Book</td>
<td width="84" valign="top">2</td>
<td width="78" valign="top">500</td>
<td width="63" valign="top">15</td>
<td width="61" valign="top">-</td>
<td width="105" valign="top">Stationary</td>
</tr>
<tr>
<td width="56" valign="top">5</td>
<td width="191" valign="top">SONY 32” TV</td>
<td width="84" valign="top">3</td>
<td width="78" valign="top">900000</td>
<td width="63" valign="top">1</td>
<td width="61" valign="top">-</td>
<td width="105" valign="top">Stationary</td>
</tr>
<tr>
<td width="56" valign="top">6</td>
<td width="191" valign="top">Moon 16” Stand Fan</td>
<td width="84" valign="top">3</td>
<td width="78" valign="top">20000</td>
<td width="63" valign="top">3</td>
<td width="61" valign="top">-</td>
<td width="105" valign="top">Stationary</td>
</tr>
<tr>
<td width="56" valign="top">1</td>
<td width="191" valign="top">Sony Optical Mouse</td>
<td width="84" valign="top">1</td>
<td width="78" valign="top">2500</td>
<td width="63" valign="top">10</td>
<td width="61" valign="top">-</td>
<td width="105" valign="top">Electronic</td>
</tr>
<tr>
<td width="56" valign="top">2</td>
<td width="191" valign="top">A4 Tech Keyboard</td>
<td width="84" valign="top">1</td>
<td width="78" valign="top">5000</td>
<td width="63" valign="top">1</td>
<td width="61" valign="top">-</td>
<td width="105" valign="top">Electronic</td>
</tr>
<tr>
<td width="56" valign="top">3</td>
<td width="191" valign="top">ViewSonic 17” Flat CRT</td>
<td width="84" valign="top">1</td>
<td width="78" valign="top">15000</td>
<td width="63" valign="top">5</td>
<td width="61" valign="top">-</td>
<td width="105" valign="top">Electronic</td>
</tr>
<tr>
<td width="56" valign="top">4</td>
<td width="191" valign="top">First Eleven Book</td>
<td width="84" valign="top">2</td>
<td width="78" valign="top">500</td>
<td width="63" valign="top">15</td>
<td width="61" valign="top">-</td>
<td width="105" valign="top">Electronic</td>
</tr>
<tr>
<td width="56" valign="top">5</td>
<td width="191" valign="top">SONY 32” TV</td>
<td width="84" valign="top">3</td>
<td width="78" valign="top">900000</td>
<td width="63" valign="top">1</td>
<td width="61" valign="top">-</td>
<td width="105" valign="top">Electronic</td>
</tr>
<tr>
<td width="56" valign="top">6</td>
<td width="191" valign="top">Moon 16” Stand Fan</td>
<td width="84" valign="top">3</td>
<td width="78" valign="top">20000</td>
<td width="63" valign="top">3</td>
<td width="61" valign="top">-</td>
<td width="105" valign="top">Electronic</td>
</tr>
<tr>
<td width="56" valign="top">1</td>
<td width="191" valign="top">Sony Optical Mouse</td>
<td width="84" valign="top">1</td>
<td width="78" valign="top">2500</td>
<td width="63" valign="top">10</td>
<td width="61" valign="top">-</td>
<td width="105" valign="top">Clothes</td>
</tr>
<tr>
<td width="56" valign="top">2</td>
<td width="191" valign="top">A4 Tech Keyboard</td>
<td width="84" valign="top">1</td>
<td width="78" valign="top">5000</td>
<td width="63" valign="top">1</td>
<td width="61" valign="top">-</td>
<td width="105" valign="top">Clothes</td>
</tr>
<tr>
<td width="56" valign="top">3</td>
<td width="191" valign="top">ViewSonic 17” Flat CRT</td>
<td width="84" valign="top">1</td>
<td width="78" valign="top">15000</td>
<td width="63" valign="top">5</td>
<td width="61" valign="top">-</td>
<td width="105" valign="top">Clothes</td>
</tr>
<tr>
<td width="56" valign="top">4</td>
<td width="191" valign="top">First Eleven Book</td>
<td width="84" valign="top">2</td>
<td width="78" valign="top">500</td>
<td width="63" valign="top">15</td>
<td width="61" valign="top">-</td>
<td width="105" valign="top">Clothes</td>
</tr>
<tr>
<td width="56" valign="top">5</td>
<td width="191" valign="top">SONY 32” TV</td>
<td width="84" valign="top">3</td>
<td width="78" valign="top">900000</td>
<td width="63" valign="top">1</td>
<td width="61" valign="top">-</td>
<td width="105" valign="top">Clothes</td>
</tr>
<tr>
<td width="56" valign="top">6</td>
<td width="191" valign="top">Moon 16” Stand Fan</td>
<td width="84" valign="top">3</td>
<td width="78" valign="top">20000</td>
<td width="63" valign="top">3</td>
<td width="61" valign="top">-</td>
<td width="105" valign="top">Clothes</td>
</tr>
<tr>
<td width="56" valign="top">1</td>
<td width="191" valign="top">Sony Optical Mouse</td>
<td width="84" valign="top">1</td>
<td width="78" valign="top">2500</td>
<td width="63" valign="top">10</td>
<td width="61" valign="top">-</td>
<td width="105" valign="top">Kitchen wares</td>
</tr>
<tr>
<td width="56" valign="top">2</td>
<td width="191" valign="top">A4 Tech Keyboard</td>
<td width="84" valign="top">1</td>
<td width="78" valign="top">5000</td>
<td width="63" valign="top">1</td>
<td width="61" valign="top">-</td>
<td width="105" valign="top">Kitchen wares</td>
</tr>
<tr>
<td width="56" valign="top">3</td>
<td width="191" valign="top">ViewSonic 17” Flat CRT</td>
<td width="84" valign="top">1</td>
<td width="78" valign="top">15000</td>
<td width="63" valign="top">5</td>
<td width="61" valign="top">-</td>
<td width="105" valign="top">Kitchen wares</td>
</tr>
<tr>
<td width="56" valign="top">4</td>
<td width="191" valign="top">First Eleven Book</td>
<td width="84" valign="top">2</td>
<td width="78" valign="top">500</td>
<td width="63" valign="top">15</td>
<td width="61" valign="top">-</td>
<td width="105" valign="top">Kitchen wares</td>
</tr>
<tr>
<td width="56" valign="top">5</td>
<td width="191" valign="top">SONY 32” TV</td>
<td width="84" valign="top">3</td>
<td width="78" valign="top">900000</td>
<td width="63" valign="top">1</td>
<td width="61" valign="top">-</td>
<td width="105" valign="top">Kitchen wares</td>
</tr>
<tr>
<td width="56" valign="top">6</td>
<td width="191" valign="top">Moon 16” Stand Fan</td>
<td width="84" valign="top">3</td>
<td width="78" valign="top">20000</td>
<td width="63" valign="top">3</td>
<td width="61" valign="top">-</td>
<td width="105" valign="top">Kitchen wares</td>
</tr>
</tbody>
</table>
<p style="text-align:justify;">cross join လို႔ ဆိုတဲ႔ အတိုင္းပါပဲ။ ဒီပံုစံက table ၂ ခုကို ေျမွာက္လို႔ရတဲ႔ အတိုင္း result ထြက္ပါတယ္။ item ထဲမွာက record ၆ ေၾကာင္း၊ category ထဲမွာက record ၅ ေၾကာင္း ရွိပါတယ္။ ဒါေၾကာင့္ အေပၚက query ကို run လိုက္ရင္ record စုစုေပါင္း အေၾကာင္း ၃၀ ထြက္ပါလိမ့္မယ္။ item table ထဲက record ၁ေၾကာင္းစီ အတြက္ category table ထဲမွာ record ရွိသေလာက္ကို ထုတ္ျပသြားတာပါ။ ကြၽန္ေတာ္က ့ အကုန္မထုတ္ပဲ item table ထဲမွာ ပါတဲ႔ categoryID နဲ႔ category table ထဲမွာပါတဲ႔ categoryID တူတဲ႔ record ေတြပဲ ထုတ္ျပခ်င္တယ္ဆိုရင္ေတာ့ ထြက္လာတဲ႔ result ကို condition ျပန္စစ္ဖို႔ လိုပါတယ္။ ဒါေၾကာင့္ ကြၽန္ေတာ္တို႔ where ကို သံုးျပီး အခုလို ေရးရပါမယ္။</p>
<div style="background-color:lightgray;padding:5px;">
<pre>select item.*, category.categoryName
from item<span style="color:#ff00ff;">,</span> category <span style="color:#ff00ff;">where</span> item.categoryID = category.categoryID
</pre>
</div>
<p><em>result</em></p>
<table border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td width="56" valign="top"><strong>ItemID</strong></td>
<td width="185" valign="top"><strong>ItemName</strong></td>
<td width="84" valign="top"><strong>CategoryID</strong></td>
<td width="84" valign="top"><strong>Price</strong></td>
<td width="63" valign="top"><strong>Balance</strong></td>
<td width="61" valign="top"><strong>Remark</strong></td>
<td width="105" valign="top"><strong>CategoryName</strong></td>
</tr>
<tr>
<td width="56" valign="top">1</td>
<td width="185" valign="top">Sony Optical Mouse</td>
<td width="84" valign="top">1</td>
<td width="84" valign="top">2500</td>
<td width="63" valign="top">10</td>
<td width="61" valign="top">-</td>
<td width="105" valign="top">Computer</td>
</tr>
<tr>
<td width="56" valign="top">2</td>
<td width="185" valign="top">A4 Tech Keyboard</td>
<td width="84" valign="top">1</td>
<td width="84" valign="top">5000</td>
<td width="63" valign="top">1</td>
<td width="61" valign="top">-</td>
<td width="105" valign="top">Computer</td>
</tr>
<tr>
<td width="56" valign="top">3</td>
<td width="185" valign="top">ViewSonic 17” Flat CRT</td>
<td width="84" valign="top">1</td>
<td width="84" valign="top">15000</td>
<td width="63" valign="top">5</td>
<td width="61" valign="top">-</td>
<td width="105" valign="top">Computer</td>
</tr>
<tr>
<td width="56" valign="top">4</td>
<td width="185" valign="top">First Eleven Book</td>
<td width="84" valign="top">2</td>
<td width="84" valign="top">500</td>
<td width="63" valign="top">15</td>
<td width="61" valign="top">-</td>
<td width="105" valign="top">Stationary</td>
</tr>
<tr>
<td width="56" valign="top">5</td>
<td width="185" valign="top">SONY 32” TV</td>
<td width="84" valign="top">3</td>
<td width="84" valign="top">900000</td>
<td width="63" valign="top">1</td>
<td width="61" valign="top">-</td>
<td width="105" valign="top">Electronic</td>
</tr>
<tr>
<td width="56" valign="top">6</td>
<td width="185" valign="top">Moon 16” Stand Fan</td>
<td width="84" valign="top">3</td>
<td width="84" valign="top">20000</td>
<td width="63" valign="top">3</td>
<td width="61" valign="top">-</td>
<td width="105" valign="top">Electronic</td>
</tr>
</tbody>
</table>
<p><span style="color:#ff00ff;">၂) inner join</span><br />
Inner join က cross join ကို where ထည့္ထားတဲ႔ ပံုစံနဲ႔ တူပါတယ္။ table ၂ ခုကို join ဖို႔အတြက္ On keyword ကိုသံုးပါတယ္။ ဒီလိုမ်ိဳးပါ..</p>
<div style="background-color:lightgray;padding:5px;">
<pre>select item.*, category.categoryName
from item <span style="color:#ff00ff;">inner join</span> category
<span style="color:#ff00ff;">on</span> item.categoryID = category.categoryID
</pre>
</div>
<p><em>result</em></p>
<table border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td width="56" valign="top"><strong>ItemID</strong></td>
<td width="185" valign="top"><strong>ItemName</strong></td>
<td width="84" valign="top"><strong>CategoryID</strong></td>
<td width="84" valign="top"><strong>Price</strong></td>
<td width="63" valign="top"><strong>Balance</strong></td>
<td width="61" valign="top"><strong>Remark</strong></td>
<td width="105" valign="top"><strong>CategoryName</strong></td>
</tr>
<tr>
<td width="56" valign="top">1</td>
<td width="185" valign="top">Sony Optical Mouse</td>
<td width="84" valign="top">1</td>
<td width="84" valign="top">2500</td>
<td width="63" valign="top">10</td>
<td width="61" valign="top">-</td>
<td width="105" valign="top">Computer</td>
</tr>
<tr>
<td width="56" valign="top">2</td>
<td width="185" valign="top">A4 Tech Keyboard</td>
<td width="84" valign="top">1</td>
<td width="84" valign="top">5000</td>
<td width="63" valign="top">1</td>
<td width="61" valign="top">-</td>
<td width="105" valign="top">Computer</td>
</tr>
<tr>
<td width="56" valign="top">3</td>
<td width="185" valign="top">ViewSonic 17” Flat CRT</td>
<td width="84" valign="top">1</td>
<td width="84" valign="top">15000</td>
<td width="63" valign="top">5</td>
<td width="61" valign="top">-</td>
<td width="105" valign="top">Computer</td>
</tr>
<tr>
<td width="56" valign="top">4</td>
<td width="185" valign="top">First Eleven Book</td>
<td width="84" valign="top">2</td>
<td width="84" valign="top">500</td>
<td width="63" valign="top">15</td>
<td width="61" valign="top">-</td>
<td width="105" valign="top">Stationary</td>
</tr>
<tr>
<td width="56" valign="top">5</td>
<td width="185" valign="top">SONY 32” TV</td>
<td width="84" valign="top">3</td>
<td width="84" valign="top">900000</td>
<td width="63" valign="top">1</td>
<td width="61" valign="top">-</td>
<td width="105" valign="top">Electronic</td>
</tr>
<tr>
<td width="56" valign="top">6</td>
<td width="185" valign="top">Moon 16” Stand Fan</td>
<td width="84" valign="top">3</td>
<td width="84" valign="top">20000</td>
<td width="63" valign="top">3</td>
<td width="61" valign="top">-</td>
<td width="105" valign="top">Electronic</td>
</tr>
</tbody>
</table>
<p>inner join နဲ႔ေရးထားတဲ႔အတြက္ item table ထဲက categoryID က category table ထဲမွာ ရွိမေနဘူးဆိုရင္ အဲဒီ record ကို ထုတ္ျပေပးမွာမဟုတ္ပါဘူး။ ဟုတ္ျပီ တစ္ခါတေလမွာ ကြၽန္ေတာ္က item table ထဲက အားလံုးထုတ္ျပခ်င္တယ္၊ category table ထဲမွာ ရွိရင္ category name ျပျပီး၊ မရွိရင္ေတာ့ null ပဲေပၚလာခ်င္တယ္ဆိုရင္။ တနည္းအားျဖင့္ table တခုခုကို မူတည္ျပီး ျပခ်င္တယ္ဆိုရင္ ကြၽန္ေတာ္တို႔ inner join ကိုသံုးလို႔ မရေတာ့ပါဘူး။ ဒီအခါမွာ outer join ကိုသံုးရေတာ့မွာပါ။</p>
<p><span style="color:#ff00ff;">၃) outer join</span><br />
Outer join ကို ၂ ပိုင္းထပ္ခြဲနိုင္ပါတယ္။ Left outer join နဲ႔ Right outer join ပါ။ join keyword ရဲ႕ ဘယ္ဖက္က table ကို အကုန္ျပမယ္ဆိုရင္ left join နဲ႔ ေရးရျပီး။ join keyword ရဲ႕ ညာဖက္က table ကို အကုန္ျပခ်င္ရင္ေတာ့ right join နဲ႔ ေရးပါမယ္။</p>
<div style="background-color:lightgray;padding:5px;">
<pre>select item.*, category.categoryName
from item <span style="color:#ff00ff;">left join</span> category
<span style="color:#ff00ff;">on </span>item.categoryID = category.categoryID
</pre>
</div>
<p><em>result</em></p>
<table border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td width="56" valign="top"><strong>ItemID</strong></td>
<td width="185" valign="top"><strong>ItemName</strong></td>
<td width="84" valign="top"><strong>CategoryID</strong></td>
<td width="84" valign="top"><strong>Price</strong></td>
<td width="63" valign="top"><strong>Balance</strong></td>
<td width="61" valign="top"><strong>Remark</strong></td>
<td width="105" valign="top"><strong>CategoryName</strong></td>
</tr>
<tr>
<td width="56" valign="top">1</td>
<td width="185" valign="top">Sony Optical Mouse</td>
<td width="84" valign="top">1</td>
<td width="84" valign="top">2500</td>
<td width="63" valign="top">10</td>
<td width="61" valign="top">-</td>
<td width="105" valign="top">Computer</td>
</tr>
<tr>
<td width="56" valign="top">2</td>
<td width="185" valign="top">A4 Tech Keyboard</td>
<td width="84" valign="top">1</td>
<td width="84" valign="top">5000</td>
<td width="63" valign="top">1</td>
<td width="61" valign="top">-</td>
<td width="105" valign="top">Computer</td>
</tr>
<tr>
<td width="56" valign="top">3</td>
<td width="185" valign="top">ViewSonic 17” Flat CRT</td>
<td width="84" valign="top">1</td>
<td width="84" valign="top">15000</td>
<td width="63" valign="top">5</td>
<td width="61" valign="top">-</td>
<td width="105" valign="top">Computer</td>
</tr>
<tr>
<td width="56" valign="top">4</td>
<td width="185" valign="top">First Eleven Book</td>
<td width="84" valign="top">2</td>
<td width="84" valign="top">500</td>
<td width="63" valign="top">15</td>
<td width="61" valign="top">-</td>
<td width="105" valign="top">Stationary</td>
</tr>
<tr>
<td width="56" valign="top">5</td>
<td width="185" valign="top">SONY 32” TV</td>
<td width="84" valign="top">3</td>
<td width="84" valign="top">900000</td>
<td width="63" valign="top">1</td>
<td width="61" valign="top">-</td>
<td width="105" valign="top">Electronic</td>
</tr>
<tr>
<td width="56" valign="top">6</td>
<td width="185" valign="top">Moon 16” Stand Fan</td>
<td width="84" valign="top">3</td>
<td width="84" valign="top">20000</td>
<td width="63" valign="top">3</td>
<td width="61" valign="top">-</td>
<td width="105" valign="top">Electronic</td>
</tr>
</tbody>
</table>
<div style="background-color:lightgray;padding:5px;">
<pre>select item.*, category.categoryName
from item <span style="color:#ff00ff;">right join</span> category
<span style="color:#ff00ff;">on </span>item.categoryID = category.categoryID
</pre>
</div>
<p><em>result</em></p>
<table border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td width="56" valign="top"><strong>ItemID</strong></td>
<td width="173" valign="top"><strong>ItemName</strong></td>
<td width="84" valign="top"><strong>CategoryID</strong></td>
<td width="96" valign="top"><strong>Price</strong></td>
<td width="63" valign="top"><strong>Balance</strong></td>
<td width="61" valign="top"><strong>Remark</strong></td>
<td width="105" valign="top"><strong>CategoryName</strong></td>
</tr>
<tr>
<td width="56" valign="top">1</td>
<td width="173" valign="top">Sony Optical Mouse</td>
<td width="84" valign="top">1</td>
<td width="96" valign="top">2500</td>
<td width="63" valign="top">10</td>
<td width="61" valign="top">-</td>
<td width="105" valign="top">Computer</td>
</tr>
<tr>
<td width="56" valign="top">2</td>
<td width="173" valign="top">A4 Tech Keyboard</td>
<td width="84" valign="top">1</td>
<td width="96" valign="top">5000</td>
<td width="63" valign="top">1</td>
<td width="61" valign="top">-</td>
<td width="105" valign="top">Computer</td>
</tr>
<tr>
<td width="56" valign="top">3</td>
<td width="173" valign="top">ViewSonic 17” Flat CRT</td>
<td width="84" valign="top">1</td>
<td width="96" valign="top">15000</td>
<td width="63" valign="top">5</td>
<td width="61" valign="top">-</td>
<td width="105" valign="top">Computer</td>
</tr>
<tr>
<td width="56" valign="top">4</td>
<td width="173" valign="top">First Eleven Book</td>
<td width="84" valign="top">2</td>
<td width="96" valign="top">500</td>
<td width="63" valign="top">15</td>
<td width="61" valign="top">-</td>
<td width="105" valign="top">Stationary</td>
</tr>
<tr>
<td width="56" valign="top">5</td>
<td width="173" valign="top">SONY 32” TV</td>
<td width="84" valign="top">3</td>
<td width="96" valign="top">900000</td>
<td width="63" valign="top">1</td>
<td width="61" valign="top">-</td>
<td width="105" valign="top">Electronic</td>
</tr>
<tr>
<td width="56" valign="top">6</td>
<td width="173" valign="top">Moon 16” Stand Fan</td>
<td width="84" valign="top">3</td>
<td width="96" valign="top">20000</td>
<td width="63" valign="top">3</td>
<td width="61" valign="top">-</td>
<td width="105" valign="top">Electronic</td>
</tr>
<tr>
<td width="56" valign="top">NULL</td>
<td width="173" valign="top">NULL</td>
<td width="84" valign="top">NULL</td>
<td width="96" valign="top">NULL</td>
<td width="63" valign="top">NULL</td>
<td width="61" valign="top">NULL</td>
<td width="105" valign="top">Clothes</td>
</tr>
<tr>
<td width="56" valign="top">NULL</td>
<td width="173" valign="top">NULL</td>
<td width="84" valign="top">NULL</td>
<td width="96" valign="top">NULL</td>
<td width="63" valign="top">NULL</td>
<td width="61" valign="top">NULL</td>
<td width="105" valign="top">Kitchen wares</td>
</tr>
</tbody>
</table>
<p>left join လို႔ေရးေရး left outer join လို႔ပဲ ေရးေရး တူတူပါပဲ။ right join နဲ႔ right outer join လည္း တူတယ္ေနာ္။ ေနာက္ဆံုး query result မွာ null ေတြထြက္လာတာ ေတြ႔ရတယ္ေနာ္၊ ဘာလို႔လဲဆိုေတာ့ category table ကို မူတည္ျပီး ယူလိုက္ေတာ့ item table ထဲမွာ မရွိတာေတြအတြက္ null ေတြထြက္လာတာပါ။</p>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[Trabalhando com JOIN (ou Junções)]]></title>
<link>http://sqlfromhell.wordpress.com/2009/09/15/trabalhando-com-join/</link>
<pubDate>Tue, 15 Sep 2009 03:25:25 +0000</pubDate>
<dc:creator>Paulo R. Pereira</dc:creator>
<guid>http://sqlfromhell.wordpress.com/2009/09/15/trabalhando-com-join/</guid>
<description><![CDATA[Boa noite pessoal! Navegando pela internet, me deparei com um ótimo guia visual sobre os JOINs. Como]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p>Boa noite pessoal!</p>
<p>Navegando pela internet, me deparei com um ótimo guia visual sobre os JOINs. Como o próprio autor do guia visual disse <em>“As coisas parecem fazer sentido com imagens. Eu olhei pela internet por uma boa representação gráfica dos SQL JOINs, mas não pude encontrar nada que me agradasse.” (tradução livre)</em>, concordo com o autor, pois muitos exemplos pela internet complicam mais do que explicam como funcionam os JOINs, mas o guia que ele desenvolveu ficou muito bom.</p>
<p>Alguns vão achar este tópico um pouco “básico” em comparação aos que tenho escrito, mas eu e muitas pessoas que indiquei o guia inglês acharam este guia muito interessante, e com este post estarei “traduzindo” o guia, esperando que seja de utilidade didática e até mesmo pessoal para vocês. Principalmente para que aprendamos como expressar melhor conceitos para nós são tão simples, mas que muitas vezes não sabemos explicar de uma forma adequada.</p>
<p>Irei abordadar neste post sete diferentes formas de retornar dados de duas tabelas relacionadas, mas além destes existem outros JOINS. Os JOINs demonstrados serão os seguintes:</p>
<ol>
<li>INNER JOIN</li>
<li>LEFT JOIN</li>
<li>RIGHT JOIN</li>
<li>OUTER JOIN</li>
<li>LEFT JOIN “excluindo” INNER JOIN</li>
<li>RIGHT JOIN “excluindo” INNER JOIN</li>
<li>OUTER JOIN “excluindo” INNER JOIN</li>
</ol>
<p><em>Obs.: Os JOINs 5, 6 e 7 são na verdade variações dos anteriores, mas ainda podem ser considerados JOINs, devido aos seus comportamentos.</em></p>
<p><strong>INNER JOIN</strong></p>
<p><img src="http://sqlfromhell.wordpress.com/files/2009/09/j1.png" alt="" /></p>
<p>Este é simples, o mais entendível e o mais comum. Esta consulta retornará todos os registros da tabela esquerda (tabela A) que têm correspondência com a tabela direita (tabela B). Podemos escrever este JOIN da seguinte forma:</p>
<div style="border:1px;background-color:#000000;color:#ffffff;font-weight:bold;border-color:#ffffff;margin:5px;padding:5px;"><code>SELECT *<br />
FROM A<br />
INNER JOIN B<br />
ON A.Key = B.Key</code></div>
<p><strong>LEFT JOIN</strong></p>
<p><img src="http://sqlfromhell.wordpress.com/files/2009/09/j2.png" alt="" /></p>
<p>Esta consulta retorna todos os registros da tabela esquerda (tabela A) e as correspondências que existirem com a tabela direita (tabela B). O código ficará da seguinte forma:</p>
<div style="border:1px;background-color:#000000;color:#ffffff;font-weight:bold;border-color:#ffffff;margin:5px;padding:5px;"><code>SELECT *<br />
FROM A<br />
LEFT JOIN B<br />
ON A.Key = B.Key</code></div>
<p><strong>RIGHT JOIN</strong></p>
<p><img src="http://sqlfromhell.wordpress.com/files/2009/09/j3.png" alt="" /> </p>
<p>Esta consulta retornará todos os registros da tabela direita (tabela B) e as correspondências que existirem com a tabela esquerda (tabela A). O código ficará da seguinte forma:</p>
<div style="border:1px;background-color:#000000;color:#ffffff;font-weight:bold;border-color:#ffffff;margin:5px;padding:5px;"><code>SELECT *<br />
FROM A<br />
RIGHT JOIN B<br />
ON A.Key = B.Key</code></div>
<p><strong>OUTER JOIN</strong></p>
<p><img src="http://sqlfromhell.wordpress.com/files/2009/09/j4.png" alt="" /></p>
<p>Este JOIN também é conhecido como FULL OUTER JOIN ou FULL JOIN. Esta consulta retornará todos os registros das duas tabelas e juntando também os registros correspondentes entres as duas tabelas. O código ficará da seguinte forma:</p>
<div style="border:1px;background-color:#000000;color:#ffffff;font-weight:bold;border-color:#ffffff;margin:5px;padding:5px;"><code>SELECT *<br />
FROM A<br />
FULL OUTER JOIN B B<br />
ON A.Key = B.Key</code></div>
<p><strong>LEFT Excluding JOIN</strong></p>
<p><img src="http://sqlfromhell.wordpress.com/files/2009/09/j5.png" alt="" /></p>
<p>Esta consulta retornará apenas os registros da tabela esquerda que não têm correspondência com a tabela direita. O código ficará da seguinte forma:</p>
<div style="border:1px;background-color:#000000;color:#ffffff;font-weight:bold;border-color:#ffffff;margin:5px;padding:5px;"><code>SELECT *<br />
FROM A<br />
LEFT JOIN B<br />
ON A.Key = B.Key<br />
WHERE B.Key IS NULL</code></div>
<p><strong>RIGHT “excluindo” JOIN</strong></p>
<p><img src="http://sqlfromhell.wordpress.com/files/2009/09/j6.png" alt="" /></p>
<p>Esta consulta retornará todos os registros da tabela esquerda que não têm correspondência com a tabela direita. O código ficará da seguinte forma:</p>
<div style="border:1px;background-color:#000000;color:#ffffff;font-weight:bold;border-color:#ffffff;margin:5px;padding:5px;"><code>SELECT *<br />
FROM A<br />
RIGHT JOIN B<br />
ON A.Key = B.Key<br />
WHERE A.Key IS NULL</code></div>
<p><strong>OUTER “excluindo” </strong><strong>JOIN</strong></p>
<p><img src="http://sqlfromhell.wordpress.com/files/2009/09/j7.png" alt="" /></p>
<p>Esta consulta retornará todos os registros das duas tabelas que não têm correspondência. O código ficará da seguinte forma:</p>
<div style="border:1px;background-color:#000000;color:#ffffff;font-weight:bold;border-color:#ffffff;margin:5px;padding:5px;"><code>SELECT *<br />
FROM A<br />
FULL OUTER JOIN B<br />
ON A.Key = B.Key<br />
WHERE A.Key IS NULL OR B.Key IS NULL</code></div>
<p>Para quem tiver interesse na figura completa com os JOINs demonstrados, segue abaixo o link:</p>
<p><a href="http://www.codeproject.com/KB/database/Visual_SQL_Joins/Visual_SQL_JOINS_orig.jpg" target="_blank"><img src="http://sqlfromhell.wordpress.com/files/2009/09/j8.png" alt="" /></a> </p>
<p>O autor do artigo original se chama <a href="http://www.codeproject.com/Members/Christopher-Moffatt" target="_blank"><strong>C.L. Moffatt</strong></a>, quem tiver interesse no artigo original e os exemplos que este autor utilizou, segue o link abaixo:</p>
<p><a href="http://www.codeproject.com/KB/database/Visual_SQL_Joins.aspx" target="_blank">http://www.codeproject.com/KB/database/Visual_SQL_Joins.aspx</a></p>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[Detecting Orphaned Rows]]></title>
<link>http://sqlbyminh.wordpress.com/2009/04/24/detecting-orphaned-rows/</link>
<pubDate>Fri, 24 Apr 2009 23:39:02 +0000</pubDate>
<dc:creator>sqlbyminh</dc:creator>
<guid>http://sqlbyminh.wordpress.com/2009/04/24/detecting-orphaned-rows/</guid>
<description><![CDATA[This post is a continuation of the post on SQL joins. Hopefully you have a better understanding of t]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p>This post is a continuation of the <a href="http://sqlbyminh.wordpress.com/2009/04/23/sql-joins/" target="_blank">post</a> on SQL joins.  Hopefully you have a better understanding of the types of joins available to you.  Now it&#8217;s time to see how we can use them to detect <em>orphaned</em> rows.</p>
<p>An orphaned row is a child row that has no parent row.  Going back to our example of the invoice metaphor, an invoice is usally broken down to two or more tables.  The <em>header</em> table contains rows that carry information about the invoice as a whole.  It would contain columns that usually have the following type of data:</p>
<ul>
<li>CUSTOMER_ID</li>
<li>INVOICE_ID</li>
<li>INVOICE_NUMBER</li>
<li>INVOICE_DATE</li>
<li>INVOICE_REF</li>
</ul>
<p>The invoice detail table usually contains information specific to an invoice line item.  An example of some columns that can be found in this type of table are:</p>
<ul>
<li>INVOICE_LINE_ID</li>
<li>INVOICE_ID</li>
<li>LINE_NUMBER</li>
<li>ITEM_NUMBER</li>
<li>QTY</li>
<li>UNIT_PRICE</li>
<li>LINE_TOTAL</li>
</ul>
<p>In the example above it is often said that the invoice header is the <em>parent</em> of the invoice detail and that the invoice detail is the <em>child</em> of the invoice header.  Normally, this relationship is easy to enforce.  Suppose however that the relationship hasn&#8217;t been enforced and you suspect that you have some orphaned detail records.  How could you go about finding them?</p>
<p>This is where the OUTER JOINS that we covered in our previous post comes in handy.  Again, using our previous test data that was defined in the previous <a href="http://sqlbyminh.wordpress.com/2009/04/23/sql-joins/" target="_blank">post</a> let&#8217;s write a query that finds any <em>invoice detail</em> line that does not have a invoice header associated to it.  Run the query below:</p>
<p><u>Code</u><br />
<code></p>
<pre>
select
   det.*
from dbo.invHdr hdr (nolock)
right join dbo.invDet det (nolock)
on det.invnum = hdr.invnum
where
   hdr.invnum is null
</pre>
<p></code></p>
<p>Notice that it returns one row.  If you were to visually inspect the data in the detail table you would find that this is the row that is missing an invoice header.  Notice that we are doing a RIGHT JOIN here.  Remember that a RIGHT JOIN returns all matching rows and all non-matching rows from the right.  Keep in mind that the header table is the &#8220;left&#8221; table and that the detail table is the &#8220;right&#8221; table.  How is it then that the query above returned only one row?</p>
<p>The answer lies in the WHERE clause that was specified.  It filters out the data after the join is made and tells SQL to return only the rows from the detail table where there is no matching header row.  This is an example of an <em>orphaned</em> record.</p>
<p>To detect header rows that have no detail (these aren&#8217;t considered orphaned rows because the term applies to child tables) we can run the query below:</p>
<p><u>Code</u></p>
<p><code></p>
<pre>
select
   hdr.*
from dbo.invHdr hdr (nolock)
left join dbo.invDet det (nolock)
on det.invnum = hdr.invnum
where
   det.invnum is null
</pre>
<p></code></p>
<p>Now let&#8217;s assume we want to find any rows in the invoice header or invoice detail table that is missing it&#8217;s associated parent or child row.  We could write the query like this:</p>
<p><u>Code</u></p>
<p><code></p>
<pre>
select
   hdr.*, det.*
from dbo.invHdr hdr (nolock)
full outer join dbo.invDet det (nolock)
on det.invnum = hdr.invnum
where
   hdr.invnum is null
   or det.invnum is null
</pre>
<p></code></p>
<p>Here we have instructed SQL (via the WHERE clause) to include any row where the invoice number is not in the header table or the detail table.  This in affect returns all rows where either a child is missing it&#8217;s parent or where the parent has no detail.</p>
<p>I hope you find this post and it&#8217;s companion <a href="http://sqlbyminh.wordpress.com/2009/04/23/sql-joins/" target="_blank">post</a> useful.  Please feel free to make any comments or suggestions regarding this post.</p>
<p>Minh</p>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[SQL Joins]]></title>
<link>http://sqlbyminh.wordpress.com/2009/04/23/sql-joins/</link>
<pubDate>Thu, 23 Apr 2009 02:48:44 +0000</pubDate>
<dc:creator>sqlbyminh</dc:creator>
<guid>http://sqlbyminh.wordpress.com/2009/04/23/sql-joins/</guid>
<description><![CDATA[What is a JOIN?  Simply put, it is an operation that combines records from two or more tables.  Ther]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p>What is a <em>JOIN</em>?  Simply put, it is an operation that combines records from two or more tables.  There are three types of joins in SQL.  They are as follows:</p>
<ul>
<li>INNER JOIN (most common type)</li>
<li>OUTER JOIN</li>
<li>CROSS JOIN</li>
</ul>
<p>Let&#8217;s look at an INNER JOIN.  What does it do?  An inner join is an operation that combines two or more tables into one resultset based on matches found in one or more columns in each of the participating tables.  For example, let&#8217;s say you have an INVOICE_HEADER table and an INVOICE_DETAIL table.  Assume that the two tables are defined as follows:</p>
<p><u>Create Tables</u><br />
<code></p>
<pre>create table dbo.invHdr
(
  invnum varchar(5) not null
  ,customer varchar(30) not null
  ,invdate datetime not null default (getdate())
)
go

create table dbo.invDet
(
  invnum varchar(5) not null
  ,trandate datetime not null default (getdate())
  ,itemnum varchar(10) not null
  ,itemdesc varchar(100) not null
  ,qty numeric(10, 2) not null default (0)
  ,unitprice numeric(10, 2) not null
)
go</pre>
<p> <br />
</code></p>
<p>Now let&#8217;s create some test data for our examples:</p>
<p><u>Populate Tables</u><br />
<code></p>
<pre>----- Insert header records. -----
insert into dbo.invHdr
(
  invnum, customer
)
values
(
  '00001', 'CUSTA'
)

insert into dbo.invHdr
(
  invnum, customer
)
values
(
  '00002', 'CUSTA'
)

insert into dbo.invHdr
(
  invnum, customer
)
values
(
  '00003', 'CUSTB'
)

----- Insert detail records. -----
insert into dbo.invDet
(
  invnum
  ,trandate
  ,itemnum
  ,itemdesc
  ,qty
  ,unitprice
)
values
(
  '00001'
  ,'4/1/2008'
  ,'I-BEV1'
  ,'COKE, 6PACK'
  ,3
  ,5
)

insert into dbo.invDet
(
  invnum
  ,trandate
  ,itemnum
  ,itemdesc
  ,qty
  ,unitprice
)
values
(
  '00005'
  ,'4/10/2008'
  ,'I-BEV2'
  ,'COKE, 12PACK'
  ,1
  ,9
)</pre>
<p></code></p>
<p><u>INNER JOIN</u></p>
<p>Now that we&#8217;ve got some test data, let&#8217;s see how to do each type of join and look at the data it returns. First up is the INNER JOIN. Run this select statement:</p>
<p><code></p>
<pre>----- Inner join. -----
select
  a.*, b.*
from dbo.invHdr a (nolock)
join dbo.invDet b (nolock)
on b.invnum = a.invnum</pre>
<p></code></p>
<div id="attachment_142" class="wp-caption alignleft" style="width: 460px"><img class="size-full wp-image-142" title="innerjoin1" src="http://sqlbyminh.wordpress.com/files/2009/04/innerjoin11.gif" alt="INNER JOIN example" width="450" height="50" /><p class="wp-caption-text">INNER JOIN example</p></div>
<p>Notice that the SELECT statement has returned a single row where a matching invoice number (invnum) was found in both the header and detail table.</p>
<p><u>LEFT JOIN</u></p>
<p>Now let&#8217;s look at what a LEFT JOIN would return. Run this statement:</p>
<p><code></p>
<pre>-- Left join.
select
  a.*, b.*
from dbo.invHdr a (nolock)
left join dbo.invDet b (nolock)
on b.invnum = a.invnum</pre>
<p></code></p>
<div id="attachment_143" class="wp-caption alignleft" style="width: 460px"><img class="size-full wp-image-142" title="leftjoin1" src="http://sqlbyminh.wordpress.com/files/2009/04/leftjoin1.gif" alt="LEFT JOIN example" width="450" height="50" /><p class="wp-caption-text">LEFT JOIN example</p></div>
<p>Notice how the LEFT JOIN statement returns all matching rows from both tables <em>and</em> all rows from the &#8220;left&#8221; table <em>regardless</em> of whether there are rows in the &#8220;right&#8221; table. The image below depicts this graphically.</p>
<div id="attachment_144" class="wp-caption alignleft" style="width: 210px"><img class="size-full wp-image-142" title="leftjoin2" src="http://sqlbyminh.wordpress.com/files/2009/04/leftjoin2.gif" alt="LEFT JOIN depiction" width="200" height="50" /><p class="wp-caption-text">LEFT JOIN depiction</p></div>
<p>The area in the middle are the matching rows. The area on the left (blue) are all the rows from the &#8220;left&#8221; table where no matches were found in the &#8220;right&#8221; table. However, since the type of JOIN is a LEFT JOIN, the rows from the left table are returned as well. The area on the right (white) are the rows from the &#8220;right&#8221; table. Only the <em>colored</em> areas are returned as part of the result.</p>
<p>You might be asking &#8220;How do I know which is the <em>left</em> table versus the <em>right</em> table?&#8221; The answer is simple. When you do any type of join, each table that is added to the join is considered to be the <em>right</em> table in relation to the table before it. For example, suppose we were to JOIN tables X, Y, and Z like so:</p>
<p>X &#8211;&#62; Y &#8211;&#62; Z</p>
<p>Here, X is considered to be the &#8220;left&#8221; table in relation to Y. Likewise, the table Y is considered to be the &#8220;left&#8221; table in relation to Z. It follows therefore that X is considered to be the &#8220;left&#8221; table in relation to Z. Some DBAs are under the misconception that the <em>equal</em> sign determines which table is &#8220;left&#8221; or &#8220;right&#8221;. Not so. For example, the two SELECT statements below would return identical results:</p>
<p><strong>SELECT statement 1</strong></p>
<p><code></p>
<pre>select a*, b.*
from dbo.invHdr a (nolock)
left join dbo.invDet b (nolock)
on a.invnum = b.invnum</pre>
<p></code></p>
<p><strong>SELECT statement 2</strong></p>
<p><code></p>
<pre>select a.*, b.*
from dbo.invHdr a (nolock)
left join dbo.invDet b (nolock)
on b.invnum = a.invnum</pre>
<p></code></p>
<p>As you can see, the same results are returned. The <strong><em>ON</em></strong> statement simply states the <em>relationship</em> between the two tables. That is, that they are related via the <em>invnum</em> column.</p>
<p><u>RIGHT JOIN</u></p>
<p>This type of join is simply the inverse of the LEFT JOIN. A <em>right join</em> returns all rows where matches were found in both tables as well as all rows from the right table. Here is a graphic that shows this concept:</p>
<div id="attachment_145" class="wp-caption aligncenter" style="width: 210px"><img class="size-full wp-image-142" title="rightjoin1" src="http://sqlbyminh.wordpress.com/files/2009/04/rightjoin1.gif" alt="RIGHT JOIN depiction" width="200" height="50" /><p class="wp-caption-text">RIGHT JOIN depiction</p></div>
<p>If we were to re-write the SELECT statement used in the LEFT JOIN example to be a RIGHT JOIN, it would look like this:</p>
<p><code></p>
<pre>
select a.*, b.*
from dbo.invHdr a (nolock)
<strong><u>right</u></strong> join dbo.invDet b (nolock)
on b.invnum = a.invnum
</pre>
<p></code></p>
<p>Notice that the only change that was made was to change the word LEFT to RIGHT.  If you were to run this statement, you would see a result set similar to the LEFT JOIN example graphic.  The only difference would be that for some rows, the columns for the LEFT table would be NULL.</p>
<p>Keep in mind that is because we are asking for all rows that match between the two tables and ALL rows from the right table <em>regardless</em> if a match was found on the left table.</p>
<p><u>FULL OUTER JOIN</u></p>
<p>Now let&#8217;s look at a full outer join.  This type of join returns all matching rows and all non-matching rows.  Below is a depiction of this concept.  As you can see, all the colored sections indicate the rows that would be returned.</p>
<div id="attachment_146" class="wp-caption aligncenter" style="width: 210px"><img class="size-full wp-image-142" title="FULL OUTER JOIN depiction" src="http://sqlbyminh.wordpress.com/files/2009/04/fullouterjoindepiction.gif" alt="FULL OUTER JOIN depiction" width="200" height="50" /><p class="wp-caption-text">FULL OUTER JOIN depiction</p></div>
<p>Run the following statement below to see the results:</p>
<p><u>Code</u></p>
<p><code></p>
<pre>
select
   a.*, b.*
from dbo.invHdr a (nolock)
full outer join dbo.invDet b (nolock)
on b.invnum = a.invnum
</pre>
<p></code></p>
<p>As you can see, the rows that are returned include both the matching rows and the <em>non-matching</em> rows.</p>
<div id="attachment_146" class="wp-caption aligncenter" style="width: 460px"><img class="size-full wp-image-147" title="FULL OUTER JOIN example" src="http://sqlbyminh.wordpress.com/files/2009/04/fullouterjoin1.gif" alt="FULL OUTER JOIN example" width="450" height="50" /><p class="wp-caption-text">FULL OUTER JOIN example</p></div>
<p><u>CROSS JOIN</u></p>
<p>A CROSS JOIN is one in which <em>all possible combinations</em> of the two tables are returned.  Run the code below to see the output:</p>
<p><code></p>
<pre>
select
   a.*, b.*
from dbo.invHdr a (nolock)
cross join dbo.invDet b (nolock)
</pre>
<p></code></p>
<p>As you can see from the graphic below, all the combinations of the records from both tables are returned.</p>
<div id="attachment_146" class="wp-caption aligncenter" style="width: 460px"><img class="size-full wp-image-148" title="CROSS JOIN example" src="http://sqlbyminh.wordpress.com/files/2009/04/crossjoin1.gif" alt="CROSS JOIN example" width="450" height="50" /><p class="wp-caption-text">CROSS JOIN example</p></div>
<p>In my next post I will show how you can use the LEFT JOIN, RIGHT JOIN or FULL OUTER JOIN to help you fined <em>orphaned records</em> in your tables.  Orphaned records are a violation of <em>database normalization</em> rules and should be addressed if and when they are found.</p>
<p>Check back in a couple of days.  Any feedback and/or comments are welcomed.<br />
Minh</p>
<p>My post on detecting orphaned records is available <a href="http://sqlbyminh.wordpress.com/2009/04/24/detecting-orphaned-rows/" target="_blank">here</a></p>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[Complete ref of SQL-Server Join, Inner Join, Left Outer Join, Right Outer Join, Full Outer Join, in SQL-Server 2005:]]></title>
<link>http://riteshshah.wordpress.com/2009/03/08/complete-ref-of-sql-server-join-inner-join-left-outer-join-right-outer-join-full-outer-join-in-sql-server-2005/</link>
<pubDate>Sun, 08 Mar 2009 04:22:29 +0000</pubDate>
<dc:creator>riteshshah</dc:creator>
<guid>http://riteshshah.wordpress.com/2009/03/08/complete-ref-of-sql-server-join-inner-join-left-outer-join-right-outer-join-full-outer-join-in-sql-server-2005/</guid>
<description><![CDATA[Complete ref of SQL-Server Join, Inner Join, Left Outer Join, Right Outer Join, Full Outer Join, in ]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p class="MsoNormal" style="margin:0 0 10pt;"><strong><span style="font-size:12pt;line-height:115%;font-family:&#34;">Complete ref of SQL-Server Join, Inner Join, Left Outer Join, Right Outer Join, Full Outer Join, in SQL-Server 2005:</span></strong></p>
<p class="MsoNormal" style="margin:0 0 10pt;"><em><span style="font-size:12pt;line-height:115%;font-family:&#34;">Merging data is heart of SQL and it shows the depth of relational algebra as well as the power and flexibility of SQL. SQL is excellent at selecting the data and SQL Developer can be master by understanding the relational algebra.</span></em></p>
<p class="MsoNormal" style="margin:0 0 10pt;"><span style="font-size:12pt;line-height:115%;font-family:&#34;">Join seems known technology but it is fun all time. Developer can make very creative query with joins by making data twist and shout. Pulling the data out of join could be the answer of very big code of programming language with iteration. It is much faster than any loop in your code. I promise you will have fun by learning this powerful technique.</span></p>
<p class="MsoNormal" style="margin:0 0 10pt;"><span style="font-size:12pt;line-height:115%;font-family:&#34;">SQL-Server 2005 is relational database so you always need to join more than one table in order to get complete information out of data tables. Since, tables are self explanatory itself, order of table doesn’t matter.</span></p>
<p class="MsoNormal" style="margin:0 0 10pt;"><span style="font-size:12pt;line-height:115%;font-family:&#34;">Here are the types of available joins in SQL-Server 2005:</span></p>
<p class="MsoNormal" style="margin:0 0 10pt;"><strong><span style="font-size:12pt;line-height:115%;font-family:&#34;">Note: AdventureWorks database is going to be used throughout in this chapter.</span></strong></p>
<p class="MsoNormal" style="margin:0 0 10pt;"><strong><span style="font-size:12pt;line-height:115%;font-family:&#34;">Inner Join:</span></strong><span style="font-size:12pt;line-height:115%;font-family:&#34;"> It’s a very common and frequently needed join. Common intersection will be pulled out in this kind of join. It means, it will compare the result sets of first table with result sets of second table based on common fields or based on primary key and foreign key relationship. Below given is an example of Inner Join.</span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:10pt;color:blue;font-family:&#34;">SELECT</span><span style="font-size:10pt;font-family:&#34;"> Sales<span style="color:gray;">.</span>SalesPerson<span style="color:gray;">.</span>SalesPersonID<span style="color:gray;">,</span></span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:10pt;font-family:&#34;">Sales<span style="color:gray;">.</span>SalesPersonQuotaHistory<span style="color:gray;">.</span>SalesQuota </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:10pt;color:blue;font-family:&#34;">FROM</span><span style="font-size:10pt;font-family:&#34;"> Sales<span style="color:gray;">.</span>SalesPerson </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:10pt;color:gray;font-family:&#34;">INNER</span><span style="font-size:10pt;font-family:&#34;"> <span style="color:gray;">JOIN</span></span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:10pt;font-family:&#34;">Sales<span style="color:gray;">.</span>SalesPersonQuotaHistory</span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:10pt;color:blue;font-family:&#34;">ON</span><span style="font-size:10pt;font-family:&#34;"> </span></p>
<p class="MsoNormal" style="margin:0 0 10pt;"><span style="font-size:10pt;line-height:115%;font-family:&#34;">Sales<span style="color:gray;">.</span>SalesPerson<span style="color:gray;">.</span>SalesPersonID<span style="color:gray;">=</span>Sales<span style="color:gray;">.</span>SalesPersonQuotaHistory<span style="color:gray;">.</span>SalesPersonID</span><span style="font-size:12pt;line-height:115%;font-family:&#34;"></span></p>
<p class="MsoNormal" style="margin:0 0 10pt;"><span style="font-size:12pt;line-height:115%;font-family:&#34;">You can get join two table with SalesPersonID in above case as both have common field. </span></p>
<p class="MsoNormal" style="margin:0 0 10pt;"><strong><span style="font-size:12pt;line-height:115%;font-family:&#34;">(Part of Outer Join) Left Outer Join:</span></strong><span style="font-size:12pt;line-height:115%;font-family:&#34;"> Include all rows from the left side table no matter whether matching rows are exists in right side table or not. It means that Outer join is an extended version of Inner Join as it not only gives your common intersection but it do gives non matching data from left side or right side along with intersection.</span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:10pt;color:blue;font-family:&#34;">SELECT</span><span style="font-size:10pt;font-family:&#34;"> HumanResources<span style="color:gray;">.</span>Employee<span style="color:gray;">.</span>EmployeeID<span style="color:gray;">,</span>HumanResources<span style="color:gray;">.</span>Employee<span style="color:gray;">.</span>NationalIDNumber<span style="color:gray;">,</span></span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:10pt;font-family:&#34;">HumanResources<span style="color:gray;">.</span>EmployeeAddress<span style="color:gray;">.</span>AddressID</span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:10pt;color:blue;font-family:&#34;">FROM</span><span style="font-size:10pt;font-family:&#34;"> HumanResources<span style="color:gray;">.</span>Employee <span style="color:gray;">Left</span> <span style="color:gray;">Outer</span> <span style="color:gray;">Join</span> HumanResources<span style="color:gray;">.</span>EmployeeAddress</span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:10pt;color:blue;font-family:&#34;">ON</span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:10pt;font-family:&#34;">HumanResources<span style="color:gray;">.</span>Employee<span style="color:gray;">.</span>EmployeeID<span style="color:gray;">=</span>HumanResources<span style="color:gray;">.</span>EmployeeAddress<span style="color:gray;">.</span>EmployeeID</span></p>
<p class="MsoNormal" style="margin:0 0 10pt;"><span style="font-size:12pt;line-height:115%;font-family:&#34;">Employee table is joined with EmployeeAddress table based on EmployeeID. Left out join has been used so it will list all employees, no matter where it meets associated entry in EmployeeAddress.</span></p>
<p class="MsoNormal" style="margin:0 0 10pt;"><strong><span style="font-size:12pt;line-height:115%;font-family:&#34;">(Part of Outer Join) Right Outer</span></strong><span style="font-size:12pt;line-height:115%;font-family:&#34;"> <strong>Join:</strong> Include all rows from the right side table no matter whether matching rows are exists in left side table or not. It means that Outer join is an extended version of Inner Join as it not only gives your common intersection but it do gives non matching data from left side or right side along with intersection.</span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:10pt;color:blue;font-family:&#34;">SELECT</span><span style="font-size:10pt;font-family:&#34;"> HumanResources<span style="color:gray;">.</span>Employee<span style="color:gray;">.</span>EmployeeID<span style="color:gray;">,</span>HumanResources<span style="color:gray;">.</span>Employee<span style="color:gray;">.</span>NationalIDNumber<span style="color:gray;">,</span></span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:10pt;font-family:&#34;">HumanResources<span style="color:gray;">.</span>EmployeeAddress<span style="color:gray;">.</span>AddressID</span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:10pt;color:blue;font-family:&#34;">FROM</span><span style="font-size:10pt;font-family:&#34;"> HumanResources<span style="color:gray;">.</span>Employee <span style="color:gray;">Right</span> <span style="color:gray;">Outer</span> <span style="color:gray;">Join</span> HumanResources<span style="color:gray;">.</span>EmployeeAddress</span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:10pt;color:blue;font-family:&#34;">ON</span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:10pt;font-family:&#34;">HumanResources<span style="color:gray;">.</span>Employee<span style="color:gray;">.</span>EmployeeID<span style="color:gray;">=</span>HumanResources<span style="color:gray;">.</span>EmployeeAddress<span style="color:gray;">.</span>EmployeeID</span></p>
<p class="MsoNormal" style="margin:0 0 10pt;"><span style="font-size:12pt;line-height:115%;font-family:&#34;"> </span></p>
<p class="MsoNormal" style="margin:0 0 10pt;"><span style="font-size:12pt;line-height:115%;font-family:&#34;">Employee table is joined with EmployeeAddress table based on EmployeeID. Right outer join has been used so it will list all employees’ Address, no matter where it meets associated entry in EmployeeAddress.</span></p>
<p class="MsoNormal" style="margin:0 0 10pt;"><strong><span style="font-size:12pt;line-height:115%;font-family:&#34;">(Part of Outer Join) Full Outer Join:</span></strong><span style="font-size:12pt;line-height:115%;font-family:&#34;"> Includes all rows from both tables regardless of match exists. It means it a union distinct of Left Outer Join and Right Outer Join.</span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:10pt;color:blue;font-family:&#34;">SELECT</span><span style="font-size:10pt;font-family:&#34;"> HumanResources<span style="color:gray;">.</span>Employee<span style="color:gray;">.</span>EmployeeID<span style="color:gray;">,</span>HumanResources<span style="color:gray;">.</span>Employee<span style="color:gray;">.</span>NationalIDNumber<span style="color:gray;">,</span></span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:10pt;font-family:&#34;">HumanResources<span style="color:gray;">.</span>EmployeeAddress<span style="color:gray;">.</span>AddressID</span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:10pt;color:blue;font-family:&#34;">FROM</span><span style="font-size:10pt;font-family:&#34;"> HumanResources<span style="color:gray;">.</span>Employee <span style="color:blue;">Full</span> <span style="color:gray;">Outer</span> <span style="color:gray;">Join</span> HumanResources<span style="color:gray;">.</span>EmployeeAddress</span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:10pt;color:blue;font-family:&#34;">ON</span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:10pt;font-family:&#34;">HumanResources<span style="color:gray;">.</span>Employee<span style="color:gray;">.</span>EmployeeID<span style="color:gray;">=</span>HumanResources<span style="color:gray;">.</span>EmployeeAddress<span style="color:gray;">.</span>EmployeeID</span></p>
<p class="MsoNormal" style="margin:0 0 10pt;"><span style="font-size:12pt;line-height:115%;font-family:&#34;"> </span></p>
<p class="MsoNormal" style="margin:0 0 10pt;"><strong><span style="font-size:12pt;line-height:115%;font-family:&#34;">Self Join: </span></strong><span style="font-size:12pt;line-height:115%;font-family:&#34;">A self-join refers back to itself. You can use this type of unary relationship for recursive purpose. Like employee table to find boss. </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:10pt;color:green;font-family:&#34;">&#8211;creating table for self join</span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:10pt;color:blue;font-family:&#34;">create</span><span style="font-size:10pt;font-family:&#34;"> <span style="color:blue;">table</span> emp</span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:10pt;color:gray;font-family:&#34;">(</span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:10pt;font-family:&#34;">ID <span style="color:blue;">int</span> <span style="color:blue;">Identity</span><span style="color:gray;">(</span>1<span style="color:gray;">,</span>1<span style="color:gray;">),</span></span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:10pt;color:blue;font-family:&#34;">Name</span><span style="font-size:10pt;font-family:&#34;"> <span style="color:blue;">Varchar</span><span style="color:gray;">(</span>15<span style="color:gray;">),</span></span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:10pt;font-family:&#34;">BossID <span style="color:blue;">int</span></span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:10pt;color:gray;font-family:&#34;">)</span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:10pt;font-family:&#34;">GO</span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:10pt;font-family:&#34;"> </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:10pt;color:green;font-family:&#34;">&#8211;Inserting some records in above table.</span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:10pt;color:blue;font-family:&#34;">Insert</span><span style="font-size:10pt;font-family:&#34;"> <span style="color:blue;">Into</span> emp</span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:10pt;color:blue;font-family:&#34;">SELECT</span><span style="font-size:10pt;font-family:&#34;"> <span style="color:red;">&#8216;DM&#8217;</span><span style="color:gray;">,</span>0 <span style="color:blue;">UNION</span> <span style="color:gray;">ALL</span></span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:10pt;color:blue;font-family:&#34;">SELECT</span><span style="font-size:10pt;font-family:&#34;"> <span style="color:red;">&#8216;MA&#8217;</span><span style="color:gray;">,</span>1 <span style="color:blue;">UNION</span> <span style="color:gray;">ALL</span></span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:10pt;color:blue;font-family:&#34;">SELECT</span><span style="font-size:10pt;font-family:&#34;"> <span style="color:red;">&#8216;UC&#8217;</span><span style="color:gray;">,</span>2 <span style="color:blue;">UNION</span> <span style="color:gray;">ALL</span></span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:10pt;color:blue;font-family:&#34;">SELECT</span><span style="font-size:10pt;font-family:&#34;"> <span style="color:red;">&#8216;LU&#8217;</span><span style="color:gray;">,</span>2</span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:10pt;font-family:&#34;">GO</span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:10pt;color:blue;font-family:&#34;"> </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:10pt;color:green;font-family:&#34;">&#8211;equi join which will find boss name for every employee, DM is a main boss in this case.</span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:10pt;color:blue;font-family:&#34;">SELECT</span><span style="font-size:10pt;font-family:&#34;"> e<span style="color:gray;">.</span>ID<span style="color:gray;">,</span>e<span style="color:gray;">.</span><span style="color:blue;">Name</span><span style="color:gray;">,</span>ep<span style="color:gray;">.</span><span style="color:blue;">Name</span> <span style="color:blue;">as</span> <span style="color:red;">&#8216;boss name&#8217;</span></span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:10pt;color:blue;font-family:&#34;">FROM</span><span style="font-size:10pt;font-family:&#34;"> emp e <span style="color:gray;">JOIN</span> emp ep</span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:10pt;color:blue;font-family:&#34;">on</span><span style="font-size:10pt;font-family:&#34;"> </span></p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-size:10pt;font-family:&#34;">e<span style="color:gray;">.</span>bossid<span style="color:gray;">=</span>ep<span style="color:gray;">.</span>id</span></p>
<p class="MsoNormal" style="margin:0 0 10pt;"><span style="font-size:10pt;line-height:115%;font-family:&#34;">GO</span><span style="font-size:12pt;line-height:115%;font-family:&#34;"></span></p>
<p class="MsoNormal" style="margin:0 0 10pt;"><span style="font-size:12pt;line-height:115%;font-family:&#34;">There are few more types of join like theta join, equi join, natural join etc. which I will explain in coming article.</span></p>
<p class="MsoNormal" style="margin:0 0 10pt;"><strong><span style="font-size:12pt;line-height:115%;font-family:&#34;">Reference: Ritesh Shah </span></strong></p>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[Right Join - The secret of swapping join input]]></title>
<link>http://dioncho.wordpress.com/2009/03/03/right-join-the-secret-of-swapping-join-input/</link>
<pubDate>Tue, 03 Mar 2009 07:40:02 +0000</pubDate>
<dc:creator>Dion Cho</dc:creator>
<guid>http://dioncho.wordpress.com/2009/03/03/right-join-the-secret-of-swapping-join-input/</guid>
<description><![CDATA[It&#8217;s not well known that Oracle really enjoys swapping the join input to get join performance ]]></description>
<content:encoded><![CDATA[It&#8217;s not well known that Oracle really enjoys swapping the join input to get join performance ]]></content:encoded>
</item>
<item>
<title><![CDATA[SQL RIGHT JOIN]]></title>
<link>http://rizwanparkar.wordpress.com/2009/02/19/sql-right-join/</link>
<pubDate>Thu, 19 Feb 2009 07:46:55 +0000</pubDate>
<dc:creator>rizwanparkar</dc:creator>
<guid>http://rizwanparkar.wordpress.com/2009/02/19/sql-right-join/</guid>
<description><![CDATA[SQL RIGHT JOIN Keyword The RIGHT JOIN keyword Return all rows from the right table (table_name2), ev]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><h2>SQL RIGHT JOIN Keyword</h2>
<p>The RIGHT JOIN keyword Return all rows from the right table (table_name2), even  	if there are no matches in the left table (table_name1).</p>
<h3>SQL RIGHT JOIN Syntax</h3>
<table id="table3" class="ex" border="1" cellspacing="0" width="100%">
<tbody>
<tr>
<td>
<pre>SELECT column_name(s)
FROM table_name1
RIGHT JOIN table_name2
ON table_name1.column_name=table_name2.column_name</pre>
</td>
</tr>
</tbody>
</table>
<p><strong>PS:</strong> In some databases RIGHT JOIN is called RIGHT OUTER JOIN.</p>
<hr />
<h2>SQL RIGHT JOIN Example</h2>
<p>The &#8220;Persons&#8221; table:</p>
<table id="table1" class="ex" border="1" cellspacing="0" width="100%">
<tbody>
<tr>
<th align="left">P_Id</th>
<th align="left">LastName</th>
<th align="left">FirstName</th>
<th align="left">Address</th>
<th align="left">City</th>
</tr>
<tr>
<td>1</td>
<td>Hansen</td>
<td>Ola</td>
<td>Timoteivn 10</td>
<td>Sandnes</td>
</tr>
<tr>
<td>2</td>
<td>Svendson</td>
<td>Tove</td>
<td>Borgvn 23</td>
<td>Sandnes</td>
</tr>
<tr>
<td>3</td>
<td>Pettersen</td>
<td>Kari</td>
<td>Storgt 20</td>
<td>Stavanger</td>
</tr>
</tbody>
</table>
<p>The &#8220;Orders&#8221; table:</p>
<table id="table2" class="ex" border="1" cellspacing="0" width="50%">
<tbody>
<tr>
<th width="15%" align="left">O_Id</th>
<th width="20%" align="left">OrderNo</th>
<th width="15%" align="left">P_Id</th>
</tr>
<tr>
<td>1</td>
<td>77895</td>
<td>3</td>
</tr>
<tr>
<td>2</td>
<td>44678</td>
<td>3</td>
</tr>
<tr>
<td>3</td>
<td>22456</td>
<td>1</td>
</tr>
<tr>
<td>4</td>
<td>24562</td>
<td>1</td>
</tr>
<tr>
<td>5</td>
<td>34764</td>
<td>15</td>
</tr>
</tbody>
</table>
<p>Now we want to list all the orders with containing persons &#8211; if any, from the tables above.</p>
<p>We use the following SELECT statement:</p>
<table class="ex" border="1" cellspacing="0" width="100%">
<tbody>
<tr>
<td>
<pre>SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo
FROM Persons
RIGHT JOIN Orders
ON Persons.P_Id=Orders.P_Id
ORDER BY Persons.LastName</pre>
</td>
</tr>
</tbody>
</table>
<p>The result-set will look like this:</p>
<table id="table4" class="ex" border="1" cellspacing="0" width="100%">
<tbody>
<tr>
<th align="left">LastName</th>
<th align="left">FirstName</th>
<th align="left">OrderNo</th>
</tr>
<tr>
<td>Hansen</td>
<td>Ola</td>
<td>22456</td>
</tr>
<tr>
<td>Hansen</td>
<td>Ola</td>
<td>24562</td>
</tr>
<tr>
<td>Pettersen</td>
<td>Kari</td>
<td>77895</td>
</tr>
<tr>
<td>Pettersen</td>
<td>Kari</td>
<td>44678</td>
</tr>
<tr>
<td></td>
<td></td>
<td>34764</td>
</tr>
</tbody>
</table>
<p>The RIGHT JOIN keyword returns all the rows from the right table (Orders), even if there are no matches in the left table (Persons).</p>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[SQL LEFT JOIN]]></title>
<link>http://rizwanparkar.wordpress.com/2009/02/19/sql-left-join/</link>
<pubDate>Thu, 19 Feb 2009 07:44:02 +0000</pubDate>
<dc:creator>rizwanparkar</dc:creator>
<guid>http://rizwanparkar.wordpress.com/2009/02/19/sql-left-join/</guid>
<description><![CDATA[SQL LEFT JOIN Keyword The LEFT JOIN keyword returns all rows from the left table (table_name1), even]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><h2>SQL LEFT JOIN Keyword</h2>
<p>The LEFT JOIN keyword returns all rows from the left table (table_name1), even if  	there are no matches in the right table (table_name2).</p>
<h3>SQL LEFT JOIN Syntax</h3>
<table id="table3" class="ex" border="1" cellspacing="0" width="100%">
<tbody>
<tr>
<td>
<pre>SELECT column_name(s)
FROM table_name1
LEFT JOIN table_name2
ON table_name1.column_name=table_name2.column_name</pre>
</td>
</tr>
</tbody>
</table>
<p><strong>PS:</strong> In some databases LEFT JOIN is called LEFT OUTER JOIN.</p>
<hr />
<h2>SQL LEFT JOIN Example</h2>
<p>The &#8220;Persons&#8221; table:</p>
<table id="table1" class="ex" border="1" cellspacing="0" width="100%">
<tbody>
<tr>
<th align="left">P_Id</th>
<th align="left">LastName</th>
<th align="left">FirstName</th>
<th align="left">Address</th>
<th align="left">City</th>
</tr>
<tr>
<td>1</td>
<td>Hansen</td>
<td>Ola</td>
<td>Timoteivn 10</td>
<td>Sandnes</td>
</tr>
<tr>
<td>2</td>
<td>Svendson</td>
<td>Tove</td>
<td>Borgvn 23</td>
<td>Sandnes</td>
</tr>
<tr>
<td>3</td>
<td>Pettersen</td>
<td>Kari</td>
<td>Storgt 20</td>
<td>Stavanger</td>
</tr>
</tbody>
</table>
<p>The &#8220;Orders&#8221; table:</p>
<table id="table2" class="ex" border="1" cellspacing="0" width="50%">
<tbody>
<tr>
<th width="15%" align="left">O_Id</th>
<th width="20%" align="left">OrderNo</th>
<th width="15%" align="left">P_Id</th>
</tr>
<tr>
<td>1</td>
<td>77895</td>
<td>3</td>
</tr>
<tr>
<td>2</td>
<td>44678</td>
<td>3</td>
</tr>
<tr>
<td>3</td>
<td>22456</td>
<td>1</td>
</tr>
<tr>
<td>4</td>
<td>24562</td>
<td>1</td>
</tr>
<tr>
<td>5</td>
<td>34764</td>
<td>15</td>
</tr>
</tbody>
</table>
<p>Now we want to list all the persons and their orders &#8211; if any, from the tables above.</p>
<p>We use the following SELECT statement:</p>
<table class="ex" border="1" cellspacing="0" width="100%">
<tbody>
<tr>
<td>
<pre>SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo
FROM Persons
LEFT JOIN Orders
ON Persons.P_Id=Orders.P_Id
ORDER BY Persons.LastName</pre>
</td>
</tr>
</tbody>
</table>
<p>The result-set will look like this:</p>
<table id="table4" class="ex" border="1" cellspacing="0" width="100%">
<tbody>
<tr>
<th align="left">LastName</th>
<th align="left">FirstName</th>
<th align="left">OrderNo</th>
</tr>
<tr>
<td>Hansen</td>
<td>Ola</td>
<td>22456</td>
</tr>
<tr>
<td>Hansen</td>
<td>Ola</td>
<td>24562</td>
</tr>
<tr>
<td>Pettersen</td>
<td>Kari</td>
<td>77895</td>
</tr>
<tr>
<td>Pettersen</td>
<td>Kari</td>
<td>44678</td>
</tr>
<tr>
<td>Svendson</td>
<td>Tove</td>
<td></td>
</tr>
</tbody>
</table>
<p>The LEFT JOIN keyword returns all the rows from the left table (Persons), even if there are no matches in the right table (Orders).</p>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[SQL INNER JOIN]]></title>
<link>http://rizwanparkar.wordpress.com/2009/02/19/sql-inner-join/</link>
<pubDate>Thu, 19 Feb 2009 07:42:33 +0000</pubDate>
<dc:creator>rizwanparkar</dc:creator>
<guid>http://rizwanparkar.wordpress.com/2009/02/19/sql-inner-join/</guid>
<description><![CDATA[SQL INNER JOIN Keyword The INNER JOIN keyword return rows when there is at least one match in both t]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p><span style="color:#993366;">SQL INNER JOIN Keyword</span></p>
<p>The INNER JOIN keyword return rows when there is at least one match in both tables.<br />
<span style="color:#993366;">SQL INNER JOIN Syntax</span></p>
<p>SELECT column_name(s)<br />
FROM table_name1<br />
INNER JOIN table_name2<br />
ON table_name1.column_name=table_name2.column_name</p>
<p>PS: INNER JOIN is the same as JOIN.<br />
<span style="color:#993366;">SQL INNER JOIN Example</span></p>
<p>The &#8220;Persons&#8221; table:<br />
P_Id    	LastName    	FirstName     	Address             	      City<br />
1            	Hansen         	Ola                 	       Timoteivn 10    	Sandnes<br />
2            	Svendson      	Tove             	       Borgvn 23           	        Sandnes<br />
3            	Pettersen      	Kari               	       Storgt 20             	        Stavanger</p>
<p><span style="color:#800080;">Note</span><span style="color:#800080;"> that</span> the &#8220;P_Id&#8221; column is the primary key in the &#8220;Persons&#8221; table. This means that no two rows can have the same P_Id. The P_Id distinguishes two persons even if they have the same name.</p>
<p>The &#8220;Orders&#8221; table:<br />
O_Id     	OrderNo      	P_Id<br />
1            	77895          	3<br />
2             44678          	3<br />
3             22456          	1<br />
4             24562           	1<br />
5            	34764         	15<br />
Now we want to list all the persons with any orders.</p>
<p>We use the following SELECT statement:</p>
<p>SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo<br />
FROM Persons<br />
INNER JOIN Orders<br />
ON Persons.P_Id=Orders.P_Id<br />
ORDER BY Persons.LastName</p>
<p>The result-set will look like this:<br />
LastName     FirstName     OrderNo<br />
Hansen           Ola                    22456<br />
Hansen           Ola                    24562<br />
Pettersen       Kari                  77895<br />
Pettersen       Kari                  44678</p>
<p>The INNER JOIN keyword return rows when there is at least one match in both tables. If there are rows in &#8220;Persons&#8221; that do not have matches in &#8220;Orders&#8221;, those rows will NOT be listed.</p>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[SQL JOIN]]></title>
<link>http://rizwanparkar.wordpress.com/2009/02/19/sql-join/</link>
<pubDate>Thu, 19 Feb 2009 07:37:55 +0000</pubDate>
<dc:creator>rizwanparkar</dc:creator>
<guid>http://rizwanparkar.wordpress.com/2009/02/19/sql-join/</guid>
<description><![CDATA[SQL joins are used to query data from two or more tables, based on a relationship between certain co]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p>SQL joins are used to query data from two or more tables, based on a relationship between certain columns in these tables.<br />
<span style="color:#800080;">SQL JOIN</span></p>
<p>The JOIN keyword is used in an SQL statement to query data from two or more tables, based on a relationship between certain columns in these tables.</p>
<p>Tables in a database are often related to each other with keys.</p>
<p>A primary key is a column (or a combination of columns) with a unique value for each row. Each primary key value must be unique within the table. The purpose is to bind data together, across tables, without repeating all of the data in every table.</p>
<p>Look at the &#8220;Persons&#8221; table:<br />
P_Id    	LastName    	FirstName     	Address             	      City<br />
1            	Hansen         	Ola                 	       Timoteivn 10    	Sandnes<br />
2            	Svendson      	Tove             	       Borgvn 23           	        Sandnes<br />
3            	Pettersen      	Kari               	       Storgt 20             	        Stavanger</p>
<p><span style="color:#800080;">Note</span><span style="color:#800080;"> that</span> the &#8220;P_Id&#8221; column is the primary key in the &#8220;Persons&#8221; table. This means that no two rows can have the same P_Id. The P_Id distinguishes two persons even if they have the same name.</p>
<p>Next, we have the &#8220;Orders&#8221; table:<br />
O_Id     	OrderNo      	P_Id<br />
1            	77895          	3<br />
2             44678          	3<br />
3             22456          	1<br />
4             24562           	1<br />
5            	34764         	15</p>
<p>Note that the &#8220;O_Id&#8221; column is the primary key in the &#8220;Orders&#8221; table and that the &#8220;P_Id&#8221; column refers to the persons in the &#8220;Persons&#8221; table without using their names.</p>
<p>Notice that the relationship between the two tables above is the &#8220;P_Id&#8221; column.<br />
Different SQL JOINs</p>
<p>Before we continue with examples, we will list the types of JOIN you can use, and the differences between them.</p>
<p>* JOIN: Return rows when there is at least one match in both tables<br />
* LEFT JOIN: Return all rows from the left table, even if there are no matches in the right table<br />
* RIGHT JOIN: Return all rows from the right table, even if there are no matches in the left table<br />
* FULL JOIN: Return rows when there is a match in one of the tables</p>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[SQL Joins Simple Notes]]></title>
<link>http://myleskadusale.wordpress.com/2009/02/17/sql-joins-simple-notes/</link>
<pubDate>Tue, 17 Feb 2009 09:49:06 +0000</pubDate>
<dc:creator>myleskadusale</dc:creator>
<guid>http://myleskadusale.wordpress.com/2009/02/17/sql-joins-simple-notes/</guid>
<description><![CDATA[INNER JOIN Inner join only returns a result that will contain matching records from both tables. OUT]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p><strong>INNER JOIN</strong><br />
Inner join only returns a result that will contain matching records from both tables.</p>
<p><strong>OUTER JOIN</strong><br />
An outer join is the inverse of the inner join. It only returns those records not in both tables.</p>
<p><strong>LEFT JOIN</strong><br />
A left join returns all the records in the “left” table whether they have a match in the right table or not.</p>
<p>If, however, they do have a match in the right table &#8211; give me the “matching” data from the right table as well. If not &#8211; fill in the holes with null.</p>
<p><strong>RIGHT JOIN</strong><br />
A right join returns all the records in the “right” table whether they have a match in the left table or not.</p>
<p>If, however, they do have a match in the left table &#8211; give me the “matching” data from the left table as well. If not &#8211; fill in the holes with null.</p>
<p><strong>FULL JOIN</strong><br />
Result will contain all records from both tables, and fill in NULLs for missing matches on either side</p>
<p><strong>LEFT OUTER JOIN</strong><br />
A left outer join combines the ideas behind a left join and an outer join. Basically &#8211; if you use a left outer join you will get the records in the left table that DO NOT have a match in the right table.</p>
<p><strong>RIGHT OUTER JOIN</strong><br />
A right outer join combines the ideas behind a right join and an outer join. Basically &#8211; if you use a right outer join you will get the records in the right table that DO NOT have a match in the left table.</p>
<p><strong>I got this info from the net</strong></p>
<p><a href="http://www.codinghorror.com/blog/archives/000976.html"> Visual Explanation of Joins</a><br />
<a href="http://www.khankennels.com/blog/index.php/archives/2007/04/20/getting-joins/">&#8216;Getting&#8217; JOINS </a><br />
<a href="http://www.keithjbrown.co.uk/vworks/mysql/mysql_p5.php">Using MySQL JOINS</a><br />
<a href="http://www.quackit.com/sql/tutorial/sql_join.cfm">SQL JOIN</a><br />
<a href="http://www.w3schools.com/sql/sql_join.asp">W3C Schools.com</a></p>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[Konsep Himpunan Dalam SQL]]></title>
<link>http://jnet99.wordpress.com/2008/10/12/konsep-himpunan-dalam-sql/</link>
<pubDate>Sun, 12 Oct 2008 05:32:17 +0000</pubDate>
<dc:creator>josenetmail</dc:creator>
<guid>http://jnet99.wordpress.com/2008/10/12/konsep-himpunan-dalam-sql/</guid>
<description><![CDATA[Kekuatan dari perintah SELECT dalam bahasa SQL terletak pada kemudahan metodenya dalam membentuk sua]]></description>
<content:encoded><![CDATA[Kekuatan dari perintah SELECT dalam bahasa SQL terletak pada kemudahan metodenya dalam membentuk sua]]></content:encoded>
</item>
<item>
<title><![CDATA[Cláusula JOIN (INNER,LEFT,RIGHT)]]></title>
<link>http://bokerao.wordpress.com/2008/08/07/clausula-join-innerleftright/</link>
<pubDate>Thu, 07 Aug 2008 12:48:54 +0000</pubDate>
<dc:creator>bokerao</dc:creator>
<guid>http://bokerao.wordpress.com/2008/08/07/clausula-join-innerleftright/</guid>
<description><![CDATA[Nas minhas pesquisas sobre o JOIN achei uma explicação muito boa e didática sobre a cláusula. Quem e]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p>Nas minhas pesquisas sobre o JOIN achei uma explicação muito boa e didática sobre a cláusula. Quem escreveu isso num fórum foi o Herleson Pontes (<span><span><a title="http://www.officeceara.com.br/" href="http://www.officeceara.com.br/"><span style="font-size:xx-small;font-family:Verdana;color:#0000ff;"><span style="text-decoration:underline;">http://www.officeceara.com.br</span></span></a><span style="font-size:xx-small;font-family:Verdana;">):</span></span></span></p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p><span><span>Vamos as explicações: suponha que você tenha duas tabelas: <em>Clientes </em>e <em>Vendas</em>, relacionadas entre si. Na tabela de <em>Clientes</em>, você tem o cadastro de todos os clientes da loja. Na tabela de <em>Vendas</em>, você tem as vendas que foram realizadas pelos clientes da loja. suponha ainda que não é obrigado o cliente ser cadastrado para efetuar uma venda.</span></span></p>
<p>Neste cenário, podemos ter três situações:</p>
<p>1) Existem clientes que ainda não fizeram nenhuma compra na loja;<br />
2) Existem vendas que foram efetuadas por clientes não cadastrados no sistema;<br />
3) Existem vendas que foram realizadas por clientes cadastrados.</p>
<p>Entendido o contexto acima, fica fácil de entender os JOINS. Basicamente, a sintaxe seria a abaixo:</p>
<p><span style="background-color:#c0c0c0;"><strong>SELECT <em>campos </em>FROM <em>Clientes c <span style="text-decoration:underline;">TIPO_DE_JOIN</span></em> JOIN <em>Venda v</em> ON <em>c.cod_cliente </em>=<em> v.cod_cliente</em>;</strong></span></p>
<p>Veja agora como aplicar cada tipo de Join:</p>
<p><strong>INNER JOIN: </strong>O Inner Join é utilizado nas situações em que você quer selecionar os registro das duas tabelas, desde que as mesmas possuam informações cruzadas (relacionadas). No nosso exemplo, é o JOIN que resolve a situação 3: a consulta vai retornar os clientes cadastrados que efetuaram compras.</p>
<p><strong><span style="background-color:#c0c0c0;">SELECT <em>campos </em>FROM <em>Clientes c <span style="text-decoration:underline;">INNER</span> </em>JOIN <em>Venda v</em> ON <em>c.cod_cliente </em>=<em> v.cod_cliente</em>;</span></strong></p>
<p><strong>OUTER JOIN:</strong> O Outer join possui o funcionamento um pouco diferente do anterior. Ao usar o Outer join, além de podermos retornar os registros das duas tabelas seguindo alguma relação, ainda podemos retornar registros que não entram nesta relação, que é a situação mais usada.</p>
<p>Você tem duas opções para se utilizar no Outer Join:</p>
<p><em>- LEFT OUTER JOIN (ou simplesmente LEFT JOIN):</em> Aplica o conceito de Outer Join na tabela que se encontrar à esquerda da relação (ou seja, o resultado vem da tabela à esquerda). No nosso exemplo, é o JOIN que resolve a situação 1: a consulta retorna apenas os clientes que ainda não efetuaram nenhuma compra na loja.</p>
<p><strong><span style="background-color:#c0c0c0;">SELECT <em>campos </em>FROM <em>Clientes c <span style="text-decoration:underline;">LEFT OUTER</span> </em>JOIN <em>Venda v</em> ON <em>c.cod_cliente </em>=<em> v.cod_cliente WHERE c.cod_cliente IS NULL</em>;</span></strong></p>
<p><em>- RIGHT OUTER JOIN (ou simplesmente RIGHT JOIN): </em>Aplica o conceito de Outer Join na tabela que se encontrar à direita da relação (ou seja, o resultado vem da tabela à direita). No nosso exemplo, é o JOIN que resolve a situação 2: a consulta retorna apenas asvendas que foram realizadas por clientes não cadastrados na loja.</p>
<p><strong><span style="background-color:#c0c0c0;">SELECT <em>campos </em>FROM <em>Clientes c <span style="text-decoration:underline;">RIGHT OUTER</span> </em>JOIN <em>Venda v</em> ON <em>c.cod_cliente </em>=<em> v.cod_cliente WHERE v.cod_cliente IS NULL</em>;</span></strong></p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[Austin Data Nerds RIGHT JOIN]]></title>
<link>http://blog.infochimps.org/2008/06/22/austin-data-nerds-right-join/</link>
<pubDate>Sun, 22 Jun 2008 21:26:30 +0000</pubDate>
<dc:creator>mrflip</dc:creator>
<guid>http://blog.infochimps.org/2008/06/22/austin-data-nerds-right-join/</guid>
<description><![CDATA[Do you work with huge datasets?  Are you interested in the opportunities that abound when the world]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p>Do you work with huge datasets?  Are you interested in the opportunities that abound when the world&#8217;s free open data are integrated and the Semantic Web becomes a reality?  Come discuss data mashups, visualizations, and tools to organize, discover and explore rich information streams.</p>
<p>Join us at Mangia pizza on Guadalupe (3016 Guadalupe #100: free wifi, beer &#38; great pizza) Thursday July 10th at 6:30pm and meet your fellow Austin data nerds.</p>
<p>(Also at <a href="http://upcoming.yahoo.com/event/827623/">Upcoming</a>)</p>
</div>]]></content:encoded>
</item>

</channel>
</rss>
