<?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>MySQL Question of the Day &#187; urs</title>
	<atom:link href="http://mysql-qotd.casperia.net/archives/author/urs/feed" rel="self" type="application/rss+xml" />
	<link>http://mysql-qotd.casperia.net</link>
	<description>mysql 5.0/5.1 questions for learning purposes</description>
	<lastBuildDate>Fri, 06 Aug 2010 16:42:05 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=abc</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Question 41: Which of the queries is the most efficient?</title>
		<link>http://mysql-qotd.casperia.net/archives/305</link>
		<comments>http://mysql-qotd.casperia.net/archives/305#comments</comments>
		<pubDate>Tue, 09 Mar 2010 19:29:57 +0000</pubDate>
		<dc:creator>urs</dc:creator>
				<category><![CDATA[mysql questions]]></category>
		<category><![CDATA[learning]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[question]]></category>

		<guid isPermaLink="false">http://mysql-qotd.casperia.net/?p=305</guid>
		<description><![CDATA[ 
-- test data creation
create table rgen
  select 1 as n
    union select 2 union select 3 union select 4 union select 5;
create table rtst
  select floor(rand()*3) as rt,(rand()*1000000/100) as rs
  from rgen a,rgen b,rgen c,rgen d,rgen e,rgen f,rgen g;
 
With the given tables, you would like to create a result [...]]]></description>
			<content:encoded><![CDATA[<p> </p>
<pre>-- test data creation
create table rgen
  select 1 as n
    union select 2 union select 3 union select 4 union select 5;
create table rtst
  select floor(rand()*3) as rt,(rand()*1000000/100) as rs
  from rgen a,rgen b,rgen c,rgen d,rgen e,rgen f,rgen g;</pre>
<p> <br />
With the given tables, you would like to create a result similar to the one below:<br />
 </p>
<pre>+-------+-------+-------+
| 1nr   | 2nr   | 3nr   |
+-------+-------+-------+
| 26146 | 25874 | 26105 |
+-------+-------+-------+
1 row in set (0.13 sec)</pre>
<p> <br />
Which of the queries is the most efficient?<br />
 </p>
<pre>a) select
     (select count(*) from rtst where rt=0) as 1nr,
     (select count(*) from rtst where rt=1) as 2nr,
     (select count(*) from rtst where rt=2) as 3nr;

b) select
     sum(if(rt=0,1,0)) as 1nr,
     sum(if(rt=1,1,0)) as 2nr,
     sum(if(rt=2,1,0)) as 3nr
   from rtst;

c) select
     v1.c as 1nr,
     v2.c as 2nr,
     v3.c as 3nr
   from
     (select count(*) as c from rtst where rt=0) v1,
     (select count(*) as c from rtst where rt=1) v2,
     (select count(*) as c from rtst where rt=2) v3;</pre>
<p> <br />
[ Querying for Data (15%) - Using SELECT to Retrieve Data ]</p>
]]></content:encoded>
			<wfw:commentRss>http://mysql-qotd.casperia.net/archives/305/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Question 40: Which of the following queries return 0?</title>
		<link>http://mysql-qotd.casperia.net/archives/301</link>
		<comments>http://mysql-qotd.casperia.net/archives/301#comments</comments>
		<pubDate>Mon, 08 Mar 2010 18:27:18 +0000</pubDate>
		<dc:creator>urs</dc:creator>
				<category><![CDATA[mysql questions]]></category>
		<category><![CDATA[learning]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[question]]></category>

		<guid isPermaLink="false">http://mysql-qotd.casperia.net/?p=301</guid>
		<description><![CDATA[&#160;
(Find all correct answers)
&#160;
a) select count(i)
     from (select 1 as i union select 2 union select null) v1
     where i is null;
&#160;
b) select count(*)
     from (select 1 as i union select 2 union select null) v1
     where i is [...]]]></description>
			<content:encoded><![CDATA[<p>&nbsp;<br />
(Find all correct answers)<br />
&nbsp;</p>
<pre>a) select count(i)
     from (select 1 as i union select 2 union select null) v1
     where i is null;
&nbsp;
b) select count(*)
     from (select 1 as i union select 2 union select null) v1
     where i is null;
