<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.2.2" -->
<rss version="2.0" 
	xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
	<title>Comments on: How to write SQL JOIN clauses more compactly</title>
	<link>http://www.xaprb.com/blog/2006/11/28/how-to-write-sql-join-clauses-more-compactly/</link>
	<description>Stay curious!</description>
	<pubDate>Fri, 29 Aug 2008 19:59:05 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.2.2</generator>

	<item>
		<title>By: rye</title>
		<link>http://www.xaprb.com/blog/2006/11/28/how-to-write-sql-join-clauses-more-compactly/#comment-5752</link>
		<author>rye</author>
		<pubDate>Sun, 15 Apr 2007 07:24:27 +0000</pubDate>
		<guid>http://www.xaprb.com/blog/2006/11/28/how-to-write-sql-join-clauses-more-compactly/#comment-5752</guid>
		<description>&lt;p&gt;Well another difference between join with 'on' and join with 'using' is that when "select *" is used, with "on" the joined column will be presented twice, while with "using' only once and placed as the first column.

Another point I think noteworthy is that with "using" the joined column must be enclosed in parenthesis/"()", without them it's an error.

ok the DBMS I play with is mysql 5.0.27 on NT.&lt;/p&gt;</description>
		<content:encoded><![CDATA[<p>Well another difference between join with &#8216;on&#8217; and join with &#8216;using&#8217; is that when &#8220;select *&#8221; is used, with &#8220;on&#8221; the joined column will be presented twice, while with &#8220;using&#8217; only once and placed as the first column.</p>
<p>Another point I think noteworthy is that with &#8220;using&#8221; the joined column must be enclosed in parenthesis/&#8221;()&#8221;, without them it&#8217;s an error.</p>
<p>ok the DBMS I play with is mysql 5.0.27 on NT.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Binary Look &#187; links for 2006-12-09</title>
		<link>http://www.xaprb.com/blog/2006/11/28/how-to-write-sql-join-clauses-more-compactly/#comment-2757</link>
		<author>Binary Look &#187; links for 2006-12-09</author>
		<pubDate>Sat, 09 Dec 2006 22:22:05 +0000</pubDate>
		<guid>http://www.xaprb.com/blog/2006/11/28/how-to-write-sql-join-clauses-more-compactly/#comment-2757</guid>
		<description>&lt;p&gt;[...] How to write SQL JOIN clauses more compactly - Xaprb Написание компактного кода SQL применительно к JOIN. (tags: sql mysql join select код программирование базы_данных) [...]&lt;/p&gt;</description>
		<content:encoded><![CDATA[<p>[&#8230;] How to write SQL JOIN clauses more compactly - Xaprb Написание компактного кода SQL применительно к JOIN. (tags: sql mysql join select код программирование базы_данных) [&#8230;]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Xaprb</title>
		<link>http://www.xaprb.com/blog/2006/11/28/how-to-write-sql-join-clauses-more-compactly/#comment-2667</link>
		<author>Xaprb</author>
		<pubDate>Wed, 29 Nov 2006 19:30:05 +0000</pubDate>
		<guid>http://www.xaprb.com/blog/2006/11/28/how-to-write-sql-join-clauses-more-compactly/#comment-2667</guid>
		<description>&lt;p&gt;Rudy, very good points.&lt;/p&gt;

&lt;p&gt;Disclosure: there is not a single USING in any codebase I'm involved with.&lt;/p&gt;</description>
		<content:encoded><![CDATA[<p>Rudy, very good points.</p>
<p>Disclosure: there is not a single USING in any codebase I&#8217;m involved with.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: rudy</title>
		<link>http://www.xaprb.com/blog/2006/11/28/how-to-write-sql-join-clauses-more-compactly/#comment-2659</link>
		<author>rudy</author>
		<pubDate>Wed, 29 Nov 2006 13:30:48 +0000</pubDate>
		<guid>http://www.xaprb.com/blog/2006/11/28/how-to-write-sql-join-clauses-more-compactly/#comment-2659</guid>
		<description>&lt;p&gt;i don't like USING either, because it's vague and can, in certain circumstances, produce ridiculous results&lt;/p&gt;
&lt;p&gt;in the example you gave:&lt;/p&gt;
&lt;pre&gt;select tbl1.col1, tbl2.col2, tbl3.col2, tbl4.col1
from apples as tbl1
   inner join oranges as tbl2 using(col3)
   inner join grapes as tbl3 using(col3)
   inner join peaches as tbl4 using(col3)&lt;/pre&gt;
&lt;p&gt;you say that "USING matches the specified columns from each table" but this is problematic -- is it really &lt;b&gt;each&lt;/b&gt; table?&lt;/p&gt;
&lt;p&gt;in this case, col3 exists in all 4 tables, so does this mean that the last USING is equivalent to:&lt;/p&gt;
&lt;pre&gt; on tbl4.col3 = tbl3.col3
and tbl4.col3 = tbl2.col3
and tbl4.col3 = tbl1.col3&lt;/pre&gt;
&lt;p&gt;and don't get me started on NATURAL joins -- &lt;em&gt;what were they thinking?!!!&lt;/em&gt;&lt;/p&gt;</description>
		<content:encoded><![CDATA[<p>i don&#8217;t like USING either, because it&#8217;s vague and can, in certain circumstances, produce ridiculous results</p>
<p>in the example you gave:</p>
<pre>select tbl1.col1, tbl2.col2, tbl3.col2, tbl4.col1
from apples as tbl1
   inner join oranges as tbl2 using(col3)
   inner join grapes as tbl3 using(col3)
   inner join peaches as tbl4 using(col3)</pre>
<p>you say that &#8220;USING matches the specified columns from each table&#8221; but this is problematic &#8212; is it really <b>each</b> table?</p>
<p>in this case, col3 exists in all 4 tables, so does this mean that the last USING is equivalent to:</p>
<pre> on tbl4.col3 = tbl3.col3
and tbl4.col3 = tbl2.col3
and tbl4.col3 = tbl1.col3</pre>
<p>and don&#8217;t get me started on NATURAL joins &#8212; <em>what were they thinking?!!!</em></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Xaprb</title>
		<link>http://www.xaprb.com/blog/2006/11/28/how-to-write-sql-join-clauses-more-compactly/#comment-2658</link>
		<author>Xaprb</author>
		<pubDate>Wed, 29 Nov 2006 13:22:15 +0000</pubDate>
		<guid>http://www.xaprb.com/blog/2006/11/28/how-to-write-sql-join-clauses-more-compactly/#comment-2658</guid>
		<description>&lt;p&gt;DOH!  What makes it even funnier is that nobody else caught it.  Fixed, thanks Dave.&lt;/p&gt;</description>
		<content:encoded><![CDATA[<p>DOH!  What makes it even funnier is that nobody else caught it.  Fixed, thanks Dave.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: David Shrewsbury</title>
		<link>http://www.xaprb.com/blog/2006/11/28/how-to-write-sql-join-clauses-more-compactly/#comment-2657</link>
		<author>David Shrewsbury</author>
		<pubDate>Wed, 29 Nov 2006 13:20:07 +0000</pubDate>
		<guid>http://www.xaprb.com/blog/2006/11/28/how-to-write-sql-join-clauses-more-compactly/#comment-2657</guid>
		<description>&lt;p&gt;Your first example of using the USING clause is missing the USING clause.  :-)&lt;/p&gt;

&lt;p&gt;-Dave&lt;/p&gt;</description>
		<content:encoded><![CDATA[<p>Your first example of using the USING clause is missing the USING clause.  :-)</p>
<p>-Dave</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Roland Bouman</title>
		<link>http://www.xaprb.com/blog/2006/11/28/how-to-write-sql-join-clauses-more-compactly/#comment-2646</link>
		<author>Roland Bouman</author>
		<pubDate>Tue, 28 Nov 2006 18:49:24 +0000</pubDate>
		<guid>http://www.xaprb.com/blog/2006/11/28/how-to-write-sql-join-clauses-more-compactly/#comment-2646</guid>
		<description>&lt;p&gt;Adequate: well, a lot of people feel that way.&lt;/p&gt;

&lt;p&gt;What I don't like about USING is that it makes you do one and the same thing (JOIN-ing) in two different ways (with ON and USING). It does happen quite often that you have a table that has more than one foreign key to one particular parent table, forcing you to use ON anyway (even if you use the convention of using the same column names for related tables - a convention which I don't prefer). So I rather write all joins the same way - with ON. And indeed, the more exotic non-equi joins pose no problem at all to ON.&lt;/p&gt;

&lt;p&gt;What I think would make the RELATE syntax interesting is that it would allow you to write the join as if it is a sentence or statement. It would allow you to code with the semantics of the relationship rather than the implementation with foreign keys.&lt;/p&gt;

&lt;p&gt;To make this really work, you would actually need to be able to define foreign key constraints with additional role names. Something like&lt;/p&gt;

&lt;pre&gt;ALTER TABLE City 
ADD CONSTRAINT city_resides_in_country
FOREIGN KEY (Country_id)
REFERENCES Country(id)
PARENT_ROLE `contains`
CHILD_ROLE `resides in`
;&lt;/pre&gt;

&lt;p&gt;There should be a unicity constraint on the name of the table, the rolename and the name of the related table. Now the join could be written like&lt;/p&gt;

&lt;pre&gt;SELECT * 
FROM  Country RELATE `Contains` City&lt;/pre&gt;

&lt;p&gt;or, if you like&lt;/p&gt;

&lt;pre&gt;SELECT * 
FROM  City RELATE `Resides in` Country&lt;/pre&gt;

&lt;p&gt;This would totally solve the issue of having more than one foreign key to the same parent table because the two relationships will always play other roles (if not, the design is wrong)&lt;/p&gt;</description>
		<content:encoded><![CDATA[<p>Adequate: well, a lot of people feel that way.</p>
<p>What I don&#8217;t like about USING is that it makes you do one and the same thing (JOIN-ing) in two different ways (with ON and USING). It does happen quite often that you have a table that has more than one foreign key to one particular parent table, forcing you to use ON anyway (even if you use the convention of using the same column names for related tables - a convention which I don&#8217;t prefer). So I rather write all joins the same way - with ON. And indeed, the more exotic non-equi joins pose no problem at all to ON.</p>
<p>What I think would make the RELATE syntax interesting is that it would allow you to write the join as if it is a sentence or statement. It would allow you to code with the semantics of the relationship rather than the implementation with foreign keys.</p>
<p>To make this really work, you would actually need to be able to define foreign key constraints with additional role names. Something like</p>
<pre>ALTER TABLE City
ADD CONSTRAINT city_resides_in_country
FOREIGN KEY (Country_id)
REFERENCES Country(id)
PARENT_ROLE `contains`
CHILD_ROLE `resides in`
;</pre>
<p>There should be a unicity constraint on the name of the table, the rolename and the name of the related table. Now the join could be written like</p>
<pre>SELECT *
FROM  Country RELATE `Contains` City</pre>
<p>or, if you like</p>
<pre>SELECT *
FROM  City RELATE `Resides in` Country</pre>
<p>This would totally solve the issue of having more than one foreign key to the same parent table because the two relationships will always play other roles (if not, the design is wrong)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Xaprb</title>
		<link>http://www.xaprb.com/blog/2006/11/28/how-to-write-sql-join-clauses-more-compactly/#comment-2645</link>
		<author>Xaprb</author>
		<pubDate>Tue, 28 Nov 2006 17:57:54 +0000</pubDate>
		<guid>http://www.xaprb.com/blog/2006/11/28/how-to-write-sql-join-clauses-more-compactly/#comment-2645</guid>
		<description>&lt;p&gt;Roland, I like the RELATE USING syntax your post discusses too.  But I think current syntaxes are adequate; there are so many different ways (especially outer joins) to join tables.  The finer points of specific ways to join tables will always require wordy syntaxes.  For example, a left outer join on a range, like so:&lt;/p&gt;

&lt;pre&gt;... left outer join tbl2 on col = col2 or col3 between col4 and col5...&lt;/pre&gt;

&lt;p&gt;But again, for many simple cases, a foreign key constraint join would make a lot of sense.  I say, bring it on... another way to make things easier.  I like having lots of ways to do things.&lt;/p&gt;

&lt;p&gt;One reason NATURAL JOIN isn't so useful for most things I do: people tend to add a TIMESTAMP column named 'ts'  Obviously this shouldn't be involved in a join :-)&lt;/p&gt;</description>
		<content:encoded><![CDATA[<p>Roland, I like the RELATE USING syntax your post discusses too.  But I think current syntaxes are adequate; there are so many different ways (especially outer joins) to join tables.  The finer points of specific ways to join tables will always require wordy syntaxes.  For example, a left outer join on a range, like so:</p>
<pre>... left outer join tbl2 on col = col2 or col3 between col4 and col5...</pre>
<p>But again, for many simple cases, a foreign key constraint join would make a lot of sense.  I say, bring it on&#8230; another way to make things easier.  I like having lots of ways to do things.</p>
<p>One reason NATURAL JOIN isn&#8217;t so useful for most things I do: people tend to add a TIMESTAMP column named &#8216;ts&#8217;  Obviously this shouldn&#8217;t be involved in a join :-)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Roland Bouman</title>
		<link>http://www.xaprb.com/blog/2006/11/28/how-to-write-sql-join-clauses-more-compactly/#comment-2643</link>
		<author>Roland Bouman</author>
		<pubDate>Tue, 28 Nov 2006 17:20:23 +0000</pubDate>
		<guid>http://www.xaprb.com/blog/2006/11/28/how-to-write-sql-join-clauses-more-compactly/#comment-2643</guid>
		<description>&lt;p&gt;Hi Baron,&lt;/p&gt;

&lt;p&gt;I knew the syntax, but I don't really like it. I am still waiting for what i think is the only sensible compact join syntax, the "foreign key constraint join".&lt;/p&gt;

&lt;p&gt;Read more here: &lt;a href="http://rpbouman.blogspot.com/2006/04/intelligent-sql-join-syntax_09.html"&gt;Intelligent SQL JOIN syntax?&lt;/a&gt;&lt;/p&gt;</description>
		<content:encoded><![CDATA[<p>Hi Baron,</p>
<p>I knew the syntax, but I don&#8217;t really like it. I am still waiting for what i think is the only sensible compact join syntax, the &#8220;foreign key constraint join&#8221;.</p>
<p>Read more here: <a href="http://rpbouman.blogspot.com/2006/04/intelligent-sql-join-syntax_09.html">Intelligent SQL JOIN syntax?</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ronald Bradford</title>
		<link>http://www.xaprb.com/blog/2006/11/28/how-to-write-sql-join-clauses-more-compactly/#comment-2638</link>
		<author>Ronald Bradford</author>
		<pubDate>Tue, 28 Nov 2006 13:37:14 +0000</pubDate>
		<guid>http://www.xaprb.com/blog/2006/11/28/how-to-write-sql-join-clauses-more-compactly/#comment-2638</guid>
		<description>&lt;p&gt;I didn't know that syntax. Good one Baron!&lt;/p&gt;</description>
		<content:encoded><![CDATA[<p>I didn&#8217;t know that syntax. Good one Baron!</p>
]]></content:encoded>
	</item>
</channel>
</rss>
