<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="http://www.sqlzoo.net/w/skins/common/feed.css?303"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>http://www.sqlzoo.net/w/index.php?title=Combine_queries&amp;feed=atom&amp;action=history</id>
		<title>Combine queries - Revision history</title>
		<link rel="self" type="application/atom+xml" href="http://www.sqlzoo.net/w/index.php?title=Combine_queries&amp;feed=atom&amp;action=history"/>
		<link rel="alternate" type="text/html" href="http://www.sqlzoo.net/w/index.php?title=Combine_queries&amp;action=history"/>
		<updated>2013-05-21T01:41:03Z</updated>
		<subtitle>Revision history for this page on the wiki</subtitle>
		<generator>MediaWiki 1.20.4</generator>

	<entry>
		<id>http://www.sqlzoo.net/w/index.php?title=Combine_queries&amp;diff=3307&amp;oldid=prev</id>
		<title>Connor at 09:33, 8 August 2012</title>
		<link rel="alternate" type="text/html" href="http://www.sqlzoo.net/w/index.php?title=Combine_queries&amp;diff=3307&amp;oldid=prev"/>
				<updated>2012-08-08T09:33:53Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table class='diff diff-contentalign-left'&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
			&lt;tr style='vertical-align: top;'&gt;
			&lt;td colspan='2' style=&quot;background-color: white; color:black;&quot;&gt;← Older revision&lt;/td&gt;
			&lt;td colspan='2' style=&quot;background-color: white; color:black;&quot;&gt;Revision as of 09:33, 8 August 2012&lt;/td&gt;
			&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 1:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 1:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&amp;lt;p&amp;gt;Here you are shown how to combine multiple queries.&amp;lt;/p&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&amp;lt;p&amp;gt;Here you are shown how to combine multiple queries.&amp;lt;/p&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&amp;lt;p&amp;gt;Both related and unrelated queries can be merged,&amp;lt;/p&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&amp;lt;p&amp;gt;Both related and unrelated queries can be merged,&amp;lt;/p&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;background: #ffa; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&amp;lt;p&amp;gt;if the queries are often used together then combining them together can greatly improve &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;performence&lt;/del&gt;.&amp;lt;/p&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&amp;lt;p&amp;gt;if the queries are often used together then combining them together can greatly improve &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;performance&lt;/ins&gt;.&amp;lt;/p&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&amp;lt;p&amp;gt;Table 1 and 2 show the two separate tables and Table 3 shows the result you would obtain from combining queries.&amp;lt;/p&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&amp;lt;p&amp;gt;Table 1 and 2 show the two separate tables and Table 3 shows the result you would obtain from combining queries.&amp;lt;/p&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&amp;lt;div class=&amp;quot;ref_section&amp;quot;&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;&amp;lt;div class=&amp;quot;ref_section&amp;quot;&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Connor</name></author>	</entry>

	<entry>
		<id>http://www.sqlzoo.net/w/index.php?title=Combine_queries&amp;diff=3306&amp;oldid=prev</id>
		<title>Connor: Created page with &quot;&lt;p&gt;Here you are shown how to combine multiple queries.&lt;/p&gt; &lt;p&gt;Both related and unrelated queries can be merged,&lt;/p&gt; &lt;p&gt;if the queries are often used together then combining th...&quot;</title>
		<link rel="alternate" type="text/html" href="http://www.sqlzoo.net/w/index.php?title=Combine_queries&amp;diff=3306&amp;oldid=prev"/>
				<updated>2012-08-08T09:32:50Z</updated>
		
		<summary type="html">&lt;p&gt;Created page with &amp;quot;&amp;lt;p&amp;gt;Here you are shown how to combine multiple queries.&amp;lt;/p&amp;gt; &amp;lt;p&amp;gt;Both related and unrelated queries can be merged,&amp;lt;/p&amp;gt; &amp;lt;p&amp;gt;if the queries are often used together then combining th...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&amp;lt;p&amp;gt;Here you are shown how to combine multiple queries.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Both related and unrelated queries can be merged,&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;if the queries are often used together then combining them together can greatly improve performence.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Table 1 and 2 show the two separate tables and Table 3 shows the result you would obtain from combining queries.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;div class=&amp;quot;ref_section&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;table class= &amp;quot;db_ref&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;caption&amp;gt;Table 1&amp;lt;/caption&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;th&amp;gt;'''content'''&amp;lt;/th&amp;gt;&amp;lt;th&amp;gt;'''Page name'''&amp;lt;/th&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;hello&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;index.html&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;Hia&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;index.html&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;page2&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;p2.html&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;Index&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;contents.html&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;lt;table class= &amp;quot;db_ref&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;caption&amp;gt;Table 2&amp;lt;/caption&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;th&amp;gt;'''Message'''&amp;lt;/th&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;The site will be down on Tuesday&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;lt;table class= &amp;quot;db_ref&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;caption&amp;gt;Table 3&amp;lt;/caption&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;th&amp;gt;'''pagename'''&amp;lt;/th&amp;gt;&amp;lt;th&amp;gt;'''content'''&amp;lt;/th&amp;gt;&amp;lt;th&amp;gt;'''NULL'''&amp;lt;/th&amp;gt;&amp;lt;th&amp;gt;'''page'''&amp;lt;/th&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;index.html&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;hello&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;page&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;index.html&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;Hia&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;page&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;The site will be down on Tuesday&amp;lt;/td&amp;gt;&amp;lt;td&amp;gt;motd&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;div class='ht'&amp;gt;&lt;br /&gt;
&amp;lt;div class=params&amp;gt;schema:scott&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;source lang=sql class='tidy'&amp;gt;DROP TABLE page;&lt;br /&gt;
DROP TABLE motd;&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;source lang=sql class='setup'&amp;gt; CREATE TABLE page (&lt;br /&gt;
   content TEXT,&lt;br /&gt;
   pagename TEXT );&lt;br /&gt;
INSERT INTO page VALUES ('hello','index.html');&lt;br /&gt;
INSERT INTO page VALUES ('Hia','index.html');&lt;br /&gt;
INSERT INTO page VALUES ('page2','p2.html');&lt;br /&gt;
INSERT INTO page VALUES ('Index','contents.html');&lt;br /&gt;
CREATE TABLE motd (&lt;br /&gt;
  message VARCHAR(100));&lt;br /&gt;
INSERT INTO motd VALUES ('The site will be down on Tuesday');&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;div&amp;gt;&lt;br /&gt;
In this example a typical approach to these tables could be:&lt;br /&gt;
 SELECT pagename, content&lt;br /&gt;
   FROM page&lt;br /&gt;
  WHERE pagename = 'index.html'&lt;br /&gt;
or:&lt;br /&gt;
 SELECT message FROM motd&lt;br /&gt;
These two queries can be combined using &amp;lt;code&amp;gt;UNION&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;NULLs&amp;lt;/code&amp;gt; where necessary&lt;br /&gt;
and therefore a single query can be run allowing a quicker response from the database.&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;source lang='sql' class='def'&amp;gt;&lt;br /&gt;
SELECT pagename, content, NULL, 'page'&lt;br /&gt;
  FROM page&lt;br /&gt;
 WHERE pagename = 'index.html'&lt;br /&gt;
 UNION &lt;br /&gt;
 SELECT NULL, NULL, message, 'motd'&lt;br /&gt;
   FROM motd&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;div class=&amp;quot;ecomm e-oracle&amp;quot; style=&amp;quot;display: none&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Hacks Ref}}&lt;/div&gt;</summary>
		<author><name>Connor</name></author>	</entry>

	</feed>