&nbsp;
c) select count(*)
     from (select 1 as i union select 2 union select null) v1
     where i &lt;=&gt; null;
&nbsp;
d) select count(*)
     from (select 1 as i union select 2 union select null) v1
     where i = null;</pre>
<p>&nbsp;<br />
[ SQL Expressions (15%) - NULL Values ]<br />
&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://mysql-qotd.casperia.net/archives/301/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Question 39: Which of the alternatives below actually work?</title>
		<link>http://mysql-qotd.casperia.net/archives/295</link>
		<comments>http://mysql-qotd.casperia.net/archives/295#comments</comments>
		<pubDate>Sun, 07 Mar 2010 15:42:36 +0000</pubDate>
		<dc:creator>urs</dc:creator>
				<category><![CDATA[mysql questions]]></category>
		<category><![CDATA[learning]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[question]]></category>

		<guid isPermaLink="false">http://mysql-qotd.casperia.net/?p=295</guid>
		<description><![CDATA[&#160;
Imagine you have to create a running number for a query result and select only the line 1, 2 and 18.
You choose user variables as a means to do that.
&#160;
(Find all correct answers)
&#160;
a) set @a=0;
      select *
      from (select (@a:=@a+1) as ln,table_name
    [...]]]></description>
			<content:encoded><![CDATA[<p>&nbsp;<br />
Imagine you have to create a running number for a query result and select only the line 1, 2 and 18.<br />
You choose user variables as a means to do that.<br />
&nbsp;<br />
(Find all correct answers)<br />
&nbsp;</p>
<pre>a) set @a=0;
      select *
      from (select (@a:=@a+1) as ln,table_name
              from information_schema.tables
              where table_schema='mysql' order by table_name) v1
      where ln in (1,2,18);
&nbsp;
b) set @a=0;
    select (@a:=@a+1) as ln,table_name
      from information_schema.tables
      where table_schema='mysql' and @a in (1,2,18)
      order by table_name;
&nbsp;
c) set @a=0;
    select @a as ln,table_name
      from information_schema.tables
      where table_schema='mysql' having (@a:=@a+1) in (1,2,18)
      order by table_name; </pre>
