<?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>left-join &amp;laquo; WordPress.com Tag Feed</title>
	<link>http://en.wordpress.com/tag/left-join/</link>
	<description>Feed of posts on WordPress.com tagged "left-join"</description>
	<pubDate>Thu, 03 Dec 2009 14:04:42 +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[C#: Left outer joins with LINQ]]></title>
<link>http://smehrozalam.wordpress.com/2009/06/10/c-left-outer-joins-with-linq/</link>
<pubDate>Wed, 10 Jun 2009 10:45:31 +0000</pubDate>
<dc:creator>Syed Mehroz Alam</dc:creator>
<guid>http://smehrozalam.wordpress.com/2009/06/10/c-left-outer-joins-with-linq/</guid>
<description><![CDATA[I always considered Left Outer Join in LINQ to be complex until today when I had to use it in my app]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p>I always considered Left Outer Join in LINQ to be complex until today when I had to use it in my application. I googled and the first <a href="http://bhaidar.net/cs/archive/2007/08/01/left-outer-join-in-linq-to-sql.aspx">result</a> gave a very nice explanation. The only difference between ordinary joins (inner joins) and left joins in LINQ is the use of &#8220;join into&#8221; and &#8220;DefaultIfEmpty()&#8221; expressions. </p>
<p>Consider this very simple query (Assuming a scenario that not all the TimesheetLines are associated with a Job)</p>
<pre class="brush: sql;">
Select TL.EntryDate, TL.Hours, J.JobName
From TimeSheetLines TL
Left Join Jobs J on TL.JobNo=J.JobNo
</pre>
<p>A LINQ query using inner join is</p>
<pre class="brush: csharp;">
var lines =
    from tl in db.TimeSheetLines
    join j  in db.Jobs on tl.JobNo equals j.JobNo
    where tl.ResourceNo == resourceNo

    select new
    {
        EntryDate = tl.EntryDate,
        Hours = tl.Hours,
        Job = j.JobName
    };
</pre>
<p>And a LINQ query performing left join is</p>
<pre class="brush: csharp;">
var lines =
    from tl in db.TimeSheetLines
    join j  in db.Jobs on tl.JobNo equals j.JobNo into tl_j
    where tl.ResourceNo == resourceNo

    from j in tl_j.DefaultIfEmpty()
    select new
    {
        EntryDate = tl.EntryDate,
        Hours = tl.Hours,
        Job = j.JobName
    };
</pre>
<p>Notice that the only difference is the use of &#8220;<strong><code>into</code></strong>&#8221; with the join statement followed by reselecting the result using &#8220;<strong><code>DefaultIfEmpty</code></strong>()&#8221; expression. And here&#8217;s the generated SQL from the above LINQ expression.</p>
<pre class="brush: sql;">
SELECT [t0].[EntryDate] as [EntryDate], [t0].[Hours] as [Hours], [t1].[JobName] AS [Job]
FROM [dbo].[TimeSheetLine] AS [t0]
LEFT OUTER JOIN [dbo].[Jobs] AS [t1] ON [t0].[JobNo] = [t1].[JobNo]
WHERE [t0].[ResourceNo] = @p0
</pre>
<p>Similar concept can be expanded for multiple left joins. Assuming that a TimeSheetLine will either have a JobNo or an IndirectCode, consider this SQL query:</p>
<pre class="brush: sql;">
Select TL.EntryDate, TL.Hours, J.JobName, I.IndirectName
From TimeSheetLines TL
Left Join Jobs J on TL.JobNo=J.JobNo
Left Join Indirects I on TL.IndirectCode=I.IndirectCode
</pre>
<p>The equivalent LINQ query is:</p>
<pre class="brush: csharp;">
var lines =
    from tl in db.TimeSheetLines
    join j in db.Jobs      on tl.JobNo        equals j.JobNo            into tl_j
    join i in db.Indirects on tl.IndirectCode equals i.IndirectCode  into tl_i
    where tl.ResourceNo == resourceNo

    from j in tl_j.DefaultIfEmpty()
    from i in tl_i.DefaultIfEmpty()
    select new
    {
        EntryDate = tl.EntryDate,
        Hours = tl.Hours,
        Job = j.JobName,
        Indirect = i.IndirectName,
    };
</pre>
<p>And the generated SQL is:</p>
<pre class="brush: sql;">
SELECT [t0].[EntryDate] as [EntryDate], [t0].[Hours] as [Hours], [t1].[JobName] AS [Job], [t2].[IndirectName] As [Indirect]
LEFT OUTER JOIN [dbo].[Jobs] AS [t1] ON [t0].[JobNo] = [t1].[JobNo]
LEFT OUTER JOIN [dbo].[Indirects] AS [t2] ON [t0].[IndirectCode] = [t2].[IndirectCode]
WHERE [t0].[ResourceNo] = @p0
</pre>
<p>That&#8217;s all, left outer joins in LINQ are as easy as in T-SQL. Happy joining.</p>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[LINQ: LEFT OUTER JOIN]]></title>
<link>http://webjagger.wordpress.com/2009/05/29/linq-left-outer-join/</link>
<pubDate>Fri, 29 May 2009 07:39:06 +0000</pubDate>
<dc:creator>webjagger</dc:creator>
<guid>http://webjagger.wordpress.com/2009/05/29/linq-left-outer-join/</guid>
<description><![CDATA[Muss das wirklich so grausam aussehen? So sahen jedenfalls die Beispiele aus, die ich gefunden habe:]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p>Muss das wirklich so grausam aussehen? So sahen jedenfalls die Beispiele aus, die ich gefunden habe:</p>
<p>Dim oDatenPlan = From oMon In listMonate _<br />
                 Group Join oUppUp In dcUpp.tUppUmsatzPlans On oMon Equals oUppUp.Monat _<br />
                 Into gVorlage = Group _<br />
                 From oUppUp In gVorlage.DefaultIfEmpty() _<br />
                 Where If(oUppUp Is Nothing, -1, oUppUp.FkFilialId) = oFiliale.FilialId _<br />
                 And If(oUppUp Is Nothing, -1, oUppUp.FkAbtId) = abtId _<br />
                 Select oMon</p>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[Learning Sql Server Joins In Form of Venn Diagrams]]></title>
<link>http://pankajlalwani.wordpress.com/2009/05/28/learning-sql-server-joins-in-form-of-venn-diagrams/</link>
<pubDate>Thu, 28 May 2009 11:50:38 +0000</pubDate>
<dc:creator>pankajlalwani</dc:creator>
<guid>http://pankajlalwani.wordpress.com/2009/05/28/learning-sql-server-joins-in-form-of-venn-diagrams/</guid>
<description><![CDATA[Learn Basic Concepts of Sql joins like Inner Join Outer Join Cross Join Self Join etc.. in the most ]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p>Learn Basic Concepts of Sql joins like<br />
<strong>Inner Join<br />
Outer Join<br />
Cross Join<br />
Self Join</strong><br />
etc..<br />
in the most simpler manner by pinal dave in the form of venn diagram.<br />
He has explained sql joins with proper examples.. very good article for newbies on sql server</p>
<p><a title="Sql Server Joins" href="http://dotnetslackers.com/articles/sql/SQL-SERVER-JOINs.aspx" target="_blank">http://dotnetslackers.com/articles/sql/SQL-SERVER-JOINs.aspx</a></p>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[Hidden Complexities [part 3]]]></title>
<link>http://2009summer.wordpress.com/2009/04/30/hidden-complexities-part-3/</link>
<pubDate>Thu, 30 Apr 2009 05:27:32 +0000</pubDate>
<dc:creator>peterpotter</dc:creator>
<guid>http://2009summer.wordpress.com/2009/04/30/hidden-complexities-part-3/</guid>
<description><![CDATA[So, the front end was working&#8212;the controller and the view seemed to function properly. I turne]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p>So, the front end was working&#8212;the controller and the view seemed to function properly. I turned to the back end, the database.</p>
<p>Since the <strong>User Account  Management</strong> module is a sister of the <strong>Role Permissions Management</strong> module, which I finished earlier, I thought I wouldn&#8217;t have any more problems on the querying part. The two modules use the same database tables and I was advised by my supervisors to use <a href="http://www.w3schools.com/sql/sql_join_left.asp">left joins</a> still, which I believed I properly utilized in the &#8216;Role&#8217; module. However, a second look at the module I thought was &#8220;<em>Ok na</em>&#8221; revealed a <strong>fat database bug.</strong></p>
<p><!--more--></p>
<p>I thought I used the <a href="http://www.w3schools.com/sql/sql_join_left.asp"><strong>LEFT JOIN</strong></a> properly. What I needed was a query which would retrieve from two tables all the modules with their <strong>id</strong> and <strong>name</strong> and a value indicating whether or not the module is selected for a certain role. With left join, you retrieve all tuples that match on the join column and if there is an entry in the left table that has no match in the right table, the result will still contain the left table entry with the right table columns filled with <strong>nulls</strong>.</p>
<p>The problem that I did not notice before was that if, let&#8217;s say, for a module <strong>M</strong>, two roles <strong>R1</strong> and <strong>R2</strong> can access it where <strong>M</strong> is in <strong>Table1</strong>(the table of modules) and <strong>R1</strong> and <strong>R2</strong> are in <strong>Table2</strong>(the table listing accessible modules for roles), the result would contain two rows for module <strong>M</strong>: <em>[M, R1] and [M, R2]</em>. It would appear that there are two modules <strong>M</strong> where there is  only one.</p>
<p>In short, the query was in error all along. And the result, being erroneous, cannot be displayed.</p>
<p>I decided to fix the &#8216;Role&#8217; module first before continuing the &#8216;User Account&#8217; module (since they have similar queries).</p>
<p>Unfortunate for me, I was all alone at the office when I discovered the bug. No-coworkers nor supervisors yet. Thus, I tried to solve the database problem alone&#8212;in solitude&#8212;trying query modifications and visiting tutorials on the net. After many attempts to fix the query, the result was still wrong.</p>
<p>After a while, I realized I could solve the problem in the application layer instead. And after implementing a strategy, I finally got the correct output. I won&#8217;t outline the solution anymore because it would be too technical. Suffice it to say that I used loops and hash tables to produce the correct display.</p>
<p>Then, I applied the same application-layer strategy on the <strong>User Account Management</strong> module, and it worked <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<div style="border:1px solid #ffcc00;background-color:#fff4c8;color:#77756a;font-family:arial,helvetica,sans-serif;font-size:11px;line-height:1.2;margin-left:50px;margin-top:5px;vertical-align:top;width:230px;padding:8px 8px 15px;">
<blockquote>
<h3><b>Lesson:</b></h3>
<p> With whatever module or project, it is always better to <strong>anticipate difficulties</strong>; if it turns out to be easy, then good; but who knows, you might just be in for some <strong>hidden complexities</strong>.</p></blockquote>
</div>
<p>~Peter</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[Business Logic with JOIN - Multiple Join examples in SQL-Server 2005:]]></title>
<link>http://riteshshah.wordpress.com/2009/03/09/business-logic-with-join-multiple-join-examples-in-sql-server-2005/</link>
<pubDate>Mon, 09 Mar 2009 05:27:24 +0000</pubDate>
<dc:creator>riteshshah</dc:creator>
<guid>http://riteshshah.wordpress.com/2009/03/09/business-logic-with-join-multiple-join-examples-in-sql-server-2005/</guid>
<description><![CDATA[Business Logic with JOIN &#8211; Multiple Join examples in SQL-Server 2005: I wrote my previous arti]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p><P class="MsoNormal" style="margin:0 0 10pt;"><B><FONT face="Calibri">Business Logic with JOIN &#8211; Multiple Join examples in SQL-Server 2005:</FONT></B></P><br />
<P class="MsoNormal" style="margin:0 0 10pt;"><FONT face="Calibri">I wrote my previous article about different type of JOIN in SQL-Server 2005. Due to the space constraint in that article, complex example has not been given there, only few simple examples.<SPAN>  </SPAN>I felt to explode this topic further by giving solution to some really needed business logic.</FONT></P><br />
<P class="MsoNormal" style="margin:0 0 10pt;"><FONT face="Calibri">If you want to create report for blood testing laboratory, who used to check samples of employee of different companies for different purposes. Following should be the table structure, we will be creating three table. One will store information about Order received from different companies. Another one will be the detail of that order, means which company sent how much samples of how much employee?? And the last one is further more details about how much test should be performs on which sample???</FONT></P><br />
<P class="MsoNormal" style="margin:0 0 10pt;"><FONT face="Calibri">Let us create three tables.</FONT></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;color:green;font-family:'Courier New';">&#8211;CREATING FIRST ORDER TABLE</SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;color:blue;font-family:'Courier New';">USE</SPAN><SPAN style="font-size:10pt;font-family:'Courier New';"> [AdventureWorks]</SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;font-family:'Courier New';">GO</SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;color:green;font-family:'Courier New';">/****** Object:<SPAN>  </SPAN>Table [dbo].[Orders]<SPAN>    </SPAN>Script Date: 03/08/2009 12:24:39 ******/</SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;color:blue;font-family:'Courier New';">SET</SPAN><SPAN style="font-size:10pt;font-family:'Courier New';"> <SPAN style="color:blue;">ANSI_NULLS</SPAN> <SPAN style="color:blue;">ON</SPAN></SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;font-family:'Courier New';">GO</SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;color:blue;font-family:'Courier New';">SET</SPAN><SPAN style="font-size:10pt;font-family:'Courier New';"> <SPAN style="color:blue;">QUOTED_IDENTIFIER</SPAN> <SPAN style="color:blue;">ON</SPAN></SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;font-family:'Courier New';">GO</SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;color:blue;font-family:'Courier New';">SET</SPAN><SPAN style="font-size:10pt;font-family:'Courier New';"> <SPAN style="color:blue;">ANSI_PADDING</SPAN> <SPAN style="color:blue;">ON</SPAN></SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;font-family:'Courier New';">GO</SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;color:blue;font-family:'Courier New';">CREATE</SPAN><SPAN style="font-size:10pt;font-family:'Courier New';"> <SPAN style="color:blue;">TABLE</SPAN> [dbo]<SPAN style="color:gray;">.</SPAN>[Orders]<SPAN style="color:gray;">(</SPAN></SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;font-family:'Courier New';"><SPAN>      </SPAN>[OrderID] [varchar]<SPAN style="color:gray;">(</SPAN>5<SPAN style="color:gray;">)</SPAN> <SPAN style="color:blue;">COLLATE</SPAN> SQL_Latin1_General_CP1_CI_AS <SPAN style="color:gray;">NOT</SPAN> <SPAN style="color:gray;">NULL,</SPAN></SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;font-family:'Courier New';"><SPAN>      </SPAN>[OrderDate] [datetime] <SPAN style="color:gray;">NOT</SPAN> <SPAN style="color:gray;">NULL,</SPAN></SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;font-family:'Courier New';"><SPAN>      </SPAN>[CompanyName] [varchar]<SPAN style="color:gray;">(</SPAN>10<SPAN style="color:gray;">)</SPAN> <SPAN style="color:blue;">COLLATE</SPAN> SQL_Latin1_General_CP1_CI_AS <SPAN style="color:gray;">NULL,</SPAN></SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;color:blue;font-family:'Courier New';">PRIMARY</SPAN><SPAN style="font-size:10pt;font-family:'Courier New';"> <SPAN style="color:blue;">KEY</SPAN> <SPAN style="color:blue;">CLUSTERED</SPAN> </SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;color:gray;font-family:'Courier New';">(</SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;font-family:'Courier New';"><SPAN>      </SPAN>[OrderID] <SPAN style="color:blue;">ASC</SPAN></SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;color:gray;font-family:'Courier New';">)</SPAN><SPAN style="font-size:10pt;color:blue;font-family:'Courier New';">WITH</SPAN><SPAN style="font-size:10pt;font-family:'Courier New';"> <SPAN style="color:gray;">(</SPAN>PAD_INDEX<SPAN>  </SPAN><SPAN style="color:gray;">=</SPAN> <SPAN style="color:blue;">OFF</SPAN><SPAN style="color:gray;">,</SPAN> STATISTICS_NORECOMPUTE<SPAN>  </SPAN><SPAN style="color:gray;">=</SPAN> <SPAN style="color:blue;">OFF</SPAN><SPAN style="color:gray;">,</SPAN> IGNORE_DUP_KEY <SPAN style="color:gray;">=</SPAN> <SPAN style="color:blue;">OFF</SPAN><SPAN style="color:gray;">,</SPAN> ALLOW_ROW_LOCKS<SPAN>  </SPAN><SPAN style="color:gray;">=</SPAN> <SPAN style="color:blue;">ON</SPAN><SPAN style="color:gray;">,</SPAN> ALLOW_PAGE_LOCKS<SPAN>  </SPAN><SPAN style="color:gray;">=</SPAN> <SPAN style="color:blue;">ON</SPAN><SPAN style="color:gray;">)</SPAN> <SPAN style="color:blue;">ON</SPAN> [PRIMARY]</SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;color:gray;font-family:'Courier New';">)</SPAN><SPAN style="font-size:10pt;font-family:'Courier New';"> <SPAN style="color:blue;">ON</SPAN> [PRIMARY]</SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;font-family:'Courier New';"> </SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;font-family:'Courier New';">GO</SPAN></P><br />
<P class="MsoNormal" style="margin:0 0 10pt;"><SPAN style="font-size:10pt;color:blue;line-height:115%;font-family:'Courier New';">SET</SPAN><SPAN style="font-size:10pt;line-height:115%;font-family:'Courier New';"> <SPAN style="color:blue;">ANSI_PADDING</SPAN> <SPAN style="color:blue;">ON</SPAN></SPAN></P><br />
<P class="MsoNormal" style="margin:0 0 10pt;"><FONT face="Calibri">Now, child table of above.</FONT></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;font-family:'Courier New';"> </SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;color:blue;font-family:'Courier New';">USE</SPAN><SPAN style="font-size:10pt;font-family:'Courier New';"> [AdventureWorks]</SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;font-family:'Courier New';">GO</SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;color:green;font-family:'Courier New';">/****** Object:<SPAN>  </SPAN>Table [dbo].[OrderDetails]<SPAN>    </SPAN>Script Date: 03/08/2009 12:37:27 ******/</SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;color:blue;font-family:'Courier New';">SET</SPAN><SPAN style="font-size:10pt;font-family:'Courier New';"> <SPAN style="color:blue;">ANSI_NULLS</SPAN> <SPAN style="color:blue;">ON</SPAN></SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;font-family:'Courier New';">GO</SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;color:blue;font-family:'Courier New';">SET</SPAN><SPAN style="font-size:10pt;font-family:'Courier New';"> <SPAN style="color:blue;">QUOTED_IDENTIFIER</SPAN> <SPAN style="color:blue;">ON</SPAN></SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;font-family:'Courier New';">GO</SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;color:blue;font-family:'Courier New';">SET</SPAN><SPAN style="font-size:10pt;font-family:'Courier New';"> <SPAN style="color:blue;">ANSI_PADDING</SPAN> <SPAN style="color:blue;">ON</SPAN></SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;font-family:'Courier New';">GO</SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;color:blue;font-family:'Courier New';">CREATE</SPAN><SPAN style="font-size:10pt;font-family:'Courier New';"> <SPAN style="color:blue;">TABLE</SPAN> [dbo]<SPAN style="color:gray;">.</SPAN>[OrderDetails]<SPAN style="color:gray;">(</SPAN></SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;font-family:'Courier New';"><SPAN>      </SPAN>[OrderID] [varchar]<SPAN style="color:gray;">(</SPAN>5<SPAN style="color:gray;">)</SPAN> <SPAN style="color:blue;">COLLATE</SPAN> SQL_Latin1_General_CP1_CI_AS <SPAN style="color:gray;">NULL,</SPAN></SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;font-family:'Courier New';"><SPAN>      </SPAN>[SampleNumber] [varchar]<SPAN style="color:gray;">(</SPAN>8<SPAN style="color:gray;">)</SPAN> <SPAN style="color:blue;">COLLATE</SPAN> SQL_Latin1_General_CP1_CI_AS <SPAN style="color:gray;">NOT</SPAN> <SPAN style="color:gray;">NULL,</SPAN></SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;font-family:'Courier New';"><SPAN>      </SPAN>[SampleOfEmployee] [varchar]<SPAN style="color:gray;">(</SPAN>15<SPAN style="color:gray;">)</SPAN> <SPAN style="color:blue;">COLLATE</SPAN> SQL_Latin1_General_CP1_CI_AS <SPAN style="color:gray;">NOT</SPAN> <SPAN style="color:gray;">NULL,</SPAN></SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;font-family:'Courier New';"><SPAN>      </SPAN>[SampleReceivedDate] [datetime] <SPAN style="color:gray;">NULL,</SPAN></SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;color:blue;font-family:'Courier New';">PRIMARY</SPAN><SPAN style="font-size:10pt;font-family:'Courier New';"> <SPAN style="color:blue;">KEY</SPAN> <SPAN style="color:blue;">CLUSTERED</SPAN> </SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;color:gray;font-family:'Courier New';">(</SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;font-family:'Courier New';"><SPAN>      </SPAN>[SampleNumber] <SPAN style="color:blue;">ASC</SPAN></SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;color:gray;font-family:'Courier New';">)</SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;color:gray;font-family:'Courier New';">)</SPAN><SPAN style="font-size:10pt;font-family:'Courier New';"> <SPAN style="color:blue;">ON</SPAN> [PRIMARY]</SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;font-family:'Courier New';"> </SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;font-family:'Courier New';">GO</SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;color:blue;font-family:'Courier New';">SET</SPAN><SPAN style="font-size:10pt;font-family:'Courier New';"> <SPAN style="color:blue;">ANSI_PADDING</SPAN> <SPAN style="color:blue;">ON</SPAN></SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;font-family:'Courier New';">GO</SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;color:blue;font-family:'Courier New';">ALTER</SPAN><SPAN style="font-size:10pt;font-family:'Courier New';"> <SPAN style="color:blue;">TABLE</SPAN> [dbo]<SPAN style="color:gray;">.</SPAN>[OrderDetails]<SPAN>  </SPAN><SPAN style="color:blue;">WITH</SPAN> <SPAN style="color:blue;">CHECK</SPAN> <SPAN style="color:blue;">ADD</SPAN><SPAN>  </SPAN><SPAN style="color:blue;">CONSTRAINT</SPAN> [fk_orderid] <SPAN style="color:blue;">FOREIGN</SPAN> <SPAN style="color:blue;">KEY</SPAN><SPAN style="color:gray;">(</SPAN>[OrderID]<SPAN style="color:gray;">)</SPAN></SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;color:blue;font-family:'Courier New';">REFERENCES</SPAN><SPAN style="font-size:10pt;font-family:'Courier New';"> [dbo]<SPAN style="color:gray;">.</SPAN>[Orders] <SPAN style="color:gray;">(</SPAN>[OrderID]<SPAN style="color:gray;">)</SPAN></SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;font-family:'Courier New';">GO</SPAN></P><br />
<P class="MsoNormal" style="margin:0 0 10pt;"><SPAN style="font-size:10pt;color:blue;line-height:115%;font-family:'Courier New';">ALTER</SPAN><SPAN style="font-size:10pt;line-height:115%;font-family:'Courier New';"> <SPAN style="color:blue;">TABLE</SPAN> [dbo]<SPAN style="color:gray;">.</SPAN>[OrderDetails] <SPAN style="color:blue;">CHECK</SPAN> <SPAN style="color:blue;">CONSTRAINT</SPAN> [fk_orderid]</SPAN></P><br />
<P class="MsoNormal" style="margin:0 0 10pt;"><FONT face="Calibri">One more child table.</FONT></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;font-family:'Courier New';"> </SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;color:blue;font-family:'Courier New';">USE</SPAN><SPAN style="font-size:10pt;font-family:'Courier New';"> [AdventureWorks]</SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;font-family:'Courier New';">GO</SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;color:green;font-family:'Courier New';">/****** Object:<SPAN>  </SPAN>Table [dbo].[sampledetails]<SPAN>    </SPAN>Script Date: 03/08/2009 12:43:00 ******/</SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;color:blue;font-family:'Courier New';">SET</SPAN><SPAN style="font-size:10pt;font-family:'Courier New';"> <SPAN style="color:blue;">ANSI_NULLS</SPAN> <SPAN style="color:blue;">ON</SPAN></SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;font-family:'Courier New';">GO</SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;color:blue;font-family:'Courier New';">SET</SPAN><SPAN style="font-size:10pt;font-family:'Courier New';"> <SPAN style="color:blue;">QUOTED_IDENTIFIER</SPAN> <SPAN style="color:blue;">ON</SPAN></SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;font-family:'Courier New';">GO</SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;color:blue;font-family:'Courier New';">SET</SPAN><SPAN style="font-size:10pt;font-family:'Courier New';"> <SPAN style="color:blue;">ANSI_PADDING</SPAN> <SPAN style="color:blue;">ON</SPAN></SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;font-family:'Courier New';">GO</SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;color:blue;font-family:'Courier New';">CREATE</SPAN><SPAN style="font-size:10pt;font-family:'Courier New';"> <SPAN style="color:blue;">TABLE</SPAN> [dbo]<SPAN style="color:gray;">.</SPAN>[sampledetails]<SPAN style="color:gray;">(</SPAN></SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;font-family:'Courier New';"><SPAN>      </SPAN>[SampleNumber] [varchar]<SPAN style="color:gray;">(</SPAN>8<SPAN style="color:gray;">)</SPAN> <SPAN style="color:blue;">COLLATE</SPAN> SQL_Latin1_General_CP1_CI_AS <SPAN style="color:gray;">NOT</SPAN> <SPAN style="color:gray;">NULL,</SPAN></SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;font-family:'Courier New';"><SPAN>      </SPAN>[TestType] [varchar]<SPAN style="color:gray;">(</SPAN>10<SPAN style="color:gray;">)</SPAN> <SPAN style="color:blue;">COLLATE</SPAN> SQL_Latin1_General_CP1_CI_AS <SPAN style="color:gray;">NOT</SPAN> <SPAN style="color:gray;">NULL,</SPAN></SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;font-family:'Courier New';"><SPAN>      </SPAN></SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;color:blue;font-family:'Courier New';">PRIMARY</SPAN><SPAN style="font-size:10pt;font-family:'Courier New';"> <SPAN style="color:blue;">KEY</SPAN> <SPAN style="color:blue;">CLUSTERED</SPAN> </SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;color:gray;font-family:'Courier New';">(</SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;font-family:'Courier New';"><SPAN>      </SPAN>[SampleNumber] <SPAN style="color:blue;">ASC</SPAN><SPAN style="color:gray;">,</SPAN></SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;font-family:'Courier New';"><SPAN>      </SPAN>[TestType] <SPAN style="color:blue;">ASC</SPAN></SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;color:gray;font-family:'Courier New';">)</SPAN><SPAN style="font-size:10pt;color:blue;font-family:'Courier New';">WITH</SPAN><SPAN style="font-size:10pt;font-family:'Courier New';"> <SPAN style="color:gray;">(</SPAN>PAD_INDEX<SPAN>  </SPAN><SPAN style="color:gray;">=</SPAN> <SPAN style="color:blue;">OFF</SPAN><SPAN style="color:gray;">,</SPAN> STATISTICS_NORECOMPUTE<SPAN>  </SPAN><SPAN style="color:gray;">=</SPAN> <SPAN style="color:blue;">OFF</SPAN><SPAN style="color:gray;">,</SPAN> IGNORE_DUP_KEY <SPAN style="color:gray;">=</SPAN> <SPAN style="color:blue;">OFF</SPAN><SPAN style="color:gray;">,</SPAN> ALLOW_ROW_LOCKS<SPAN>  </SPAN><SPAN style="color:gray;">=</SPAN> <SPAN style="color:blue;">ON</SPAN><SPAN style="color:gray;">,</SPAN> ALLOW_PAGE_LOCKS<SPAN>  </SPAN><SPAN style="color:gray;">=</SPAN> <SPAN style="color:blue;">ON</SPAN><SPAN style="color:gray;">)</SPAN> <SPAN style="color:blue;">ON</SPAN> [PRIMARY]</SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;color:gray;font-family:'Courier New';">)</SPAN><SPAN style="font-size:10pt;font-family:'Courier New';"> <SPAN style="color:blue;">ON</SPAN> [PRIMARY]</SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;font-family:'Courier New';"> </SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;font-family:'Courier New';">GO</SPAN></P><br />
<P class="MsoNormal" style="margin:0 0 10pt;"><SPAN style="font-size:10pt;color:blue;line-height:115%;font-family:'Courier New';">SET</SPAN><SPAN style="font-size:10pt;line-height:115%;font-family:'Courier New';"> <SPAN style="color:blue;">ANSI_PADDING</SPAN> <SPAN style="color:blue;">ON</SPAN></SPAN></P><br />
<P class="MsoNormal" style="margin:0 0 10pt;"><FONT face="Calibri">Now, this is time to insert few records on above tables.</FONT></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;color:green;font-family:'Courier New';">&#8211;Received three order from two company</SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;color:blue;font-family:'Courier New';">INSERT</SPAN><SPAN style="font-size:10pt;font-family:'Courier New';"> <SPAN style="color:blue;">INTO</SPAN> Orders</SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;color:blue;font-family:'Courier New';">SELECT</SPAN><SPAN style="font-size:10pt;font-family:'Courier New';"> <SPAN style="color:red;">&#8216;L1000&#8242;</SPAN><SPAN style="color:gray;">,</SPAN><SPAN style="color:red;">&#8216;2009-03-01&#8242;</SPAN><SPAN style="color:gray;">,</SPAN><SPAN style="color:red;">&#8216;Test Inc.&#8217;</SPAN> <SPAN style="color:blue;">UNION</SPAN> <SPAN style="color:gray;">ALL</SPAN></SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;color:blue;font-family:'Courier New';">SELECT</SPAN><SPAN style="font-size:10pt;font-family:'Courier New';"> <SPAN style="color:red;">&#8216;L1001&#8242;</SPAN><SPAN style="color:gray;">,</SPAN><SPAN style="color:red;">&#8216;2009-03-02&#8242;</SPAN><SPAN style="color:gray;">,</SPAN><SPAN style="color:red;">&#8216;Zeel Inc.&#8217;</SPAN> <SPAN style="color:blue;">UNION</SPAN> <SPAN style="color:gray;">ALL</SPAN></SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;color:blue;font-family:'Courier New';">SELECT</SPAN><SPAN style="font-size:10pt;font-family:'Courier New';"> <SPAN style="color:red;">&#8216;L1002&#8242;</SPAN><SPAN style="color:gray;">,</SPAN><SPAN style="color:red;">&#8216;2009-03-04&#8242;</SPAN><SPAN style="color:gray;">,</SPAN><SPAN style="color:red;">&#8216;ABC Inc.&#8217;</SPAN></SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;color:red;font-family:'Courier New';"> </SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;color:green;font-family:'Courier New';">&#8211;received two sample for first order, one sample for second order</SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;color:green;font-family:'Courier New';">&#8211;sample for third order yet to receive</SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;color:blue;font-family:'Courier New';">INSERT</SPAN><SPAN style="font-size:10pt;font-family:'Courier New';"> <SPAN style="color:blue;">INTO</SPAN> OrderDetails</SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;color:blue;font-family:'Courier New';">SELECT</SPAN><SPAN style="font-size:10pt;font-family:'Courier New';"> <SPAN style="color:red;">&#8216;L1000&#8242;</SPAN><SPAN style="color:gray;">,</SPAN><SPAN style="color:red;">&#8216;L1000-01&#8242;</SPAN><SPAN style="color:gray;">,</SPAN><SPAN style="color:red;">&#8216;James&#8217;</SPAN><SPAN style="color:gray;">,</SPAN><SPAN style="color:red;">&#8216;2009-03-01&#8242;</SPAN> <SPAN style="color:blue;">UNION</SPAN> <SPAN style="color:gray;">ALL</SPAN></SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;color:blue;font-family:'Courier New';">SELECT</SPAN><SPAN style="font-size:10pt;font-family:'Courier New';"> <SPAN style="color:red;">&#8216;L1000&#8242;</SPAN><SPAN style="color:gray;">,</SPAN><SPAN style="color:red;">&#8216;L1000-02&#8242;</SPAN><SPAN style="color:gray;">,</SPAN><SPAN style="color:red;">&#8216;John&#8217;</SPAN><SPAN style="color:gray;">,</SPAN><SPAN style="color:red;">&#8216;2009-04-01&#8242;</SPAN> <SPAN style="color:blue;">UNION</SPAN> <SPAN style="color:gray;">ALL</SPAN></SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;color:blue;font-family:'Courier New';">SELECT</SPAN><SPAN style="font-size:10pt;font-family:'Courier New';"> <SPAN style="color:red;">&#8216;L1001&#8242;</SPAN><SPAN style="color:gray;">,</SPAN><SPAN style="color:red;">&#8216;L1001-01&#8242;</SPAN><SPAN style="color:gray;">,</SPAN><SPAN style="color:red;">&#8216;Smita&#8217;</SPAN><SPAN style="color:gray;">,</SPAN><SPAN style="color:red;">&#8216;2009-03-05&#8242;</SPAN></SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;font-family:'Courier New';"><SPAN> </SPAN></SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;color:green;font-family:'Courier New';">&#8211;details of sample, which test suppose to be performed</SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;color:green;font-family:'Courier New';">&#8211;on which sample</SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;color:blue;font-family:'Courier New';">INSERT</SPAN><SPAN style="font-size:10pt;font-family:'Courier New';"> <SPAN style="color:blue;">INTO</SPAN> SampleDetails</SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;color:blue;font-family:'Courier New';">SELECT</SPAN><SPAN style="font-size:10pt;font-family:'Courier New';"> <SPAN style="color:red;">&#8216;L1000-01&#8242;</SPAN><SPAN style="color:gray;">,</SPAN><SPAN style="color:red;">&#8216;Cancer&#8217;</SPAN> <SPAN style="color:blue;">UNION</SPAN> <SPAN style="color:gray;">ALL</SPAN></SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;color:blue;font-family:'Courier New';">SELECT</SPAN><SPAN style="font-size:10pt;font-family:'Courier New';"> <SPAN style="color:red;">&#8216;L1000-01&#8242;</SPAN><SPAN style="color:gray;">,</SPAN><SPAN style="color:red;">&#8216;AIDS&#8217;</SPAN> <SPAN style="color:blue;">UNION</SPAN> <SPAN style="color:gray;">ALL</SPAN></SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;color:blue;font-family:'Courier New';">SELECT</SPAN><SPAN style="font-size:10pt;font-family:'Courier New';"> <SPAN style="color:red;">&#8216;L1000-02&#8242;</SPAN><SPAN style="color:gray;">,</SPAN><SPAN style="color:red;">&#8216;BP&#8217;</SPAN> <SPAN style="color:blue;">UNION</SPAN> <SPAN style="color:gray;">ALL</SPAN></SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;color:blue;font-family:'Courier New';">SELECT</SPAN><SPAN style="font-size:10pt;font-family:'Courier New';"> <SPAN style="color:red;">&#8216;L1001-01&#8242;</SPAN><SPAN style="color:gray;">,</SPAN><SPAN style="color:red;">&#8216;AIDS&#8217;</SPAN> <SPAN style="color:blue;">UNION</SPAN> <SPAN style="color:gray;">ALL</SPAN></SPAN></P><br />
<P class="MsoNormal" style="margin:0 0 10pt;"><SPAN style="font-size:10pt;color:blue;line-height:115%;font-family:'Courier New';">SELECT</SPAN><SPAN style="font-size:10pt;line-height:115%;font-family:'Courier New';"> <SPAN style="color:red;">&#8216;L1001-01&#8242;</SPAN><SPAN style="color:gray;">,</SPAN><SPAN style="color:red;">&#8216;Cancer&#8217;</SPAN></SPAN></P><br />
<P class="MsoNormal" style="margin:0 0 10pt;"><FONT face="Calibri">Well, now we are ready to pull data out of three tables with different type of JOIN which we have seen in my previous article.</FONT></P><br />
<P class="MsoNormal" style="margin:0 0 10pt;"><B><FONT face="Calibri">NOTE: many different logic can be used to perform following tasks, I am giving this just to explain the use of JOIN otherwise there are some more facility which can handle all of the below situation other than join.</FONT></B></P><br />
<P class="MsoNormal" style="margin:0 0 10pt;"><FONT face="Calibri">If you want to get OrderID, OrderDate, Company (who gave the order??), SampleNumber, EmployeeName (for which employee company has sent the sample??), TestType (which test to be performed in above tables?)</FONT></P><br />
<P class="MsoNormal" style="margin:0 0 10pt;"><FONT face="Calibri">You can use following query which has used one LEFT OUTER JOIN and one INNER JOIN.</FONT></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;color:blue;font-family:'Courier New';">SELECT</SPAN><SPAN style="font-size:10pt;font-family:'Courier New';"> O<SPAN style="color:gray;">.</SPAN>OrderID<SPAN style="color:gray;">,</SPAN>O<SPAN style="color:gray;">.</SPAN>OrderDate<SPAN style="color:gray;">,</SPAN>O<SPAN style="color:gray;">.</SPAN>CompanyName<SPAN style="color:gray;">,</SPAN></SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;font-family:'Courier New';">OD<SPAN style="color:gray;">.</SPAN>SampleofEmployee<SPAN style="color:gray;">,</SPAN>OD<SPAN style="color:gray;">.</SPAN>SampleNumber<SPAN style="color:gray;">,</SPAN>SD<SPAN style="color:gray;">.</SPAN>TestType</SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;color:blue;font-family:'Courier New';">FROM</SPAN><SPAN style="font-size:10pt;font-family:'Courier New';"> </SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;font-family:'Courier New';">ORDERS <SPAN style="color:blue;">AS</SPAN> O</SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;color:gray;font-family:'Courier New';">LEFT</SPAN><SPAN style="font-size:10pt;font-family:'Courier New';"> <SPAN style="color:gray;">OUTER</SPAN> <SPAN style="color:gray;">JOIN</SPAN></SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;font-family:'Courier New';">ORDERDETAILS <SPAN style="color:blue;">AS</SPAN> OD</SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;color:blue;font-family:'Courier New';">ON</SPAN><SPAN style="font-size:10pt;font-family:'Courier New';"> </SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;font-family:'Courier New';">O<SPAN style="color:gray;">.</SPAN>OrderID<SPAN style="color:gray;">=</SPAN>OD<SPAN style="color:gray;">.</SPAN>OrderID</SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;color:gray;font-family:'Courier New';">INNER</SPAN><SPAN style="font-size:10pt;font-family:'Courier New';"> <SPAN style="color:gray;">JOIN</SPAN></SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;font-family:'Courier New';">SampleDetails</SPAN><SPAN style="font-size:10pt;font-family:'Courier New';"> <SPAN style="color:blue;">AS</SPAN></SPAN><SPAN style="font-size:10pt;font-family:'Courier New';"> SD</SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;color:blue;font-family:'Courier New';">ON</SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;font-family:'Courier New';">OD<SPAN style="color:gray;">.</SPAN>SampleNumber<SPAN style="color:gray;">=</SPAN>SD<SPAN style="color:gray;">.</SPAN>SampleNumber</SPAN></P><br />
<P class="MsoNormal" style="margin:0 0 10pt;"><FONT face="Calibri"> </FONT></P><br />
<P class="MsoNormal" style="margin:0 0 10pt;"><FONT face="Calibri">If there is a need to find how many samples came from which company. Use following query with aggregate function COUNT.</FONT></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;font-family:'Courier New';"> </SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;color:blue;font-family:'Courier New';">SELECT</SPAN><SPAN style="font-size:10pt;font-family:'Courier New';"> </SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;font-family:'Courier New';">O<SPAN style="color:gray;">.</SPAN>CompanyName<SPAN style="color:gray;">,</SPAN><SPAN style="color:fuchsia;">Count</SPAN><SPAN style="color:gray;">(</SPAN>SampleNumber<SPAN style="color:gray;">)</SPAN> <SPAN style="color:blue;">as</SPAN> <SPAN style="color:red;">&#8216;TOTAL&#8217;</SPAN></SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;color:blue;font-family:'Courier New';">FROM</SPAN><SPAN style="font-size:10pt;font-family:'Courier New';"> </SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;font-family:'Courier New';">ORDERS <SPAN style="color:blue;">AS</SPAN> O</SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;color:gray;font-family:'Courier New';">LEFT</SPAN><SPAN style="font-size:10pt;font-family:'Courier New';"> <SPAN style="color:gray;">OUTER</SPAN> <SPAN style="color:gray;">JOIN</SPAN></SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;font-family:'Courier New';">ORDERDETAILS <SPAN style="color:blue;">AS</SPAN> OD</SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;color:blue;font-family:'Courier New';">ON</SPAN><SPAN style="font-size:10pt;font-family:'Courier New';"> </SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;font-family:'Courier New';">O<SPAN style="color:gray;">.</SPAN>OrderID<SPAN style="color:gray;">=</SPAN>OD<SPAN style="color:gray;">.</SPAN>OrderID</SPAN></P><br />
<P class="MsoNormal" style="line-height:normal;margin:0;"><SPAN style="font-size:10pt;color:blue;font-family:'Courier New';">GROUP</SPAN><SPAN style="font-size:10pt;font-family:'Courier New';"> <SPAN style="color:blue;">BY</SPAN> O<SPAN style="color:gray;">.</SPAN>CompanyName</SPAN></P><br />
<P class="MsoNormal" style="margin:0 0 10pt;"><SPAN style="font-size:10pt;color:blue;line-height:115%;font-family:'Courier New';">ORDER</SPAN><SPAN style="font-size:10pt;line-height:115%;font-family:'Courier New';"> <SPAN style="color:blue;">BY</SPAN> TOTAL <SPAN style="color:blue;">DESC</SPAN></SPAN></P><br />
<P class="MsoNormal" style="margin:0 0 10pt;"><B><SPAN style="font-size:12pt;line-height:115%;"><FONT face="Calibri">Reference: Ritesh Shah</FONT></SPAN></B></P></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[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[Diferencias entre dos datatables]]></title>
<link>http://febrer.wordpress.com/2009/02/10/diferencias-entre-dos-datatables/</link>
<pubDate>Tue, 10 Feb 2009 11:55:30 +0000</pubDate>
<dc:creator>febrer</dc:creator>
<guid>http://febrer.wordpress.com/2009/02/10/diferencias-entre-dos-datatables/</guid>
<description><![CDATA[Este es un interesante script que devuelve la diferencia entre dos datatables. Su funcionamiento es ]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p>Este es un interesante script que devuelve la diferencia entre dos datatables. Su funcionamiento es similar a realizar un LEFT JOIN en Transact SQL. La diferencia es que en este método, utilizamos el objeto DataRelation de .NET. He tenido que utilizar este método ya que los origenes de los datatables, son de conexiones direferentes, por lo que la utilización de LEFT JOIN, no es posible.</p>
<blockquote><p>///<br />
/// Devuelve un datatable con las diferencias entre los datatables de entrada<br />
///<br />
///<br />
1er datatable ///<br />
2o datatable ///<br />
public static DataTable Difference(DataTable First, DataTable Second)<br />
{<br />
//creamos una tabla vacia<br />
DataTable table = new DataTable(&#8220;Diferencias&#8221;);</p>
<p>//creamos un dataSet para poder utilizar el objeto DataRelation<br />
using (DataSet ds = new DataSet())<br />
{<br />
//añadimos una copia de las tablas<br />
ds.Tables.AddRange(new DataTable[] { First.Copy(), Second.Copy() });<br />
//creamos un dataColumn con las columnas del datatable 1.<br />
DataColumn[] firstcolumns = new DataColumn[ds.Tables[0].Columns.Count];<br />
for (int i = 0; i &#60; firstcolumns.Length; i++)<br />
{<br />
firstcolumns[i] = ds.Tables[0].Columns[i];<br />
}</p>
<p>DataColumn[] secondcolumns = new DataColumn[ds.Tables[1].Columns.Count];<br />
for (int i = 0; i &#60; secondcolumns.Length; i++)<br />
{<br />
secondcolumns[i] = ds.Tables[1].Columns[i];<br />
}<br />
//creamos la relación<br />
DataRelation r = new DataRelation(string.Empty, firstcolumns, secondcolumns, false);<br />
ds.Relations.Add(r);</p>
<p>//creamos las columnas en el datatable a devolver.<br />
for (int i = 0; i &#60; First.Columns.Count; i++)<br />
{<br />
table.Columns.Add(First.Columns[i].ColumnName, First.Columns[i].DataType);<br />
}</p>
<p>//si la primera fila no esta en la segunda tabla, la añadimos a la tabla.<br />
table.BeginLoadData();<br />
foreach (DataRow parentrow in ds.Tables[0].Rows)<br />
{<br />
DataRow[] childrows = parentrow.GetChildRows(r);<br />
if (childrows == null &#124;&#124; childrows.Length == 0)<br />
table.LoadDataRow(parentrow.ItemArray, true);<br />
}<br />
table.EndLoadData();<br />
}</p>
<p>return table;<br />
}</p></blockquote>
</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[left outer joins]]></title>
<link>http://nullpointers.wordpress.com/2008/08/05/left-outer-joins/</link>
<pubDate>Tue, 05 Aug 2008 10:32:14 +0000</pubDate>
<dc:creator>Intensity !</dc:creator>
<guid>http://nullpointers.wordpress.com/2008/08/05/left-outer-joins/</guid>
<description><![CDATA[today we faced an interesting problem regarding left outer joins. i will skip the details and come t]]></description>
<content:encoded><![CDATA[today we faced an interesting problem regarding left outer joins. i will skip the details and come t]]></content:encoded>
</item>
<item>
<title><![CDATA[Hirarki data di MySQL]]></title>
<link>http://noryahya.wordpress.com/2008/08/05/hirarki-data-di-mysql/</link>
<pubDate>Tue, 05 Aug 2008 09:35:58 +0000</pubDate>
<dc:creator>noryahya</dc:creator>
<guid>http://noryahya.wordpress.com/2008/08/05/hirarki-data-di-mysql/</guid>
<description><![CDATA[Ada beberapa programmer yang dihadapkan dengan kasus membuat hirarki data dalam SQL database. Hirark]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p>Ada beberapa programmer yang dihadapkan dengan kasus membuat hirarki data dalam SQL database. Hirarki data mempunyai relasi parent-child, namun bersifat rekursif. Tidak natural dalam konsep relasional database. Hirarki data adalah data collection yang tiap itemnya mempunyai single parent atau tidak sama sekali dan mempunyai satu, dua atau lebih children. Hirarki data dapat kita jumpai dalam banyak aplikasi database, termasuk forum dan mailing list, diagram oraganisasi bisnis, produk, multi level marketing dan kode akun dalam accounting. dalam postingan ini, saya mencoba untuk membuat data hirarki dengan kasus membuat kode akun accounting. Contoh yang saya buat, kurang lebih sama dengan kasus-kasus yang lain seperti yang saya jelaskan diawal.</p>
<p>Sebelumnya, kita buat dulu tabel dengan nama tabelnya account (sekalian asaya buat fungsi <span style="color:#ff6600;"><span style="color:#3366ff;"><strong><code>CREATE</code></strong></span> </span>dan <span style="color:#3366ff;"><strong><code>INSERT</code></strong></span> jadi tinggal diikuti aja..) <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
<pre class="brush: sql;">CREATE TABLE `account` (
  `accountId` int(11) NOT NULL AUTO_INCREMENT,
  `parentId` int(11) DEFAULT NULL,
  `accountName` char(20) NOT NULL,
  PRIMARY KEY (`accountId`)
);

INSERT INTO `account` (`accountId`, `parentId`, `accountName`) VALUES
  (1, 0, 'Harta'),  (2, 1, 'Harta Lancar'),  (3, 2, 'Kas'),
  (4, 2, 'Bank'),  (5, 2, 'Piutang Teman'),  (6, 1, 'Harta Tetap'),
  (7, 6, 'Kendaraan'),  (8, 7, 'Vario Matic'),
  (9, 6, 'Product Electronic'),  (10, 6, 'Investasi'),
  (11, 0, 'Pendapatan'),  (12, 11, 'Gaji'),
  (13, 12, 'Gaji gue'),  (14, 11, 'Hasil Investasi');

 SELECT * FROM account;

 +-----------+----------+--------------------+
 &amp;#124; accountId &amp;#124; parentId &amp;#124; accountName        &amp;#124;
 +-----------+----------+--------------------+
 &amp;#124;         1 &amp;#124;        0 &amp;#124; Harta              &amp;#124;
 &amp;#124;         2 &amp;#124;        1 &amp;#124; Harta Lancar       &amp;#124;
 &amp;#124;         3 &amp;#124;        2 &amp;#124; Kas                &amp;#124;
 &amp;#124;         4 &amp;#124;        2 &amp;#124; Bank               &amp;#124;
 &amp;#124;         5 &amp;#124;        2 &amp;#124; Piutang Teman      &amp;#124;
 &amp;#124;         6 &amp;#124;        1 &amp;#124; Harta Tetap        &amp;#124;
 &amp;#124;         7 &amp;#124;        6 &amp;#124; Kendaraan          &amp;#124;
 &amp;#124;         8 &amp;#124;        7 &amp;#124; Vario Matic        &amp;#124;
 &amp;#124;         9 &amp;#124;        6 &amp;#124; Product Electronic &amp;#124;
 &amp;#124;        10 &amp;#124;        6 &amp;#124; Investasi          &amp;#124;
 &amp;#124;        11 &amp;#124;        0 &amp;#124; Pendapatan         &amp;#124;
 &amp;#124;        12 &amp;#124;       11 &amp;#124; Gaji               &amp;#124;
 &amp;#124;        13 &amp;#124;       12 &amp;#124; Gaji gue           &amp;#124;
 &amp;#124;        14 &amp;#124;       11 &amp;#124; Hasil Investasi    &amp;#124;
 +-----------+----------+--------------------+
 14 rows in set (0.00 sec)</pre>
<p><!--more Penjelasan tentang query yang dibuat --><br />
Pada hasil dari <span style="color:#3366ff;"><code>SELECT</code> </span>diatas, bahwa tiap item pada tabel berisi <code>parentId</code> yang menunjuk ke parentnya. pada baris pertama dan 11 berisi 0. Ini berarti row tersebut tidak mempunyai parent atau bisa disebut sebagai parent itu sendiri. Model tabel diatas bisa diterjemahkan <strong>Vario Matic</strong> adalah child dari <strong>Kendaraan</strong>, <strong>Kendaraan</strong> adalah child dari <strong>Harta Tetap</strong> dan <strong>Harta Tetap</strong> adalah child dari <strong>Harta</strong> yang mana tidak mempunyai parent lagi alias parent itu sendiri. Walaupun tabel diatas sudah dapat diterjemahkan, tetapi masih dibutuhkan suatu query statement agar tampilan data collection lebih mudah untuk dilihat/dimengerti. Untuk menampilkan data tersebut dapat kita gunakan <span style="color:#3366ff;"><code>LEFT JOIN</code></span> :</p>
<pre class="brush: sql;">
mysql&gt; SELECT a1.accountName AS Parent, a2.accountName as Child1,
 -&gt;   a3.accountName as Child2, a4.accountName as Child3
 -&gt;   FROM account AS a1
 -&gt;   LEFT JOIN account AS a2 ON a2.parentId = a1.accountId
 -&gt;   LEFT JOIN account AS a3 ON a3.parentId = a2.accountId
 -&gt;   LEFT JOIN account AS a4 ON a4.parentId = a3.accountId
 -&gt;   WHERE a1.parentId =0;
+------------+-----------------+--------------------+-------------+
&amp;#124; Parent     &amp;#124; Child1          &amp;#124; Child2             &amp;#124; Child3      &amp;#124;
+------------+-----------------+--------------------+-------------+
&amp;#124; Harta      &amp;#124; Harta Lancar    &amp;#124; Kas                &amp;#124; NULL        &amp;#124;
&amp;#124; Harta      &amp;#124; Harta Lancar    &amp;#124; Bank               &amp;#124; NULL        &amp;#124;
&amp;#124; Harta      &amp;#124; Harta Lancar    &amp;#124; Piutang Teman      &amp;#124; NULL        &amp;#124;
&amp;#124; Harta      &amp;#124; Harta Tetap     &amp;#124; Kendaraan          &amp;#124; Vario Matic &amp;#124;
&amp;#124; Harta      &amp;#124; Harta Tetap     &amp;#124; Product Electronic &amp;#124; NULL        &amp;#124;
&amp;#124; Harta      &amp;#124; Harta Tetap     &amp;#124; Investasi          &amp;#124; NULL        &amp;#124;
&amp;#124; Pendapatan &amp;#124; Gaji            &amp;#124; Gaji gue           &amp;#124; NULL        &amp;#124;
&amp;#124; Pendapatan &amp;#124; Hasil Investasi &amp;#124; NULL               &amp;#124; NULL        &amp;#124;
+------------+-----------------+--------------------+-------------+
8 rows in set (0.00 sec)</pre>
<p>Untuk menampilkan data account yang paling bawah atau account yang tidak mempunyai child, saya menggunakan <code>LEFT JOIN</code>:</p>
<pre class="brush: sql;">
mysql&gt; SELECT aOne.accountName FROM
-&gt; account AS aOne LEFT JOIN account as aTwo
 -&gt; ON aOne.accountId = aTwo.parentId
 -&gt; WHERE aTwo.accountId IS NULL;
+--------------------+
&amp;#124; accountName        &amp;#124;
+--------------------+
&amp;#124; Kas                &amp;#124;
&amp;#124; Bank               &amp;#124;
&amp;#124; Piutang Teman      &amp;#124;
&amp;#124; Vario Matic        &amp;#124;
&amp;#124; Product Electronic &amp;#124;
&amp;#124; Investasi          &amp;#124;
&amp;#124; Gaji gue           &amp;#124;
&amp;#124; Hasil Investasi    &amp;#124;
+--------------------+
8 rows in set (0.00 sec)</pre>
<p>Namun, masih ada beberapa yang menurut saya kurang dari postingan ini. Bagaimana membuat query yang dinamis sehingga sewaktu menambahkan child baru tidak harus merubah struktur query yang sudah di buat. <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' />  akan saya posting pada kesempatan yang akan datang. <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[Join Tables Across Different Databases]]></title>
<link>http://acsummer.wordpress.com/2008/01/02/join-tables-across-different-databases/</link>
<pubDate>Wed, 02 Jan 2008 21:03:45 +0000</pubDate>
<dc:creator>acsummer</dc:creator>
<guid>http://acsummer.wordpress.com/2008/01/02/join-tables-across-different-databases/</guid>
<description><![CDATA[Joining tables in queries across different MySQL databases is possible if you use fully qualified da]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p>Joining tables in queries across different MySQL databases is possible if you use fully qualified database and table names.</p>
<blockquote><p>Both databases will need to be on the same server for this to work <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p></blockquote>
<pre style="background:#f8f8f8 none repeat scroll 0 50%;overflow:auto;">
SELECT  db1.table1.column1,
        db2.table1.column1
FROM    db1.table1 LEFT JOIN db2.table1
        ON
          (
            db1.table1.column2 = db2.table1.column2
          );</pre>
<p>The example query will pull two columns (column1 from both databases) and display the results of the LEFT JOIN.</p>
</div>]]></content:encoded>
</item>

</channel>
</rss>
