<?xml version="1.0" encoding="UTF-8"?>
<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/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>youdidwhatwithtsql.com &#187; Non-SELECT JOINS</title>
	<atom:link href="http://www.youdidwhatwithtsql.com/tag/non-select-joins/feed" rel="self" type="application/rss+xml" />
	<link>http://www.youdidwhatwithtsql.com</link>
	<description>making DBAs everywhere curse!</description>
	<lastBuildDate>Tue, 31 Jan 2012 12:21:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Non-SELECT Joins in T-SQL and MySQL</title>
		<link>http://www.youdidwhatwithtsql.com/non-select-joins-in-t-sql-and-mysql/64</link>
		<comments>http://www.youdidwhatwithtsql.com/non-select-joins-in-t-sql-and-mysql/64#comments</comments>
		<pubDate>Tue, 21 Apr 2009 20:23:13 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[JOIN]]></category>
		<category><![CDATA[Non-SELECT JOINS]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/non-select-joins-in-t-sql-and-mysql/64</guid>
		<description><![CDATA[I read a great article, by Pinal Dave, on SQL Joins&#160; this week. I thought I’d add something for Non-SELECT joins as I’ve noticed a few developers missing these in their armoury. It doesn’t help that there is no standard so every database implements this differently. This is one of the few occasions where you [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/non-select-joins-in-t-sql-and-mysql/64">Non-SELECT Joins in T-SQL and MySQL</a></p>
]]></description>
			<content:encoded><![CDATA[<p>I read a great article, by <a href="http://blog.sqlauthority.com/" target="_blank">Pinal Dave</a>, on <a href="http://blog.sqlauthority.com/2009/04/13/sql-server-introduction-to-joins-basic-of-joins/ " target="_blank">SQL Joins</a>&#160; this week. I thought I’d add something for Non-SELECT joins as I’ve noticed a few developers missing these in their armoury. It doesn’t help that there is no standard so every database implements this differently. This is one of the few occasions where you will hear me rant about <a href="http://www.mysql.com" target="_blank">MySQL</a> over <a href="http://www.microsoft.com/sqlserver/2008/en/us/default.aspx" target="_blank">SQL Server</a>. The way you express Non-SELECT joins in <a href="http://www.mysql.com" target="_blank">MySQL</a> just seems far more natural to me.</p>
<p>You’ll need both <a href="http://dev.mysql.com/downloads/mysql/5.1.html" target="_blank">MySQL</a> and <a href="http://www.microsoft.com/express/sql/download/" target="_blank">SQL Server 2008 Express</a> to follow this demo. I’ll be providing a simple script to create tables, and populate with data, for both systems. Then I’ll compare how Non-SELECT Joins are performed in each system. So get prepared to switch between environments.</p>
<p><strong>SQL Server create tables and data</strong> </p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p64code7'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p647"><td class="code" id="p64code7"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> Employee
<span style="color: #808080;">&#40;</span>
	id <span style="color: #0000FF;">INTEGER</span> <span style="color: #0000FF;">IDENTITY</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span>, <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span> <span style="color: #0000FF;">PRIMARY</span> <span style="color: #0000FF;">KEY</span> <span style="color: #0000FF;">CLUSTERED</span>,
	FirstName <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">20</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	LastName <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">30</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	Sex <span style="color: #0000FF;">CHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	Dob <span style="color: #0000FF;">DATE</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	Position <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">30</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span> <span style="color: #0000FF;">DEFAULT</span> <span style="color: #FF0000;">'Unassigned'</span>,
	Salary <span style="color: #0000FF;">MONEY</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>
<span style="color: #808080;">&#41;</span>;
<span style="color: #008080;">-- Insert some Employees</span>
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> Employee
<span style="color: #808080;">&#40;</span>
	FirstName,
	LastName,
	Sex,
	Dob,
	Position,
	Salary
<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">VALUES</span> <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'Dave'</span>,<span style="color: #FF0000;">'Smith'</span>,<span style="color: #FF0000;">'M'</span>,<span style="color: #FF0000;">'1978-01-01'</span>,<span style="color: #FF0000;">'Product Director'</span>,<span style="color: #000;">35000.00</span><span style="color: #808080;">&#41;</span>,
<span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'Joe'</span>,<span style="color: #FF0000;">'Bloggs'</span>,<span style="color: #FF0000;">'M'</span>,<span style="color: #FF0000;">'1973-03-11'</span>,<span style="color: #FF0000;">'CEO'</span>,<span style="color: #000;">100000.00</span><span style="color: #808080;">&#41;</span>,
<span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'John'</span>,<span style="color: #FF0000;">'Doe'</span>,<span style="color: #FF0000;">'M'</span>,<span style="color: #FF0000;">'1956-09-29'</span>,<span style="color: #FF0000;">'CFO'</span>,<span style="color: #000;">95000.00</span><span style="color: #808080;">&#41;</span>,
<span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'Karen'</span>,<span style="color: #FF0000;">'Smith'</span>,<span style="color: #FF0000;">'F'</span>,<span style="color: #FF0000;">'1980-02-03'</span>,<span style="color: #FF0000;">'Marketing'</span>,<span style="color: #000;">60000.00</span><span style="color: #808080;">&#41;</span>,
<span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'Clare'</span>,<span style="color: #FF0000;">'Jones'</span>,<span style="color: #FF0000;">'F'</span>,<span style="color: #FF0000;">'1970-10-30'</span>,<span style="color: #FF0000;">'Accounts'</span>,<span style="color: #000;">30000.00</span><span style="color: #808080;">&#41;</span>,
<span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'Fernando'</span>,<span style="color: #FF0000;">'Cruz'</span>,<span style="color: #FF0000;">'M'</span>,<span style="color: #FF0000;">'1978-01-01'</span>,<span style="color: #FF0000;">'Technical Support'</span>,<span style="color: #000;">30000.00</span><span style="color: #808080;">&#41;</span>,
<span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'Steve'</span>,<span style="color: #FF0000;">'Campbell'</span>,<span style="color: #FF0000;">'M'</span>,<span style="color: #FF0000;">'1975-05-17'</span>,<span style="color: #FF0000;">'IT Manager'</span>,<span style="color: #000;">45000.00</span><span style="color: #808080;">&#41;</span>;
<span style="color: #008080;">-- Create a table for Salary adjustments</span>
<span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> SalaryAdjustment
<span style="color: #808080;">&#40;</span>
	EmployeeId <span style="color: #0000FF;">INTEGER</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span> <span style="color: #0000FF;">PRIMARY</span> <span style="color: #0000FF;">KEY</span> <span style="color: #0000FF;">CLUSTERED</span>,
	NewSalary <span style="color: #0000FF;">MONEY</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>
<span style="color: #808080;">&#41;</span>;
<span style="color: #008080;">-- Salary Adjustments</span>
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> SalaryAdjustment
<span style="color: #808080;">&#40;</span>
	EmployeeId,
	NewSalary
<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">VALUES</span>
<span style="color: #808080;">&#40;</span><span style="color: #000;">2</span>, <span style="color: #000;">80000.00</span><span style="color: #808080;">&#41;</span>,<span style="color: #808080;">&#40;</span><span style="color: #000;">6</span>, <span style="color: #000;">40000.00</span><span style="color: #808080;">&#41;</span>;
&nbsp;
<span style="color: #008080;">-- Just a simple table containing Employee IDs</span>
<span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> Leavers
<span style="color: #808080;">&#40;</span>
	EmployeeId <span style="color: #0000FF;">INTEGER</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span> <span style="color: #0000FF;">PRIMARY</span> <span style="color: #0000FF;">KEY</span> <span style="color: #0000FF;">CLUSTERED</span>
<span style="color: #808080;">&#41;</span>;
<span style="color: #008080;">-- Insert some EmployeeIds</span>
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> Leavers <span style="color: #808080;">&#40;</span>EmployeeId<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">VALUES</span> <span style="color: #808080;">&#40;</span><span style="color: #000;">2</span><span style="color: #808080;">&#41;</span>,<span style="color: #808080;">&#40;</span><span style="color: #000;">5</span><span style="color: #808080;">&#41;</span>;</pre></td></tr></table></div>

<p><strong>MySQL create tables and data</strong></p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p64code8'); return false;">View Code</a> MYSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p648"><td class="code" id="p64code8"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #990099; font-weight: bold;">TABLE</span> Employee
<span style="color: #FF00FF;">&#40;</span>
	id <span style="color: #999900; font-weight: bold;">INTEGER</span> <span style="color: #FF9900; font-weight: bold;">AUTO_INCREMENT</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span> <span style="color: #990099; font-weight: bold;">PRIMARY KEY</span><span style="color: #000033;">,</span>
	FirstName <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">20</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span>
	LastName <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">30</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span>
	Sex <span style="color: #000099;">CHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">1</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span>
	Dob <span style="color: #999900; font-weight: bold;">DATE</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span>
	<span style="color: #000099;">Position</span> <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">30</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span> <span style="color: #990099; font-weight: bold;">DEFAULT</span> <span style="color: #008000;">'Unassigned'</span><span style="color: #000033;">,</span>
	Salary <span style="color: #999900; font-weight: bold;">DECIMAL</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">10</span><span style="color: #000033;">,</span><span style="color: #008080;">2</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span>
<span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
<span style="color: #808080; font-style: italic;"># Insert some Employees</span>
<span style="color: #990099; font-weight: bold;">INSERT</span> <span style="color: #990099; font-weight: bold;">INTO</span> Employee
<span style="color: #FF00FF;">&#40;</span>
	FirstName<span style="color: #000033;">,</span>
	LastName<span style="color: #000033;">,</span>
	Sex<span style="color: #000033;">,</span>
	Dob<span style="color: #000033;">,</span>
	<span style="color: #000099;">Position</span><span style="color: #000033;">,</span>
	Salary
<span style="color: #FF00FF;">&#41;</span>
<span style="color: #990099; font-weight: bold;">VALUES</span> <span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">'Dave'</span><span style="color: #000033;">,</span><span style="color: #008000;">'Smith'</span><span style="color: #000033;">,</span><span style="color: #008000;">'M'</span><span style="color: #000033;">,</span><span style="color: #008000;">'1978-01-01'</span><span style="color: #000033;">,</span><span style="color: #008000;">'Product Director'</span><span style="color: #000033;">,</span><span style="color: #008080;">35000.00</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span>
<span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">'Joe'</span><span style="color: #000033;">,</span><span style="color: #008000;">'Bloggs'</span><span style="color: #000033;">,</span><span style="color: #008000;">'M'</span><span style="color: #000033;">,</span><span style="color: #008000;">'1973-03-11'</span><span style="color: #000033;">,</span><span style="color: #008000;">'CEO'</span><span style="color: #000033;">,</span><span style="color: #008080;">100000.00</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span>
<span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">'John'</span><span style="color: #000033;">,</span><span style="color: #008000;">'Doe'</span><span style="color: #000033;">,</span><span style="color: #008000;">'M'</span><span style="color: #000033;">,</span><span style="color: #008000;">'1956-09-29'</span><span style="color: #000033;">,</span><span style="color: #008000;">'CFO'</span><span style="color: #000033;">,</span><span style="color: #008080;">95000.00</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span>
<span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">'Karen'</span><span style="color: #000033;">,</span><span style="color: #008000;">'Smith'</span><span style="color: #000033;">,</span><span style="color: #008000;">'F'</span><span style="color: #000033;">,</span><span style="color: #008000;">'1980-02-03'</span><span style="color: #000033;">,</span><span style="color: #008000;">'Marketing'</span><span style="color: #000033;">,</span><span style="color: #008080;">60000.00</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span>
<span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">'Clare'</span><span style="color: #000033;">,</span><span style="color: #008000;">'Jones'</span><span style="color: #000033;">,</span><span style="color: #008000;">'F'</span><span style="color: #000033;">,</span><span style="color: #008000;">'1970-10-30'</span><span style="color: #000033;">,</span><span style="color: #008000;">'Accounts'</span><span style="color: #000033;">,</span><span style="color: #008080;">30000.00</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span>
<span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">'Fernando'</span><span style="color: #000033;">,</span><span style="color: #008000;">'Cruz'</span><span style="color: #000033;">,</span><span style="color: #008000;">'M'</span><span style="color: #000033;">,</span><span style="color: #008000;">'1978-01-01'</span><span style="color: #000033;">,</span><span style="color: #008000;">'Technical Support'</span><span style="color: #000033;">,</span><span style="color: #008080;">30000.00</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span>
<span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">'Steve'</span><span style="color: #000033;">,</span><span style="color: #008000;">'Campbell'</span><span style="color: #000033;">,</span><span style="color: #008000;">'M'</span><span style="color: #000033;">,</span><span style="color: #008000;">'1975-05-17'</span><span style="color: #000033;">,</span><span style="color: #008000;">'IT Manager'</span><span style="color: #000033;">,</span><span style="color: #008080;">45000.00</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
<span style="color: #808080; font-style: italic;"># Create a table for Salary adjustments</span>
<span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #990099; font-weight: bold;">TABLE</span> SalaryAdjustment
<span style="color: #FF00FF;">&#40;</span>
	EmployeeId <span style="color: #999900; font-weight: bold;">INTEGER</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span> <span style="color: #990099; font-weight: bold;">PRIMARY KEY</span><span style="color: #000033;">,</span>
	NewSalary <span style="color: #999900; font-weight: bold;">DECIMAL</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">10</span><span style="color: #000033;">,</span><span style="color: #008080;">2</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span>
<span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
<span style="color: #808080; font-style: italic;"># Salary Adjustments</span>
<span style="color: #990099; font-weight: bold;">INSERT</span> <span style="color: #990099; font-weight: bold;">INTO</span> SalaryAdjustment
<span style="color: #FF00FF;">&#40;</span>
	EmployeeId<span style="color: #000033;">,</span>
	NewSalary
<span style="color: #FF00FF;">&#41;</span>
<span style="color: #990099; font-weight: bold;">VALUES</span>
<span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">2</span><span style="color: #000033;">,</span> <span style="color: #008080;">80000.00</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">6</span><span style="color: #000033;">,</span> <span style="color: #008080;">40000.00</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
<span style="color: #808080; font-style: italic;"># Just a simple table containing Employee IDs</span>
<span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #990099; font-weight: bold;">TABLE</span> Leavers
<span style="color: #FF00FF;">&#40;</span>
	EmployeeId <span style="color: #999900; font-weight: bold;">INTEGER</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span> <span style="color: #990099; font-weight: bold;">PRIMARY KEY</span>
<span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
<span style="color: #808080; font-style: italic;"># Insert some EmployeeIds</span>
<span style="color: #990099; font-weight: bold;">INSERT</span> <span style="color: #990099; font-weight: bold;">INTO</span> Leavers <span style="color: #FF00FF;">&#40;</span>EmployeeId<span style="color: #FF00FF;">&#41;</span>
<span style="color: #990099; font-weight: bold;">VALUES</span> <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">2</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">5</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span></pre></td></tr></table></div>

<p><strong>SQL Server: UPDATE from another table with a JOIN</strong></p>
<p>This SQL updates the records in <strong>Employee</strong> that are matched in the <strong>SalaryAdjustment</strong> table. While it’s second nature to me now I do recall thinking this was confusing once.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p64code9'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p649"><td class="code" id="p64code9"><pre class="tsql" style="font-family:monospace;"><span style="color: #008080;">-- Update the Employee table with SalaryAdjustment</span>
<span style="color: #0000FF;">UPDATE</span> emp
<span style="color: #0000FF;">SET</span> emp.<span style="color: #202020;">Salary</span> <span style="color: #808080;">=</span> NewSalary
<span style="color: #0000FF;">FROM</span> Employee emp
<span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span> SalaryAdjustment adj <span style="color: #0000FF;">ON</span> emp.<span style="color: #202020;">id</span> <span style="color: #808080;">=</span> adj.<span style="color: #202020;">EmployeeId</span>;</pre></td></tr></table></div>

<p><strong>MySQL: UPDATE from another table with a JOIN</strong></p>
<p>Learning Non-SELECT Joins in <a href="http://www.mysql.com" target="_blank">MySQL</a> really was a breath of fresh air to me. This statement does the same as the SQL Server equivalent above. Doesn&#8217;t this look so more natural?</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p64code10'); return false;">View Code</a> MYSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p6410"><td class="code" id="p64code10"><pre class="mysql" style="font-family:monospace;"><span style="color: #808080; font-style: italic;"># Update the Employee table with SalaryAdjustment</span>
<span style="color: #990099; font-weight: bold;">UPDATE</span> Employee<span style="color: #000033;">,</span> SalaryAdjustment
<span style="color: #990099; font-weight: bold;">SET</span> Employee.Salary <span style="color: #CC0099;">=</span> SalaryAdjustment.NewSalary
<span style="color: #990099; font-weight: bold;">WHERE</span> Employee.id <span style="color: #CC0099;">=</span> SalaryAdjustment.EmployeeId<span style="color: #000033;">;</span></pre></td></tr></table></div>

<p><strong>SQL Server: DELETE from another table with a JOIN</strong></p>
<p><font color="#666666">The statement will delete the records in <strong>Employee</strong> that are matched by the <strong>Leavers</strong> table. I’ve seen people act nervous about what this will actually delete!</font></p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p64code11'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p6411"><td class="code" id="p64code11"><pre class="tsql" style="font-family:monospace;"><span style="color: #008080;">-- Delete the Employees found in Leavers</span>
<span style="color: #0000FF;">DELETE</span> <span style="color: #0000FF;">FROM</span> Employee
<span style="color: #0000FF;">FROM</span> Employee
<span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span> Leavers <span style="color: #0000FF;">ON</span> Employee.<span style="color: #202020;">id</span> <span style="color: #808080;">=</span> Leavers.<span style="color: #202020;">EmployeeId</span>;</pre></td></tr></table></div>

<p><strong>MySQL: DELETE from another table with a JOIN</strong></p>
<p>This statement does the same as the SQL Server equivalent above. Again, I find this syntax just so more natural to work with.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p64code12'); return false;">View Code</a> MYSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p6412"><td class="code" id="p64code12"><pre class="mysql" style="font-family:monospace;"><span style="color: #808080; font-style: italic;"># Delete the Employees found in Leavers</span>
<span style="color: #990099; font-weight: bold;">DELETE</span> <span style="color: #990099; font-weight: bold;">FROM</span> Employee
<span style="color: #990099; font-weight: bold;">USING</span> Employee<span style="color: #000033;">,</span> Leavers
<span style="color: #990099; font-weight: bold;">WHERE</span> Employee.id <span style="color: #CC0099;">=</span> Leavers.EmployeeId<span style="color: #000033;">;</span></pre></td></tr></table></div>

<p>I hope these simple examples have been fun to follow and informative. Using these JOINs in your UPDATE and DELETE statements can be so much more performance pleasing, when compared to sub-selects, so they are worth knowing.</p>
<p><map name='google_ad_map_64_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/64?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_64_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=64&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fnon-select-joins-in-t-sql-and-mysql%2F64' title="Non SELECT Joins in T SQL and MySQL" alt=" Non SELECT Joins in T SQL and MySQL" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/non-select-joins-in-t-sql-and-mysql/64">Non-SELECT Joins in T-SQL and MySQL</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/non-select-joins-in-t-sql-and-mysql/64" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/non-select-joins-in-t-sql-and-mysql/64/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