<p>&nbsp;<br />
[ User Variables (5%) - User Variable Properties ]<br />
&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://mysql-qotd.casperia.net/archives/295/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Question 38: Which of the following statements are true?</title>
		<link>http://mysql-qotd.casperia.net/archives/279</link>
		<comments>http://mysql-qotd.casperia.net/archives/279#comments</comments>
		<pubDate>Sat, 06 Mar 2010 16:45:19 +0000</pubDate>
		<dc:creator>urs</dc:creator>
				<category><![CDATA[mysql questions]]></category>
		<category><![CDATA[learning]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[question]]></category>

		<guid isPermaLink="false">http://mysql-qotd.casperia.net/?p=279</guid>
		<description><![CDATA[&#160;
Imagine, that in your environment there is the policy, that the reporting tool doesn&#8217;t use your tables directly,
but that all access is done via views. So you create views for all your tables like this:
&#160;
create view rep.sales as select * from appdb.sales;
&#160;
Which of the following statements are true?
&#160;
a) Using views like this will decrease performance [...]]]></description>
			<content:encoded><![CDATA[<p>&nbsp;<br />
Imagine, that in your environment there is the policy, that the reporting tool doesn&#8217;t use your tables directly,<br />
but that all access is done via views. So you create views for all your tables like this:<br />
&nbsp;</p>
<pre>create view rep.sales as select * from appdb.sales;</pre>
<p>&nbsp;<br />
Which of the following statements are true?<br />
&nbsp;<br />
a) Using views like this will decrease performance a lot because of the <code>select *</code><br />
b) Performance will not be noticeably impacted, because mysql can use the merge algorithm for views like that<br />
c) Columns added after the view-creation will be visible in the view immediately because of the <code>select *</code><br />
d) After the view creation, changes to the base table are not reaching the view<br />
e) Views like these are actually insertable and updatable, as there is a 1:1 relation to the rows in the base table<br />
f) As their name implies, views can&#8217;t be written to<br />
&nbsp;<br />
[ Views (15%) - Creating Views ]<br />
&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://mysql-qotd.casperia.net/archives/279/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Question 37: Which is the correct order of the steps below to execute a prepared statement from the mysql client?</title>
		<link>http://mysql-qotd.casperia.net/archives/276</link>
		<comments>http://mysql-qotd.casperia.net/archives/276#comments</comments>
		<pubDate>Fri, 05 Mar 2010 16:20:24 +0000</pubDate>
		<dc:creator>urs</dc:creator>
				<category><![CDATA[mysql questions]]></category>
		<category><![CDATA[learning]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[question]]></category>

		<guid isPermaLink="false">http://mysql-qotd.casperia.net/?p=276</guid>
		<description><![CDATA[&#160;
1. execute test;
2. deallocate prepare test;
3. prepare test from @a;
4. set @a:=concat('show create database ',ifnull(database(),'mysql'));
&#160;
Which is the correct order?
&#160;
a) 1,2,3,4
b) 4,3,2,1
c) 3,1,2,4
d) 4,3,1,2
&#160;
Extra question: What does the prepared statement in the example actually do?
&#160;
[ Prepared Statements (5%) - Using Prepared Statements from the mysql Client ]
&#160;
]]></description>
			<content:encoded><![CDATA[<p>&nbsp;</p>
<pre>1. execute test;
2. deallocate prepare test;
3. prepare test from @a;
4. set @a:=concat('show create database ',ifnull(database(),'mysql'));</pre>
<p>&nbsp;<br />
Which is the correct order?<br />
&nbsp;</p>
<pre>a) 1,2,3,4
b) 4,3,2,1
c) 3,1,2,4
d) 4,3,1,2</pre>
<p>&nbsp;<br />
Extra question: What does the prepared statement in the example actually do?<br />
&nbsp;<br />
[ Prepared Statements (5%) - Using Prepared Statements from the mysql Client ]<br />
&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://mysql-qotd.casperia.net/archives/276/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Question 36: What is the difference between the following export operations?</title>
		<link>http://mysql-qotd.casperia.net/archives/273</link>
		<comments>http://mysql-qotd.casperia.net/archives/273#comments</comments>
		<pubDate>Thu, 04 Mar 2010 17:30:09 +0000</pubDate>
		<dc:creator>urs</dc:creator>
				<category><![CDATA[mysql questions]]></category>
		<category><![CDATA[learning]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[question]]></category>

		<guid isPermaLink="false">http://mysql-qotd.casperia.net/?p=273</guid>
		<description><![CDATA[&#160;
1. mysqldump -p -u root --databases tt > tt.sql
&#160;
2. mysqldump -p -u root tt > tt.sql
&#160;
(Find all correct answers)
&#160;
a) There is no difference
b) The syntax of 1 is incorrect
c) The syntax of 2 is incorrect
d) 1 will include the create database statement
e) 2 will include the create database statement
&#160;
[ Importing and Exporting Data (5%) - [...]]]></description>
			<content:encoded><![CDATA[<p>&nbsp;</p>
<pre>1. mysqldump -p -u root --databases tt > tt.sql
&nbsp;
2. mysqldump -p -u root tt > tt.sql</pre>
<p>&nbsp;<br />
(Find all correct answers)<br />
&nbsp;</p>
<pre>a) There is no difference
b) The syntax of 1 is incorrect
c) The syntax of 2 is incorrect
d) 1 will include the create database statement
e) 2 will include the create database statement</pre>
<p>&nbsp;<br />
[ Importing and Exporting Data (5%) - Importing and Exporting Data from the Command Line ]<br />
&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://mysql-qotd.casperia.net/archives/273/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Question 35: Which of the following alternatives return the same result as the original?</title>
		<link>http://mysql-qotd.casperia.net/archives/268</link>
		<comments>http://mysql-qotd.casperia.net/archives/268#comments</comments>
		<pubDate>Wed, 03 Mar 2010 15:44:09 +0000</pubDate>
		<dc:creator>urs</dc:creator>
				<category><![CDATA[mysql questions]]></category>
		<category><![CDATA[learning]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[question]]></category>

		<guid isPermaLink="false">http://mysql-qotd.casperia.net/?p=268</guid>
		<description><![CDATA[&#160;
create table work1 (
         site_id int,
	 sale_dt date,
	 amount decimal(10,2),
	 primary key (site_id,sale_dt));
create table work2 (
         site_id int,
	 sale_dt date,
	 amount decimal(10,2),
	 primary key (site_id,sale_dt));
&#160;
-- test data
create table wgen(a1 bit(1));
insert into wgen
  values (1),(1),(1),(0),(1),(1),(1),(1),(0),(1);
&#160;
insert ignore into work1
  [...]]]></description>
			<content:encoded><![CDATA[<p>&nbsp;</p>
<pre>create table work1 (
         site_id int,
	 sale_dt date,
	 amount decimal(10,2),
	 primary key (site_id,sale_dt));
create table work2 (
         site_id int,
	 sale_dt date,
	 amount decimal(10,2),
	 primary key (site_id,sale_dt));
&nbsp;
-- test data
create table wgen(a1 bit(1));
insert into wgen
  values (1),(1),(1),(0),(1),(1),(1),(1),(0),(1);
&nbsp;
insert ignore into work1
  select rand()*100,
         adddate('2010-01-01',
	 interval rand()*360 day),rand()*100000
  from wgen v1,wgen v2,wgen v3,wgen v4;
&nbsp;
insert ignore into work2
  select rand()*100,adddate('2010-01-01',
         interval rand()*360 day),rand()*100000
  from wgen v1,wgen v2,wgen v3,wgen v4;</pre>
<p>&nbsp;<br />
You would like to rewrite the query below in different ways.<br />
&nbsp;</p>
<pre>select count(*)
  from work1 v1
  where not exists (
    select * from work2
    where site_id=v1.site_id and sale_dt=v1.sale_dt);</pre>
<p>&nbsp;<br />
(Find all correct answers)<br />
&nbsp;</p>
<pre>a) select count(*)
     from work1 v1 left join work2 v2
          on (v1.site_id=v2.site_id and v1.sale_dt=v2.sale_dt)
     where v2.site_id is null and v2.sale_dt is null;
&nbsp;
b) select count(*)
     from work1 v1, work2 v2
     where (v1.site_id<>v2.site_id and v1.sale_dt<>v2.sale_dt);
