<?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>entroducing.com &#187; database</title>
	<atom:link href="http://www.entroducing.com/view/tag/database/feed" rel="self" type="application/rss+xml" />
	<link>http://www.entroducing.com</link>
	<description>to prove that i have too much time</description>
	<lastBuildDate>Sun, 15 May 2011 06:00:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.4</generator>
		<item>
		<title>Using single SQL query to retrieve counts of two subqueries</title>
		<link>http://www.entroducing.com/view/using-single-sql-query-to-retrieve-counts-of-two-subqueries</link>
		<comments>http://www.entroducing.com/view/using-single-sql-query-to-retrieve-counts-of-two-subqueries#comments</comments>
		<pubDate>Sat, 23 Jan 2010 12:06:09 +0000</pubDate>
		<dc:creator>Benny</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[database]]></category>

		<guid isPermaLink="false">http://www.entroducing.com/?p=151</guid>
		<description><![CDATA[Recently received a question from someone asking for a single SQL statement to retrieve the counts for two subqueries (or something like that). I had altered the question and and my answer below. A company has several customers and courses. Each customer may be signing up on several courses. Write an SQL query to display [...]]]></description>
			<content:encoded><![CDATA[<p>Recently received a question from someone asking for a <strong>single </strong>SQL statement to retrieve the counts for two subqueries (or something like that).</p>
<p>I had altered the question and and my answer below.</p>
<blockquote><p>A company has several customers and courses. Each customer may be signing up on several courses.<br />
Write an SQL query to display the number of women and the number of men signing on each course.</p></blockquote>
<p>And my simple database design</p>
<pre class="brush: sql">
CREATE TABLE `CUSTOMER` (
`CUSTOMER_ID` INT NULL AUTO_INCREMENT PRIMARY KEY,
`NAME` VARCHAR( 100 ) NULL ,
`GENDER` CHAR( 1 ) NULL ,
`EMAIL` VARCHAR( 150 ) NULL
) ENGINE = MYISAM ;

CREATE TABLE `COURSE` (
`COURSE_ID` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
`NAME` VARCHAR( 50 ) NOT NULL ,
`DESCRIPTION` VARCHAR( 255 ) NOT NULL ,
`START_DATE` DATE NOT NULL ,
`END_DATE` DATE NOT NULL
PRIMARY KEY ( `COURSE_ID` )
) ENGINE = MYISAM ;

CREATE TABLE `CUSTOMER_COURSE` (
`CUSTOMER_ID` INT NULL ,
`COURSE_ID` INT NULL ,
`STATUS` VARCHAR( 5 ) NULL
) ENGINE = MYISAM ;
</pre>
<p>And the SQL to &#8220;display the number of women and the number of men signing on each course&#8221;</p>
<pre class="brush: sql">
select
    sum(total_males) as total_males,
    sum(total_females) as total_females,
    COURSE_id
from
    (select COUNT(e.GENDER) as total_males , 0 as total_females, ep.COURSE_ID
        from CUSTOMER e, CUSTOMER_COURSE ep
        where ep.CUSTOMER_ID = e.CUSTOMER_ID and e.GENDER = &#039;M&#039;
        group by ep.COURSE_ID
    union all
    select 0 as total_males, COUNT(e.GENDER) as total_females ,ep.COURSE_ID
        from CUSTOMER e, CUSTOMER_COURSE ep
        where ep.CUSTOMER_ID = e.CUSTOMER_ID and e.GENDER = &#039;F&#039;
    group by ep.COURSE_ID
	) t1
group by COURSE_id
</pre>
<p>Explanation:</p>
<ul>
<li> the two subqueries generate 3 columns: total males, total females, course ID</li>
<li> one of the columns in the two subqueries is 0</li>
<li>the two subqueries would then UNION ALL to populate all results for the parent SQL</li>
<li>the parent SQL would then do a SUM/COUNT for the subqueries by grouping the course ID.</li>
<li>the &#8216;t1&#8242; is just a empty table name thrown back to the parent for reference</li>
</ul>
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.entroducing.com%2Fview%2Fusing-single-sql-query-to-retrieve-counts-of-two-subqueries&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light&amp;height=80" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:80px;" allowTransparency="true"></iframe>]]></content:encoded>
			<wfw:commentRss>http://www.entroducing.com/view/using-single-sql-query-to-retrieve-counts-of-two-subqueries/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

