<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: SQL Tip: Using EXISTS to generate a column in place of a subquery</title>
	<atom:link href="http://www.ghidinelli.com/2009/01/30/sql-tip-using-exists-to-generate-a-column-in-place-of-a-subquery/feed" rel="self" type="application/rss+xml" />
	<link>http://www.ghidinelli.com/2009/01/30/sql-tip-using-exists-to-generate-a-column-in-place-of-a-subquery</link>
	<description></description>
	<lastBuildDate>Thu, 01 Jun 2017 18:51:00 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: John Whish</title>
		<link>http://www.ghidinelli.com/2009/01/30/sql-tip-using-exists-to-generate-a-column-in-place-of-a-subquery/comment-page-1#comment-54403</link>
		<dc:creator>John Whish</dc:creator>
		<pubDate>Tue, 24 Feb 2009 15:05:10 +0000</pubDate>
		<guid isPermaLink="false">http://www.ghidinelli.com/?p=559#comment-54403</guid>
		<description>Just a thought, but wouldn&#039;t you be better off using a derived table? As far as I know the subquery will execute for each row, whereas a derived table runs once.</description>
		<content:encoded><![CDATA[<p>Just a thought, but wouldn&#8217;t you be better off using a derived table? As far as I know the subquery will execute for each row, whereas a derived table runs once.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ben Nadel</title>
		<link>http://www.ghidinelli.com/2009/01/30/sql-tip-using-exists-to-generate-a-column-in-place-of-a-subquery/comment-page-1#comment-53926</link>
		<dc:creator>Ben Nadel</dc:creator>
		<pubDate>Mon, 02 Feb 2009 18:17:13 +0000</pubDate>
		<guid isPermaLink="false">http://www.ghidinelli.com/?p=559#comment-53926</guid>
		<description>IF(), COALESCE()... you can always use a CASE statements in the SELECT:

(CASE WHEN EXISTS( ... ) THEN 1 ELSE 0 END) AS IsEditor</description>
		<content:encoded><![CDATA[<p>IF(), COALESCE()&#8230; you can always use a CASE statements in the SELECT:</p>
<p>(CASE WHEN EXISTS( &#8230; ) THEN 1 ELSE 0 END) AS IsEditor</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: brian</title>
		<link>http://www.ghidinelli.com/2009/01/30/sql-tip-using-exists-to-generate-a-column-in-place-of-a-subquery/comment-page-1#comment-53919</link>
		<dc:creator>brian</dc:creator>
		<pubDate>Sun, 01 Feb 2009 18:38:18 +0000</pubDate>
		<guid isPermaLink="false">http://www.ghidinelli.com/?p=559#comment-53919</guid>
		<description>Sounds like the equivalent of Postgres&#039; COALESCE().  I&#039;ll try running some additional analysis when I get some time for the outer join as well as some other scenarios to better flesh out the trade offs in approaches.</description>
		<content:encoded><![CDATA[<p>Sounds like the equivalent of Postgres&#8217; COALESCE().  I&#8217;ll try running some additional analysis when I get some time for the outer join as well as some other scenarios to better flesh out the trade offs in approaches.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Zack Steinkamp</title>
		<link>http://www.ghidinelli.com/2009/01/30/sql-tip-using-exists-to-generate-a-column-in-place-of-a-subquery/comment-page-1#comment-53918</link>
		<dc:creator>Zack Steinkamp</dc:creator>
		<pubDate>Sun, 01 Feb 2009 17:47:59 +0000</pubDate>
		<guid isPermaLink="false">http://www.ghidinelli.com/?p=559#comment-53918</guid>
		<description>Brian -- IF() is a built-in function to MySql that you can use in your SELECT fields to mutate a value.

Without the IF(), the second column value would either be &quot;Editor&quot; or NULL.  If I was building the app, I&#039;d leave it at that and test for NULL when rendering the output.

I&#039;m curious to see a performance comparison to the EXISTS(-subquery-) method vs. the LEFT JOIN method...</description>
		<content:encoded><![CDATA[<p>Brian &#8212; IF() is a built-in function to MySql that you can use in your SELECT fields to mutate a value.</p>
<p>Without the IF(), the second column value would either be &#8220;Editor&#8221; or NULL.  If I was building the app, I&#8217;d leave it at that and test for NULL when rendering the output.</p>
<p>I&#8217;m curious to see a performance comparison to the EXISTS(-subquery-) method vs. the LEFT JOIN method&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: brian</title>
		<link>http://www.ghidinelli.com/2009/01/30/sql-tip-using-exists-to-generate-a-column-in-place-of-a-subquery/comment-page-1#comment-53917</link>
		<dc:creator>brian</dc:creator>
		<pubDate>Sun, 01 Feb 2009 17:24:13 +0000</pubDate>
		<guid isPermaLink="false">http://www.ghidinelli.com/?p=559#comment-53917</guid>
		<description>@Zack - The concept is intended to be database agnostic although Postgres does support all JOIN syntaxes (INNER, OUTER, CROSS, etc).  Is the IF() a MySQL extension for processing outer joins?  There are many ways to skin the cat of coalescing values but the power of the EXISTS approach is that it can be significantly more narrow than a typical JOIN would afford.  My query should also be portable across most database platforms, although I believe database portability to be a red herring concern for most web applications.

I find the more correctly normalized my schema, the more hoops I need to jump through when it comes time to reporting.  Some people accomplish it with reporting packages but I typically roll up some queries as needed.  I have yet to find a really good reporting suite with an acceptable end-user interface for non-technical users.  Actuate has some tools on top of BIRT that come close but I&#039;m still waiting for &quot;the one&quot; I can drop into my app.</description>
		<content:encoded><![CDATA[<p>@Zack &#8211; The concept is intended to be database agnostic although Postgres does support all JOIN syntaxes (INNER, OUTER, CROSS, etc).  Is the IF() a MySQL extension for processing outer joins?  There are many ways to skin the cat of coalescing values but the power of the EXISTS approach is that it can be significantly more narrow than a typical JOIN would afford.  My query should also be portable across most database platforms, although I believe database portability to be a red herring concern for most web applications.</p>
<p>I find the more correctly normalized my schema, the more hoops I need to jump through when it comes time to reporting.  Some people accomplish it with reporting packages but I typically roll up some queries as needed.  I have yet to find a really good reporting suite with an acceptable end-user interface for non-technical users.  Actuate has some tools on top of BIRT that come close but I&#8217;m still waiting for &#8220;the one&#8221; I can drop into my app.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Zack Steinkamp</title>
		<link>http://www.ghidinelli.com/2009/01/30/sql-tip-using-exists-to-generate-a-column-in-place-of-a-subquery/comment-page-1#comment-53911</link>
		<dc:creator>Zack Steinkamp</dc:creator>
		<pubDate>Sun, 01 Feb 2009 01:25:12 +0000</pubDate>
		<guid isPermaLink="false">http://www.ghidinelli.com/?p=559#comment-53911</guid>
		<description>Not sure if Postgres has a &quot;LEFT JOIN&quot;, but in MySql, this can be done without a subquery...

&lt;code&gt;SELECT name, IF(option_name = &quot;Editor&quot;, 1, 0) FROM customers LEFT JOIN options ON (customers.id = options.customer_id) AND options.option_name=&#039;Editor&#039;;&lt;/code&gt;

The nature of the LEFT JOIN is that it includes all rows from the &quot;left&quot; table, and uses NULLs for values from the right table if there is not a row to join.</description>
		<content:encoded><![CDATA[<p>Not sure if Postgres has a &#8220;LEFT JOIN&#8221;, but in MySql, this can be done without a subquery&#8230;</p>
<p><code>SELECT name, IF(option_name = "Editor", 1, 0) FROM customers LEFT JOIN options ON (customers.id = options.customer_id) AND options.option_name='Editor';</code></p>
<p>The nature of the LEFT JOIN is that it includes all rows from the &#8220;left&#8221; table, and uses NULLs for values from the right table if there is not a row to join.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: brian</title>
		<link>http://www.ghidinelli.com/2009/01/30/sql-tip-using-exists-to-generate-a-column-in-place-of-a-subquery/comment-page-1#comment-53908</link>
		<dc:creator>brian</dc:creator>
		<pubDate>Sat, 31 Jan 2009 18:51:54 +0000</pubDate>
		<guid isPermaLink="false">http://www.ghidinelli.com/?p=559#comment-53908</guid>
		<description>@Andrew - I knew someone was going to point it out! Technically you are correct in the integer vs boolean and your fix would correct it.  Consuming those two queries above with ColdFusion however would be equivalent.  Both approaches would allow you to do say:

&lt;code&gt;
&lt;cfif isEditor&gt;...&lt;/cfif&gt;
&lt;/code&gt;

Thanks though, you are definitely right.  I thought about fixing the first query but it was late and I wanted to go home. :)</description>
		<content:encoded><![CDATA[<p>@Andrew &#8211; I knew someone was going to point it out! Technically you are correct in the integer vs boolean and your fix would correct it.  Consuming those two queries above with ColdFusion however would be equivalent.  Both approaches would allow you to do say:</p>
<p><code><br />
&lt;cfif isEditor&gt;...&lt;/cfif&gt;<br />
</code></p>
<p>Thanks though, you are definitely right.  I thought about fixing the first query but it was late and I wanted to go home. <img src='http://www.ghidinelli.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Andrew</title>
		<link>http://www.ghidinelli.com/2009/01/30/sql-tip-using-exists-to-generate-a-column-in-place-of-a-subquery/comment-page-1#comment-53907</link>
		<dc:creator>Andrew</dc:creator>
		<pubDate>Sat, 31 Jan 2009 18:47:05 +0000</pubDate>
		<guid isPermaLink="false">http://www.ghidinelli.com/?p=559#comment-53907</guid>
		<description>I realize this is a simplified example, but my main issue with these 2 sql selects is that they are not equivalent.  At the postgres level EXISTS returns a boolean, where as count(*) returns a bigint.

As an example, since options doesn&#039;t have a unique contraint on customer_id and option_name it&#039;s possible that a customer_id has multiple Editor option_name entries, and the result of the 2 sql will be different.  Now if the first select was &#039;... select  count(*) &gt; 0 from options ...&#039; then replacing it with exists would be equivalent.

However, I whole heartily agree with using EXPLAIN.</description>
		<content:encoded><![CDATA[<p>I realize this is a simplified example, but my main issue with these 2 sql selects is that they are not equivalent.  At the postgres level EXISTS returns a boolean, where as count(*) returns a bigint.</p>
<p>As an example, since options doesn&#8217;t have a unique contraint on customer_id and option_name it&#8217;s possible that a customer_id has multiple Editor option_name entries, and the result of the 2 sql will be different.  Now if the first select was &#8216;&#8230; select  count(*) &gt; 0 from options &#8230;&#8217; then replacing it with exists would be equivalent.</p>
<p>However, I whole heartily agree with using EXPLAIN.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