&nbsp;
c) select count(*)
     from work1 v1
     where (v1.site_id,v1.sale_dt)
       not in (select site_id,sale_dt from work2);
&nbsp;
d) select count(*)
     from (select * from work1 minus select * from work2) v1;</pre>
<p>&nbsp;<br />
Extra question: How does the test-data generation work, and how many rows will it create?<br />
&nbsp;<br />
[ Subqueries (10%) - Converting Subqueries to Joins ]<br />
&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://mysql-qotd.casperia.net/archives/268/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Question 34: Connect the queries a-d to the resultsets 1-4</title>
		<link>http://mysql-qotd.casperia.net/archives/265</link>
		<comments>http://mysql-qotd.casperia.net/archives/265#comments</comments>
		<pubDate>Tue, 02 Mar 2010 16:42:16 +0000</pubDate>
		<dc:creator>urs</dc:creator>
				<category><![CDATA[mysql questions]]></category>
		<category><![CDATA[learning]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[question]]></category>

		<guid isPermaLink="false">http://mysql-qotd.casperia.net/?p=265</guid>
		<description><![CDATA[&#160;
create table j1 (i int primary key);
create table j2 like j1;
&#160;
insert into j1 values (1),(2),(3);
insert into j2 values (1),(2),(3);
&#160;
Queries:
&#160;
a) select * from j1,j2;
b) select * from j1,j2 where j1.i=j2.i;
c) select * from j1,j2 where j1.ij2.i;
d) select * from j1,j2 where j1.i>j2.i;
&#160;
Results:
&#160;
1)
+---+---+
&#124; i &#124; i &#124;
+---+---+
&#124; 1 &#124; 1 &#124;
&#124; 2 &#124; 2 &#124;
&#124; 3 &#124; [...]]]></description>
			<content:encoded><![CDATA[<p>&nbsp;</p>
<pre>create table j1 (i int primary key);
create table j2 like j1;
&nbsp;
insert into j1 values (1),(2),(3);
insert into j2 values (1),(2),(3);</pre>
<p>&nbsp;<br />
Queries:<br />
&nbsp;</p>
<pre>a) select * from j1,j2;
b) select * from j1,j2 where j1.i=j2.i;
c) select * from j1,j2 where j1.i<>j2.i;
d) select * from j1,j2 where j1.i>j2.i;</pre>
<p>&nbsp;<br />
Results:<br />
&nbsp;</p>
<pre>1)
+---+---+
| i | i |
+---+---+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
+---+---+
3 rows in set (0.00 sec)
&nbsp;
2)
+---+---+
| i | i |
+---+---+
| 2 | 1 |
| 3 | 1 |
| 1 | 2 |
| 3 | 2 |
| 1 | 3 |
| 2 | 3 |
+---+---+
6 rows in set (0.00 sec)
&nbsp;
3)
+---+---+
| i | i |
+---+---+
| 1 | 1 |
| 2 | 1 |
| 3 | 1 |
| 1 | 2 |
| 2 | 2 |
| 3 | 2 |
| 1 | 3 |
| 2 | 3 |
| 3 | 3 |
+---+---+
9 rows in set (0.00 sec)
&nbsp;
4)
+---+---+
| i | i |
+---+---+
| 2 | 1 |
| 3 | 1 |
| 3 | 2 |
+---+---+
3 rows in set (0.00 sec)</pre>
<p>&nbsp;<br />
[ Joins (10%) - Overview ]<br />
&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://mysql-qotd.casperia.net/archives/265/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Question 33: Connect the queries a-c to the resultsets 1-3</title>
		<link>http://mysql-qotd.casperia.net/archives/262</link>
		<comments>http://mysql-qotd.casperia.net/archives/262#comments</comments>
		<pubDate>Mon, 01 Mar 2010 16:35:02 +0000</pubDate>
		<dc:creator>urs</dc:creator>
				<category><![CDATA[mysql questions]]></category>
		<category><![CDATA[learning]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[question]]></category>

		<guid isPermaLink="false">http://mysql-qotd.casperia.net/?p=262</guid>
		<description><![CDATA[&#160;
create table jj1 (i int primary key);
create table jj2 like jj1;
&#160;
insert into jj1 values (1),(2);
insert into jj2 values (1),(3);
&#160;
Queries:
&#160;
a) select * from jj1 left join jj2 on (jj1.i=jj2.i);
b) select * from jj1 right join jj2 on (jj1.i=jj2.i);
c) select * from jj1 inner join jj2 (jj1.i=jj2.i);
&#160;
&#160;
Results:
&#160;
1)
+------+---+
&#124; i    &#124; i &#124;
+------+---+
&#124;    [...]]]></description>
			<content:encoded><![CDATA[<p>&nbsp;</p>
<pre>create table jj1 (i int primary key);
create table jj2 like jj1;
&nbsp;
insert into jj1 values (1),(2);
insert into jj2 values (1),(3);</pre>
<p>&nbsp;<br />
Queries:<br />
&nbsp;</p>
<pre>a) select * from jj1 left join jj2 on (jj1.i=jj2.i);
b) select * from jj1 right join jj2 on (jj1.i=jj2.i);
c) select * from jj1 inner join jj2 (jj1.i=jj2.i);</pre>
<p>&nbsp;<br />
&nbsp;<br />
Results:<br />
&nbsp;</p>
<pre>1)
+------+---+
| i    | i |
+------+---+
|    1 | 1 |
| NULL | 3 |
+------+---+
2 rows in set (0.02 sec)
&nbsp;
2)
+---+---+
| i | i |
+---+---+
| 1 | 1 |
+---+---+
1 row in set (0.00 sec)
&nbsp;
3)
+---+------+
| i | i    |
+---+------+
| 1 |    1 |
| 2 | NULL |
+---+------+
2 rows in set (0.00 sec)</pre>
<p>&nbsp;<br />
[ Joins (10%) - Overview ]<br />
&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://mysql-qotd.casperia.net/archives/262/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Question 32: What is the result of the following query?</title>
		<link>http://mysql-qotd.casperia.net/archives/258</link>
		<comments>http://mysql-qotd.casperia.net/archives/258#comments</comments>
		<pubDate>Sun, 28 Feb 2010 06:58:57 +0000</pubDate>
		<dc:creator>urs</dc:creator>
				<category><![CDATA[mysql questions]]></category>
		<category><![CDATA[learning]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[question]]></category>

		<guid isPermaLink="false">http://mysql-qotd.casperia.net/?p=258</guid>
		<description><![CDATA[&#160;
create table shop(
	       shop_id int primary key,
	       shop_name varchar(32));
insert into shop values
	       (1,'shop one'),
	       (2,'shop two'),
	       (3,'shop three');
create table product(
      [...]]]></description>
			<content:encoded><![CDATA[<p>&nbsp;</p>
<pre>create table shop(
	       shop_id int primary key,
	       shop_name varchar(32));
insert into shop values
	       (1,'shop one'),
	       (2,'shop two'),
	       (3,'shop three');
create table product(
               product_id int primary key,
	       product_name varchar(32),
	       product_type int);
insert into product values
               (1,'product one',1),
	       (2,'product two',1),
	       (3,'product three',2);
create table sale(
               shop_id int,
               sale_dt date,
	       product_id int,
	       type int,
	       amount decimal(10,2),
	       key ix1(shop_id, sale_dt));
insert into sale values
	       (1,'2010-01-01',1,0,100.50),(1,'2010-01-01',2,0,100.50),
	       (3,'2010-02-01',3,1,100.50),(2,'2010-02-01',3,1,100.50);
&nbsp;
select v2.shop_name,v1.sale_dt,v3.product_type,sum(v1.amount)
  from sale v1, shop v2, product v3
  where v1.shop_id=v2.shop_id and v1.product_id=v3.product_id
  group by v1.shop_id,v1.sale_dt,v3.product_type
  order by 1,2,3;</pre>
<p>&nbsp;<br />
Possible results:<br />
&nbsp;</p>
<pre>a)
+------------+------------+--------------+----------------+
| shop_name  | sale_dt    | product_type | sum(v1.amount) |
+------------+------------+--------------+----------------+
| shop one   | 2010-01-01 |            1 |         201.00 |
| shop two   | 2010-02-01 |            2 |         100.50 |
| shop three | 2010-02-01 |            2 |         100.50 |
+------------+------------+--------------+----------------+
3 rows in set (0.00 sec)
&nbsp;
b)
+------------+------------+--------------+----------------+
| shop_name  | sale_dt    | product_type | sum(v1.amount) |
+------------+------------+--------------+----------------+
| shop one   | 2010-01-01 |            1 |         201.00 |
| shop three | 2010-02-01 |            2 |         100.50 |
| shop two   | 2010-02-01 |            2 |         100.50 |
+------------+------------+--------------+----------------+
3 rows in set (0.00 sec)
&nbsp;
c)
+------------+------------+--------------+----------------+
| shop_name  | sale_dt    | product_type | sum(v1.amount) |
+------------+------------+--------------+----------------+
| shop one   | 2010-01-01 |            1 |         201.00 |
| shop three | 2010-02-01 |            1 |         100.50 |
| shop two   | 2010-02-01 |            2 |         100.50 |
+------------+------------+--------------+----------------+
3 rows in set (0.02 sec)</pre>
<p>&nbsp;<br />
[ Querying for Data (15%) - Using SELECT to Retrieve Data ]<br />
&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://mysql-qotd.casperia.net/archives/258/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

